KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > workfloweditor > flowchart > ConnectMouseHandler


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;
20
21 import java.awt.event.*;
22 import java.awt.geom.*;
23 import java.util.*;
24
25 import javax.swing.event.*;
26
27 import org.openharmonise.workfloweditor.flowchart.shapes.*;
28 import org.openharmonise.workfloweditor.model.*;
29
30
31 /**
32  * The connect handler deals with mouse gestures for connecting
33  * workflow stage instance shapes by dependency.
34  *
35  * @author Matthew Large
36  * @version $Revision: 1.1 $
37  *
38  */

39 public class ConnectMouseHandler extends MouseInputAdapter {
40
41     /**
42      * Workflow diagram.
43      */

44     private FlowChart m_chart = null;
45
46     /**
47      * Starting workflow stage instance shape.
48      */

49     private AbstractWorkflowShape m_startShape = null;
50     
51     /**
52      * Ending workflow stage instance shape.
53      */

54     private AbstractWorkflowShape m_endShape = null;
55
56     /**
57      * Connection line shape.
58      */

59     private ConnectionLine m_connectionLine = null;
60
61     /**
62      * Constructs a new connection handler.
63      *
64      * @param chart Workflow diagram
65      */

66     public ConnectMouseHandler(FlowChart chart) {
67         super();
68         this.m_chart = chart;
69     }
70
71     /* (non-Javadoc)
72      * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
73      */

74     public void mouseDragged(MouseEvent me) {
75         if(this.m_connectionLine!=null) {
76             this.m_connectionLine.setEndPoint( new Point2D.Float(me.getX(), me.getY()) );
77             this.m_chart.repaint();
78         }
79     }
80
81     /* (non-Javadoc)
82      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
83      */

84     public void mousePressed(MouseEvent me) {
85         Iterator itor = this.m_chart.getShapes().iterator();
86         while (itor.hasNext()) {
87             AbstractWorkflowShape shape = (AbstractWorkflowShape) itor.next();
88             if(shape instanceof StageShape) {
89                 StageShape stageShape = (StageShape) shape;
90                 if(stageShape.outConnectionPointContains(me.getX(), me.getY())) {
91                     this.m_startShape = stageShape;
92                     this.m_connectionLine = new ConnectionLine(me.getX(), me.getY());
93                     this.m_chart.addTempShape(this.m_connectionLine);
94                     break;
95                 }
96             }
97         }
98     }
99
100     /* (non-Javadoc)
101      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
102      */

103     public void mouseReleased(MouseEvent me) {
104         boolean bFoundEnd = false;
105         Iterator itor = this.m_chart.getShapes().iterator();
106         while (itor.hasNext()) {
107             AbstractWorkflowShape shape = (AbstractWorkflowShape) itor.next();
108             if(shape instanceof StageShape) {
109                 StageShape stageShape = (StageShape) shape;
110                 if(stageShape.inConnectionPointContains(me.getX(), me.getY())) {
111                     this.m_endShape = stageShape;
112                     bFoundEnd = true;
113                     break;
114                 }
115             }
116         }
117         if(!bFoundEnd) {
118             this.m_chart.removeTempShape(this.m_connectionLine);
119         } else {
120             boolean bLooped = false;
121             
122             WorkflowStage startStage = ((StageShape) this.m_startShape).getStage();
123             WorkflowStage endStage = ((StageShape) this.m_endShape).getStage();
124             if(startStage==endStage) {
125                 bLooped=true;
126             } else {
127                 itor = startStage.getDependancies().iterator();
128                 while (itor.hasNext()) {
129                     WorkflowStage tempStage = (WorkflowStage) itor.next();
130                     if(tempStage==endStage) {
131                         bLooped=true;
132                     } else {
133                         
134                     }
135                     if(!bLooped && tempStage.getDependancies().size()>0 && this.isStageInDependancyPath(tempStage, endStage)) {
136                         bLooped=true;
137                     }
138                 }
139             }
140             if(!bLooped) {
141                 this.m_connectionLine.setStartShape((StageShape) this.m_startShape);
142                 this.m_connectionLine.setEndShape((StageShape) this.m_endShape);
143                 ((StageShape) this.m_endShape).addDependancy((StageShape) this.m_startShape);
144                 this.m_chart.removeTempShape(this.m_connectionLine);
145                 this.m_chart.addShape(this.m_connectionLine);
146             } else {
147                 this.m_chart.removeTempShape(this.m_connectionLine);
148             }
149         }
150         this.m_connectionLine = null;
151         this.m_startShape = null;
152         this.m_endShape = null;
153         this.m_chart.repaint();
154     }
155     
156     /**
157      * Checks for dependency loops.
158      *
159      * @param stages Workflow stage whos dependencies are to be checked
160      * @param checkStage Workflow stage to check
161      * @return true if there is a potential dependency loop
162      */

163     private boolean isStageInDependancyPath(WorkflowStage stages, WorkflowStage checkStage) {
164         boolean bLooped = false;
165         Iterator itor = stages.getDependancies().iterator();
166         while (itor.hasNext()) {
167             WorkflowStage tempStage = (WorkflowStage) itor.next();
168             if(tempStage==checkStage) {
169                 bLooped=true;
170             } else {
171
172             }
173             if(!bLooped && tempStage.getDependancies().size()>0 && this.isStageInDependancyPath(tempStage, checkStage)) {
174                 bLooped=true;
175             }
176         }
177         return bLooped;
178     }
179
180 }
181
Popular Tags