001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.workfloweditor.flowchart.shapes;
020:
021: import java.awt.*;
022: import java.awt.geom.*;
023:
024: import org.openharmonise.workfloweditor.flowchart.*;
025:
026: /**
027: * Shape that represents a dependency between two stages.
028: *
029: * @author Matthew Large
030: * @version $Revision: 1.1 $
031: *
032: */
033: public class ConnectionLine extends AbstractWorkflowShape implements
034: MoveListener {
035:
036: /**
037: * 2D line.
038: */
039: private Line2D.Float m_line = null;
040:
041: /**
042: * Starting workflow stage instance shape.
043: */
044: private StageShape m_startShape = null;
045:
046: /**
047: * Ending workflow stage instance shape.
048: */
049: private StageShape m_endShape = null;
050:
051: /**
052: * Delete box on line.
053: */
054: private LineSelectionPoint m_selectionPoint = null;
055:
056: /**
057: * Colour to draw the line.
058: */
059: private Paint m_drawColor = Color.BLACK;
060:
061: /**
062: * Construcs a new connection line.
063: *
064: * @param x Start X co-ordinate
065: * @param y Start Y co-ordinate
066: */
067: public ConnectionLine(float x, float y) {
068: super (x, y);
069: this .m_line = new Line2D.Float(x, y, x, y);
070: this .m_selectionPoint = new LineSelectionPoint(
071: new Point2D.Double(
072: this .m_line.x2
073: - ((this .m_line.x2 - this .m_line.x1) / 2),
074: this .m_line.y2
075: - ((this .m_line.y2 - this .m_line.y1) / 2)));
076: }
077:
078: /* (non-Javadoc)
079: * @see com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape#drawSelf(java.awt.Graphics2D)
080: */
081: public void drawSelf(Graphics2D g) {
082: g.setStroke(new BasicStroke(2));
083: g.setPaint(this .m_drawColor);
084: g.draw(m_line);
085:
086: double dHeight = m_line.getX1() - m_line.x2;
087: double dWidth = m_line.getY1() - m_line.getY2();
088: double dLength = Math.sqrt(dHeight * dHeight + dWidth * dWidth);
089: double percent = (15 / dLength) * 100;
090:
091: Graphics2D g2 = (Graphics2D) g.create();
092: g2.rotate(Math.toRadians(25), m_line.x2, m_line.y2);
093: Line2D.Float arrow1 = new Line2D.Float(
094: m_line.x2,
095: m_line.y2,
096: new Double(
097: this .m_line.x2
098: - (((this .m_line.x2 - this .m_line.x1) / 100) * percent))
099: .floatValue(),
100: new Double(
101: this .m_line.y2
102: - (((this .m_line.y2 - this .m_line.y1) / 100) * percent))
103: .floatValue());
104: g2.draw(arrow1);
105:
106: Graphics2D g3 = (Graphics2D) g.create();
107: g3.rotate(-Math.toRadians(25), m_line.x2, m_line.y2);
108: Line2D.Float arrow2 = new Line2D.Float(
109: m_line.x2,
110: m_line.y2,
111: new Double(
112: this .m_line.x2
113: - (((this .m_line.x2 - this .m_line.x1) / 100) * percent))
114: .floatValue(),
115: new Double(
116: this .m_line.y2
117: - (((this .m_line.y2 - this .m_line.y1) / 100) * percent))
118: .floatValue());
119: g3.draw(arrow2);
120:
121: this .m_selectionPoint.draw(g);
122: }
123:
124: /**
125: * Sets the end point of the line.
126: *
127: * @param endPoint 2D end point
128: */
129: public void setEndPoint(Point2D.Float endPoint) {
130: this .m_line.x2 = endPoint.x;
131: this .m_line.y2 = endPoint.y;
132: this .setSelectionPointPos();
133: }
134:
135: /**
136: * Sets if the line is selected.
137: *
138: * @param bSelected true to set the line to be selected
139: */
140: public void setSelected(boolean bSelected) {
141: if (bSelected) {
142: this .m_drawColor = new Color(254, 204, 101);
143: } else {
144: this .m_drawColor = Color.BLACK;
145: }
146: }
147:
148: /**
149: * Sets the workflow stage shape to start the line.
150: *
151: * @param startShape Starting workflow stage
152: */
153: public void setStartShape(StageShape startShape) {
154: if (this .m_startShape != null) {
155: this .m_startShape.removeMoveListener(this );
156: }
157: this .m_startShape = startShape;
158: this .m_startShape.addMoveListener(this );
159: this .setStartByShape(startShape);
160: }
161:
162: /**
163: * Returns the workflow stage shape which starts the line.
164: *
165: * @return Starting workflow stage
166: */
167: public StageShape getStartShape() {
168: return this .m_startShape;
169: }
170:
171: /**
172: * Sets the workflow stage shape to end the line.
173: *
174: * @param startShape Ending workflow stage
175: */
176: public void setEndShape(StageShape endShape) {
177: if (this .m_endShape != null) {
178: this .m_endShape.removeMoveListener(this );
179: }
180: this .m_endShape = endShape;
181: this .m_endShape.addMoveListener(this );
182: this .setEndByShape(endShape);
183: }
184:
185: /**
186: * Returns the workflow stage shape which ends the line.
187: *
188: * @return Ending workflow stage
189: */
190: public StageShape getEndShape() {
191: return this .m_endShape;
192: }
193:
194: /**
195: * Checks if the delete box on the line contains a given
196: * co-ordinate.
197: *
198: * @param x X co-ordinate to check
199: * @param y Y co-ordinate to check
200: * @return true if the delete box contains the co-ordinate
201: */
202: public boolean selectionPointContains(double x, double y) {
203: return this .m_selectionPoint.contains(x, y);
204: }
205:
206: /* (non-Javadoc)
207: * @see com.simulacramedia.workfloweditor.flowchart.MoveListener#moved(com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape)
208: */
209: public void moved(AbstractWorkflowShape shape) {
210: if (shape instanceof StageShape) {
211: if (shape == this .m_startShape) {
212: this .setStartByShape((StageShape) shape);
213: } else if (shape == this .m_endShape) {
214: this .setEndByShape((StageShape) shape);
215: }
216: }
217: }
218:
219: /**
220: * Sets the start point of the line based on a given stage shape.
221: *
222: * @param shape Stage shape to start at
223: */
224: private void setStartByShape(StageShape shape) {
225: this .m_line.x1 = shape.getOutConnectionPoint().getX();
226: this .m_line.y1 = shape.getOutConnectionPoint().getY();
227: this .setSelectionPointPos();
228: }
229:
230: /**
231: * Sets the end point of the line based on a given stage shape.
232: *
233: * @param shape Stage shape to end at
234: */
235: private void setEndByShape(StageShape shape) {
236: this .m_line.x2 = shape.getInConnectionPoint().getX();
237: this .m_line.y2 = shape.getInConnectionPoint().getY();
238: this .setSelectionPointPos();
239: }
240:
241: /**
242: * Recalculates the position of the delete box on the line.
243: *
244: */
245: private void setSelectionPointPos() {
246: this .m_selectionPoint.setX(this .m_line.x2
247: - ((this .m_line.x2 - this .m_line.x1) / 2));
248: this .m_selectionPoint.setY(this .m_line.y2
249: - ((this .m_line.y2 - this .m_line.y1) / 2));
250: }
251: }
|