/*******************************************************************************
* 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 java.util.ArrayList;

import org.eclipse.swt.graphics.Color;
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.AMouseListenerObject;

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

    protected ArrayList<Line> lines;
    protected Point lastPosition;
    protected Color color;
    
    public LineSequence() {
        
        super();
        lines = new ArrayList<Line>();
        lastPosition = null;
        color = new Color(Display.getDefault(), 0, 0, 0);
//        System.err.println("Created new LineSequence");
        
    }
    
    private void updateArea() {
        
        Point minPos = null;
        Point maxSize = null;
        
        for (int i=0; i<lines.size(); i++) {
            
            Line l = lines.get(i);
            Point startPos = l.getStartPosition();
            Point endPos = l.getEndPosition();
            
            if (minPos == null) {
                
                minPos = new Point(Math.min(startPos.x, endPos.x), Math.min(startPos.y, endPos.y));
                
            } else {
                
                minPos = new Point(Math.min(minPos.x, Math.min(startPos.x, endPos.x)), Math.min(minPos.y, Math.min(startPos.y, endPos.y)));
                
            }
            
            if (maxSize == null) {
                
                maxSize = new Point(Math.max(startPos.x, endPos.x), Math.max(startPos.y, endPos.y));
                
            } else {
                
                maxSize = new Point(Math.max(maxSize.x, Math.max(startPos.x, endPos.x)), Math.max(maxSize.y, Math.max(startPos.y, endPos.y)));
                
            }
            
        }
        
        if (minPos != null && (minPos.x != 0 || minPos.y != 0)) {
            
            minPos.x = minPos.x-1;
            minPos.y = minPos.y-1;
            
            setArea(new Rectangle(area.x+minPos.x, area.y+minPos.y, area.width-minPos.x, area.height-minPos.y));
            
            for (int i=0; i<lines.size(); i++) {
             
                Line l = lines.get(i);
                Point startPos = l.getStartPosition();
                Point endPos = l.getEndPosition();
                
                l.setStartPosition(new Point(startPos.x-minPos.x, startPos.y-minPos.y));
                l.setEndPosition(new Point(endPos.x-minPos.x, endPos.y-minPos.y));
                
            }
            
        }
        
        if (maxSize != null && (maxSize.x != area.width || maxSize.y != area.height)) {

            setSize(new Point(maxSize.x+2, maxSize.y+2));
            
        }
        
    }
    
    public void addPoint(Point point) {
        
        if (lastPosition != null) {

            Line l = new Line();
            l.setStartPosition(new Point(lastPosition.x-area.x, lastPosition.y-area.y));
            l.setEndPosition(new Point(point.x-area.x, point.y-area.y));
            l.setColor(color);
            lines.add(l);
            addChild(l);
            updateArea();
            
        }

        lastPosition = point;
               
    }

    public void removePoints() {
        
        while (lines.size() > 0) {
            
            Line l = lines.get(0);
            removeChild(l);
            lines.remove(l);
            
        }
        
        lastPosition = null;
        setArea(new Rectangle(0, 0, 0, 0));
        
    }
    
    public void setColor(Color c) {
        
        for (int i=0; i<lines.size(); i++) {
            
            lines.get(i).setColor(c);
            
        }
        
        color = c;
        
    }
    
    public void redraw() {
        
        for (int i=0; i<lines.size(); i++) {
            
            lines.get(i).redraw();
            
        }
        
    }
    
    public Point[] getPoints() {
        
        if (lines.size() > 0) {
        
            Point[] result = new Point[lines.size()+1];
        
            for (int i=0; i<lines.size(); i++) {
            
                Point p = lines.get(i).getStartPosition();
                result[i] = new Point(p.x+area.x, p.y+area.y);
            
            }
        
            Point p = lines.get(lines.size()-1).getEndPosition();
            result[lines.size()] = new Point(p.x+area.x, p.y+area.y);
            return result;
            
        } else {
            
            return new Point[0];
            
        }
        
    }
    
}
