Package DisplayProject.controls

Source Code of DisplayProject.controls.Route

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.controls;

import java.util.Iterator;

import DisplayProject.Array_Of_DataPoint;
import DisplayProject.DataPoint;
import DisplayProject.UIutils;

/**
* Route is an abstract class that defines routed lines: lines or polylines that are defined in segments consisting of starting and ending points. The points in a route are data points, defined by the DataPoint class.
*
*/
public class Route extends Figure {

    public static final int CLICK_COEFICENT = 5;
    protected Arrow beginArrow;
    protected Arrow endArrow;
    protected Array_Of_DataPoint<DataPoint> pointArray;
    private static final long serialVersionUID = -2603865661806975046L;
    protected int[] xPoints;
    protected int[] yPoints;
    protected int nPoints;

    public Route() {
        super();
        xPoints = new int[0];
        yPoints = new int[0];
        nPoints = 0;
      // TF:15/12/2009:DET-141:Set this up to allow inheriting of popup menu
      this.setInheritsPopupMenu(true);
    }

    public Arrow getBeginArrow() {
        return beginArrow;
    }

    public void setBeginArrow(Arrow beginArrow) {
        this.beginArrow = beginArrow;
    }

    public void setBegArrow(Arrow beginArrow) {
        this.setBeginArrow(beginArrow);
    }

    public Arrow getEndArrow() {
        return endArrow;
    }

    public void setEndArrow(Arrow endArrow) {
        this.endArrow = endArrow;
    }

    /**
     * The GetPointArray method retrieves a copy of the current set of points, in mils, for a PolyLine.
     * You cannot directly manipulate the data point array that composes the segments of a PolyLine.
     * GetPointArray provides you a copy of the array, which you can use as a basis for creating and setting an entirely new array with the SetPointArray method.
     */
    public Array_Of_DataPoint<DataPoint> getPointArray() {
        return this.pointArray;
    }

    /**
     * The SetPointArray method sets a completely new group of points for a PolyLine.
     * @param pointArray
     */
    public void setPointArray(Array_Of_DataPoint<DataPoint> pointArray) {
        this.pointArray = pointArray;
        if (this.pointArray != null){
            this.nPoints = this.pointArray.size();
            this.xPoints = new int[this.nPoints];
            this.yPoints = new int[this.nPoints];
            Iterator<DataPoint> it = this.pointArray.iterator();
            int index = 0;
            while(it.hasNext()){
                DataPoint dp = it.next();
                this.xPoints[index] = UIutils.milsToPixels(dp.X);
                this.yPoints[index] = UIutils.milsToPixels(dp.Y);
                index += 1;
            }
        } else {

        }
        this.sizeToFit();
        if (this.getParent() != null) {
            this.getParent().repaint(this.getX(), this.getY(), this.getWidth(), this.getHeight());
        }
        else {
            this.repaint();
        }
    }
    /**
     * The SetPixelPointArray method sets a completely new group of points for a PolyLine.
     * @param points
     */
    public void setPixelPointArray(Array_Of_DataPoint<DataPoint> points){
        if (points != null){
            Array_Of_DataPoint<DataPoint> ptArray = new Array_Of_DataPoint<DataPoint>();
            for (DataPoint pt : points) {
                if (pt != null){
                    ptArray.add(new DataPoint(UIutils.pixelsToMils(pt.X), UIutils.pixelsToMils(pt.Y)));
                }
            }
            this.setPointArray(ptArray);
        } else {
            setPointArray(null);
        }
        this.sizeToFit();
        this.repaint();
    }
    /**
     * The GetPixelPointArray method retrieves a copy of the current set of points, in pixels, for a PolyLine.
     * You cannot directly manipulate the data point array that composes the segments of a PolyLine.
     * GetPixelPointArray provides you a copy of the array, which you can use as a basis for creating and setting an entirely new array with the SetPixelPointArray method.
     */
    public Array_Of_DataPoint<DataPoint> getPixelPointArray(){
        if (this.pointArray != null){
            Array_Of_DataPoint<DataPoint> pts = new Array_Of_DataPoint<DataPoint>();
            for (DataPoint pt : this.pointArray) {
                if (pt != null){
                    pts.add(new DataPoint(UIutils.milsToPixels(pt.X), UIutils.milsToPixels(pt.Y)));
                }
            }
            return pts;
        } else {
            return null;
        }
    }
    public void sizeToFit(){
        int minX = 1000000;
        int maxX = 0;
        int minY = 1000000;
        int maxY = 0;
        for (int x = 0; x < nPoints; x++){
            minX = Math.min(minX, xPoints[x]);
            maxX = Math.max(maxX, xPoints[x]);
        }
        for (int y = 0; y < nPoints; y++){
            minY = Math.min(minY, xPoints[y]);
            maxY = Math.max(maxY, xPoints[y]);
        }

        int Width = Math.max((maxX - minX), 1);
        int Height = Math.max((maxY - minY), 1);

        super.setSize(Width, Height);
        super.setMinimumSize(getSize());
    }

    public boolean contains(int x, int y) {
        /*
        gradient = y2-y1 / x2- x1
        y0-y1 = m(x0-x1)
     */
        int wdt = UIutils.getLineWeightInPixels(this.lineWeight);

        /*
         * for each segment in the line apply the formula
         */
        for (int i = 0; i < nPoints-1; i++){

            int sx = this.xPoints[i];// - getLocation().x;   
            int ex = this.xPoints[i+1];// - getLocation().x;   
            int sy = this.yPoints[i];// - getLocation().y;   
            int ey = this.yPoints[i+1];// - getLocation().y;   

            if (sx == ex){
                return (sx-2 < x)&&(x<sx+2)&&(Math.min(sy, ey)-2 < y)&&(y < Math.max(sy, ey)+2);
            }

            // gradient = (y1 - y0) / (x1 - x0), but this needs to be reversed for screen coordinates
            double m = (ey - sy) / (sx - ex);

            if (Math.abs((ey-y) - m * (x-ex)) < CLICK_COEFICENT+wdt)
                return true;
        }
        return false;
    }

}
TOP

Related Classes of DisplayProject.controls.Route

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.