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;
020:
021: import java.awt.event.*;
022: import java.awt.geom.*;
023: import java.util.*;
024:
025: import javax.swing.event.*;
026:
027: import org.openharmonise.workfloweditor.flowchart.shapes.*;
028: import org.openharmonise.workfloweditor.model.*;
029:
030: /**
031: * The connect handler deals with mouse gestures for connecting
032: * workflow stage instance shapes by dependency.
033: *
034: * @author Matthew Large
035: * @version $Revision: 1.1 $
036: *
037: */
038: public class ConnectMouseHandler extends MouseInputAdapter {
039:
040: /**
041: * Workflow diagram.
042: */
043: private FlowChart m_chart = null;
044:
045: /**
046: * Starting workflow stage instance shape.
047: */
048: private AbstractWorkflowShape m_startShape = null;
049:
050: /**
051: * Ending workflow stage instance shape.
052: */
053: private AbstractWorkflowShape m_endShape = null;
054:
055: /**
056: * Connection line shape.
057: */
058: private ConnectionLine m_connectionLine = null;
059:
060: /**
061: * Constructs a new connection handler.
062: *
063: * @param chart Workflow diagram
064: */
065: public ConnectMouseHandler(FlowChart chart) {
066: super ();
067: this .m_chart = chart;
068: }
069:
070: /* (non-Javadoc)
071: * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
072: */
073: public void mouseDragged(MouseEvent me) {
074: if (this .m_connectionLine != null) {
075: this .m_connectionLine.setEndPoint(new Point2D.Float(me
076: .getX(), me.getY()));
077: this .m_chart.repaint();
078: }
079: }
080:
081: /* (non-Javadoc)
082: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
083: */
084: public void mousePressed(MouseEvent me) {
085: Iterator itor = this .m_chart.getShapes().iterator();
086: while (itor.hasNext()) {
087: AbstractWorkflowShape shape = (AbstractWorkflowShape) itor
088: .next();
089: if (shape instanceof StageShape) {
090: StageShape stageShape = (StageShape) shape;
091: if (stageShape.outConnectionPointContains(me.getX(), me
092: .getY())) {
093: this .m_startShape = stageShape;
094: this .m_connectionLine = new ConnectionLine(me
095: .getX(), me.getY());
096: this .m_chart.addTempShape(this .m_connectionLine);
097: break;
098: }
099: }
100: }
101: }
102:
103: /* (non-Javadoc)
104: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
105: */
106: public void mouseReleased(MouseEvent me) {
107: boolean bFoundEnd = false;
108: Iterator itor = this .m_chart.getShapes().iterator();
109: while (itor.hasNext()) {
110: AbstractWorkflowShape shape = (AbstractWorkflowShape) itor
111: .next();
112: if (shape instanceof StageShape) {
113: StageShape stageShape = (StageShape) shape;
114: if (stageShape.inConnectionPointContains(me.getX(), me
115: .getY())) {
116: this .m_endShape = stageShape;
117: bFoundEnd = true;
118: break;
119: }
120: }
121: }
122: if (!bFoundEnd) {
123: this .m_chart.removeTempShape(this .m_connectionLine);
124: } else {
125: boolean bLooped = false;
126:
127: WorkflowStage startStage = ((StageShape) this .m_startShape)
128: .getStage();
129: WorkflowStage endStage = ((StageShape) this .m_endShape)
130: .getStage();
131: if (startStage == endStage) {
132: bLooped = true;
133: } else {
134: itor = startStage.getDependancies().iterator();
135: while (itor.hasNext()) {
136: WorkflowStage tempStage = (WorkflowStage) itor
137: .next();
138: if (tempStage == endStage) {
139: bLooped = true;
140: } else {
141:
142: }
143: if (!bLooped
144: && tempStage.getDependancies().size() > 0
145: && this .isStageInDependancyPath(tempStage,
146: endStage)) {
147: bLooped = true;
148: }
149: }
150: }
151: if (!bLooped) {
152: this .m_connectionLine
153: .setStartShape((StageShape) this .m_startShape);
154: this .m_connectionLine
155: .setEndShape((StageShape) this .m_endShape);
156: ((StageShape) this .m_endShape)
157: .addDependancy((StageShape) this .m_startShape);
158: this .m_chart.removeTempShape(this .m_connectionLine);
159: this .m_chart.addShape(this .m_connectionLine);
160: } else {
161: this .m_chart.removeTempShape(this .m_connectionLine);
162: }
163: }
164: this .m_connectionLine = null;
165: this .m_startShape = null;
166: this .m_endShape = null;
167: this .m_chart.repaint();
168: }
169:
170: /**
171: * Checks for dependency loops.
172: *
173: * @param stages Workflow stage whos dependencies are to be checked
174: * @param checkStage Workflow stage to check
175: * @return true if there is a potential dependency loop
176: */
177: private boolean isStageInDependancyPath(WorkflowStage stages,
178: WorkflowStage checkStage) {
179: boolean bLooped = false;
180: Iterator itor = stages.getDependancies().iterator();
181: while (itor.hasNext()) {
182: WorkflowStage tempStage = (WorkflowStage) itor.next();
183: if (tempStage == checkStage) {
184: bLooped = true;
185: } else {
186:
187: }
188: if (!bLooped
189: && tempStage.getDependancies().size() > 0
190: && this .isStageInDependancyPath(tempStage,
191: checkStage)) {
192: bLooped = true;
193: }
194: }
195: return bLooped;
196: }
197:
198: }
|