/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 07-Nov-2004
 */
package uk.ac.ic.doc.kevlar.gui.internal.widgets;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;

/**
 * @author Marc
 **/
public class Line extends AMouseListenerObject {

    private Point parentStartPos;
    private Point parentEndPos;
    private Point relativeStartPos;
    private Point relativeEndPos;
    private Color color;
    
    public Line() {
        
        super();
        parentStartPos = new Point(0, 0);
        parentEndPos = new Point(0, 0);
        relativeStartPos = new Point(0, 0);
        relativeEndPos = new Point(0, 0);
        color = new Color(Display.getDefault(), 0, 0, 0);
                        
    }

    public Point getStartPosition() {
        
        return parentStartPos;
        
    }
    
    public void setStartPosition(Point position) {
        
        parentStartPos = position;
        updateLine();
        redraw();
        
    }
    
    public Point getEndPosition() {
        
        return parentEndPos;
        
    }
    
    public void setEndPosition(Point position) {
        
        parentEndPos = position;
        updateLine();
        redraw();
        
    }
    
    public void setColor(Color c) {
        
        color = c;
        
    }
    
    public void setParent(ADrawableObject p) {
    	
    	super.setParent(p);
//    	System.err.println("Parent of this line is... "+p.getClass().getName());
    	
    }
        
    private void updateLine() {
        
        Point position = new Point(Math.min(parentStartPos.x, parentEndPos.x), Math.min(parentStartPos.y, parentEndPos.y));
        Point size = new Point(Math.max(parentStartPos.x, parentEndPos.x), Math.max(parentStartPos.y, parentEndPos.y)); 
        
        setPosition(position);
        setSize(size);
        
        relativeStartPos = new Point(parentStartPos.x-position.x, parentStartPos.y-position.y);
        relativeEndPos = new Point(parentEndPos.x-position.x, parentEndPos.y-position.y);        
        
    }
    
    public void drawBefore(GC gc, Rectangle redrawArea) {
        
        gc.setForeground(color);
        gc.drawLine(relativeStartPos.x-redrawArea.x, relativeStartPos.y-redrawArea.y, relativeEndPos.x-redrawArea.x, relativeEndPos.y-redrawArea.y);
        
    }
    
}
