KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > workfloweditor > flowchart > shapes > ConnectionLine


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.workfloweditor.flowchart.shapes;
20
21 import java.awt.*;
22 import java.awt.geom.*;
23
24 import org.openharmonise.workfloweditor.flowchart.*;
25
26
27 /**
28  * Shape that represents a dependency between two stages.
29  *
30  * @author Matthew Large
31  * @version $Revision: 1.1 $
32  *
33  */

34 public class ConnectionLine extends AbstractWorkflowShape implements MoveListener {
35
36     /**
37      * 2D line.
38      */

39     private Line2D.Float m_line = null;
40     
41     /**
42      * Starting workflow stage instance shape.
43      */

44     private StageShape m_startShape = null;
45     
46     /**
47      * Ending workflow stage instance shape.
48      */

49     private StageShape m_endShape = null;
50     
51     /**
52      * Delete box on line.
53      */

54     private LineSelectionPoint m_selectionPoint = null;
55     
56     /**
57      * Colour to draw the line.
58      */

59     private Paint m_drawColor = Color.BLACK;
60
61     /**
62      * Construcs a new connection line.
63      *
64      * @param x Start X co-ordinate
65      * @param y Start Y co-ordinate
66      */

67     public ConnectionLine(float x, float y) {
68         super(x, y);
69         this.m_line = new Line2D.Float(x,y,x,y);
70         this.m_selectionPoint = new LineSelectionPoint( new Point2D.Double(this.m_line.x2-((this.m_line.x2-this.m_line.x1)/2), this.m_line.y2-((this.m_line.y2-this.m_line.y1)/2)) );
71     }
72
73     /* (non-Javadoc)
74      * @see com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape#drawSelf(java.awt.Graphics2D)
75      */

76     public void drawSelf(Graphics2D g) {
77         g.setStroke( new BasicStroke(2));
78         g.setPaint( this.m_drawColor );
79         g.draw(m_line);
80
81         double dHeight = m_line.getX1()-m_line.x2;
82         double dWidth = m_line.getY1()-m_line.getY2();
83         double dLength = Math.sqrt( dHeight*dHeight + dWidth*dWidth );
84         double percent = (15/dLength)*100;
85         
86         Graphics2D g2 = (Graphics2D) g.create();
87         g2.rotate(Math.toRadians(25), m_line.x2, m_line.y2);
88         Line2D.Float arrow1 = new Line2D.Float(m_line.x2, m_line.y2, new Double JavaDoc( this.m_line.x2-(((this.m_line.x2-this.m_line.x1)/100)*percent) ).floatValue(), new Double JavaDoc(this.m_line.y2-(((this.m_line.y2-this.m_line.y1)/100)*percent)).floatValue() );
89         g2.draw(arrow1);
90         
91         Graphics2D g3 = (Graphics2D) g.create();
92         g3.rotate(-Math.toRadians(25), m_line.x2, m_line.y2);
93         Line2D.Float arrow2 = new Line2D.Float(m_line.x2, m_line.y2, new Double JavaDoc( this.m_line.x2-(((this.m_line.x2-this.m_line.x1)/100)*percent) ).floatValue(), new Double JavaDoc(this.m_line.y2-(((this.m_line.y2-this.m_line.y1)/100)*percent)).floatValue() );
94         g3.draw(arrow2);
95         
96         this.m_selectionPoint.draw(g);
97     }
98     
99     /**
100      * Sets the end point of the line.
101      *
102      * @param endPoint 2D end point
103      */

104     public void setEndPoint(Point2D.Float endPoint) {
105         this.m_line.x2 = endPoint.x;
106         this.m_line.y2 = endPoint.y;
107         this.setSelectionPointPos();
108     }
109     
110     /**
111      * Sets if the line is selected.
112      *
113      * @param bSelected true to set the line to be selected
114      */

115     public void setSelected(boolean bSelected) {
116         if(bSelected) {
117             this.m_drawColor = new Color(254,204,101);
118         } else {
119             this.m_drawColor = Color.BLACK;
120         }
121     }
122     
123     /**
124      * Sets the workflow stage shape to start the line.
125      *
126      * @param startShape Starting workflow stage
127      */

128     public void setStartShape(StageShape startShape) {
129         if(this.m_startShape!=null) {
130             this.m_startShape.removeMoveListener(this);
131         }
132         this.m_startShape = startShape;
133         this.m_startShape.addMoveListener(this);
134         this.setStartByShape(startShape);
135     }
136     
137     /**
138      * Returns the workflow stage shape which starts the line.
139      *
140      * @return Starting workflow stage
141      */

142     public StageShape getStartShape() {
143         return this.m_startShape;
144     }
145     
146     /**
147      * Sets the workflow stage shape to end the line.
148      *
149      * @param startShape Ending workflow stage
150      */

151     public void setEndShape(StageShape endShape) {
152         if(this.m_endShape!=null) {
153             this.m_endShape.removeMoveListener(this);
154         }
155         this.m_endShape = endShape;
156         this.m_endShape.addMoveListener(this);
157         this.setEndByShape(endShape);
158     }
159     
160     /**
161      * Returns the workflow stage shape which ends the line.
162      *
163      * @return Ending workflow stage
164      */

165     public StageShape getEndShape() {
166         return this.m_endShape;
167     }
168     
169     /**
170      * Checks if the delete box on the line contains a given
171      * co-ordinate.
172      *
173      * @param x X co-ordinate to check
174      * @param y Y co-ordinate to check
175      * @return true if the delete box contains the co-ordinate
176      */

177     public boolean selectionPointContains(double x, double y) {
178         return this.m_selectionPoint.contains(x, y);
179     }
180
181     /* (non-Javadoc)
182      * @see com.simulacramedia.workfloweditor.flowchart.MoveListener#moved(com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape)
183      */

184     public void moved(AbstractWorkflowShape shape) {
185         if(shape instanceof StageShape) {
186             if(shape==this.m_startShape) {
187                 this.setStartByShape((StageShape) shape);
188             } else if(shape==this.m_endShape) {
189                 this.setEndByShape((StageShape) shape);
190             }
191         }
192     }
193
194     /**
195      * Sets the start point of the line based on a given stage shape.
196      *
197      * @param shape Stage shape to start at
198      */

199     private void setStartByShape(StageShape shape) {
200         this.m_line.x1 = shape.getOutConnectionPoint().getX();
201         this.m_line.y1 = shape.getOutConnectionPoint().getY();
202         this.setSelectionPointPos();
203     }
204
205     /**
206      * Sets the end point of the line based on a given stage shape.
207      *
208      * @param shape Stage shape to end at
209      */

210     private void setEndByShape(StageShape shape) {
211         this.m_line.x2 = shape.getInConnectionPoint().getX();
212         this.m_line.y2 = shape.getInConnectionPoint().getY();
213         this.setSelectionPointPos();
214     }
215
216     /**
217      * Recalculates the position of the delete box on the line.
218      *
219      */

220     private void setSelectionPointPos() {
221         this.m_selectionPoint.setX(this.m_line.x2-((this.m_line.x2-this.m_line.x1)/2));
222         this.m_selectionPoint.setY(this.m_line.y2-((this.m_line.y2-this.m_line.y1)/2));
223     }
224 }
225
Popular Tags