KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.awt.geom.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.workfloweditor.*;
29 import org.openharmonise.workfloweditor.flowchart.shapes.*;
30 import org.openharmonise.workfloweditor.model.*;
31 import org.openharmonise.workfloweditor.vfs.*;
32
33
34 /**
35  * The workflow diagram.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class FlowChart extends JComponent {
42
43     /**
44      * List of all the {@link AbstractWorkflowShape} objects in the diagram.
45      */

46     private ArrayList m_shapes = new ArrayList();
47     
48     /**
49      * The model that this diagram represents.
50      */

51     private WorkflowModel m_model = null;
52     
53     /**
54      * List of all the {@link StageShape} objects in the diagram.
55      */

56     private ArrayList m_stages = new ArrayList();
57     
58     /**
59      * Map of end {@link StageShape} object to a list of {@link ConnectionLine} objects.
60      */

61     private HashMap m_lines = new HashMap();
62     
63     /**
64      * List of {@link AbstractWorkflowShape} objects to be displayed temporarily.
65      */

66     private ArrayList m_tempShapes = new ArrayList();
67
68     /**
69      *
70      */

71     public FlowChart() {
72         super();
73         this.setup();
74     }
75     
76     /**
77      * Configures the flow chart.
78      *
79      */

80     private void setup() {
81         MoveMouseHandler moveHandler = new MoveMouseHandler(this);
82         this.addMouseListener(moveHandler);
83         this.addMouseMotionListener(moveHandler);
84         
85         ConnectMouseHandler connectHandler = new ConnectMouseHandler(this);
86         this.addMouseListener(connectHandler);
87         this.addMouseMotionListener(connectHandler);
88         
89         LineSelectionMouseHandler lineHandler = new LineSelectionMouseHandler(this);
90         this.addMouseListener(lineHandler);
91         this.addMouseMotionListener(lineHandler);
92         
93         AttributeMouseHandler attHandler = new AttributeMouseHandler(this);
94         this.addMouseListener(attHandler);
95         this.addMouseMotionListener(attHandler);
96         this.repaint();
97     }
98     
99     
100     /* (non-Javadoc)
101      * @see java.awt.Component#paint(java.awt.Graphics)
102      */

103     public void paint(Graphics g) {
104         Graphics2D g2D = (Graphics2D) g;
105         g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
106         g2D.setPaint( new Color(173, 169, 143) );
107         g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
108
109         for(int i=this.m_stages.size()-1; i>=0; i--) {
110             StageShape stage = (StageShape) this.m_stages.get(i);
111             stage.draw(g2D);
112             if(this.m_lines.get(stage)!=null) {
113                 ArrayList lines = ((ArrayList)this.m_lines.get(stage));
114                 for(int j=lines.size()-1; j>=0; j--) {
115                     ConnectionLine line = (ConnectionLine) lines.get(j);
116                     line.draw(g2D);
117                 }
118             }
119         }
120         Iterator itor = this.m_tempShapes.iterator();
121         while (itor.hasNext()) {
122             AbstractWorkflowShape shape = (AbstractWorkflowShape) itor.next();
123             shape.draw(g2D);
124         }
125     }
126     
127     /**
128      * Adds a shapes to be displayed temporarily.
129      *
130      * @param shape Shape to add
131      */

132     public void addTempShape(AbstractWorkflowShape shape) {
133         this.m_tempShapes.add(shape);
134     }
135     
136     /**
137      * Removes a shape that was added to be displayed temporarily.
138      *
139      * @param shape Shape to be removed
140      */

141     public void removeTempShape(AbstractWorkflowShape shape) {
142         this.m_tempShapes.remove(shape);
143     }
144     
145     /**
146      * Returns a list of all the shapes in the diagram.
147      *
148      * @return List of {@link AbstractWorkflowShape} objects
149      */

150     public List JavaDoc getShapes() {
151         return (List JavaDoc) this.m_shapes.clone();
152     }
153     
154     /**
155      * Adds a shape to the diagram.
156      *
157      * @param shape Shape to add
158      */

159     public void addShape(AbstractWorkflowShape shape) {
160         this.m_shapes.add(shape);
161         this.indexShapes();
162     }
163
164     /**
165      * Removes a shape from the diagram.
166      *
167      * @param shape Shape to remove
168      */

169     public void removeShape(AbstractWorkflowShape shape) {
170         if(shape instanceof StageShape) {
171             StageShape stageShape = (StageShape) shape;
172             Iterator itor = this.getShapes().iterator();
173             while (itor.hasNext()) {
174                 AbstractWorkflowShape shape2 = (AbstractWorkflowShape) itor.next();
175                 if(shape2 instanceof ConnectionLine) {
176                     ConnectionLine line = (ConnectionLine) shape2;
177                     if(line.getStartShape()==stageShape) {
178                         this.removeShape(line);
179                     }
180                     if(line.getEndShape()==stageShape) {
181                         this.removeShape(line);
182                     }
183                 }
184             }
185             itor = stageShape.getDependancies().iterator();
186             while (itor.hasNext()) {
187                 StageShape dependancy = (StageShape) itor.next();
188                 stageShape.removeDependancy(dependancy);
189             }
190             
191         } else if(shape instanceof ConnectionLine) {
192             ConnectionLine line = (ConnectionLine) shape;
193             if(line.getEndShape()!=null && line.getStartShape()!=null) {
194                 line.getEndShape().removeDependancy(line.getStartShape());
195             }
196         }
197         this.m_shapes.remove(shape);
198         this.indexShapes();
199     }
200     
201     /**
202      * Returns the model that this diagram represents.
203      *
204      * @return Model
205      */

206     public WorkflowModel getModel() {
207         return this.m_model;
208     }
209     
210     /**
211      * Sets the model that this diagram should represent.
212      *
213      * @param model Model
214      */

215     public void setModel(WorkflowModel model) {
216         this.m_model = model;
217         
218         int nYpos = 10;
219         
220         Iterator itor = model.getWorkflowStages().iterator();
221         while (itor.hasNext()) {
222             VFSWorkflowStage stage = (VFSWorkflowStage) itor.next();
223             String JavaDoc sWorkflowPath = ((VFSWorkflowModel)model).getStagesCollection().getFullPath();
224             String JavaDoc sStagePath = stage.getPath();
225             
226             int nXtemp = 10;
227             int nYtemp = nYpos;
228             
229             Point2D.Float point = WorkflowLayoutStore.getInstance().getStageLayout(sWorkflowPath, sStagePath);
230             if(point!=null) {
231                 nXtemp = new Float JavaDoc(point.x).intValue();
232                 nYtemp = new Float JavaDoc(point.y).intValue();
233             }
234             
235             StageShape stageShape = new StageShape(nXtemp,nYtemp, stage);
236             this.m_shapes.add(stageShape);
237             
238             nYpos = nYpos+120;
239         }
240         
241         itor = model.getWorkflowStages().iterator();
242         while (itor.hasNext()) {
243             WorkflowStage stage = (WorkflowStage) itor.next();
244             Iterator dependItor = stage.getDependancies().iterator();
245             while (dependItor.hasNext()) {
246                 WorkflowStage dependStage = (WorkflowStage) dependItor.next();
247                 
248                 StageShape startShape = null;
249                 StageShape endShape = null;
250                 Iterator itor2 = this.m_shapes.iterator();
251                 while (itor2.hasNext()) {
252                     AbstractWorkflowShape shape = (AbstractWorkflowShape) itor2.next();
253                     if(shape instanceof StageShape && ((StageShape)shape).getStage()==dependStage) {
254                         startShape = (StageShape) shape;
255                     } else if(shape instanceof StageShape && ((StageShape)shape).getStage()==stage) {
256                         endShape = (StageShape) shape;
257                     }
258                 }
259                 if(startShape!=null && endShape!=null) {
260                     ConnectionLine connection = new ConnectionLine(10,10);
261                     connection.setStartShape(startShape);
262                     connection.setEndShape(endShape);
263                     this.m_shapes.add(connection);
264                 }
265             }
266         }
267         this.indexShapes();
268     }
269     
270     /**
271      * Indexes the shapes in the diagram, applying appropriate layering
272      * to them.
273      *
274      */

275     private void indexShapes() {
276         this.m_stages.clear();
277         this.m_lines.clear();
278         
279         Iterator itor = this.m_shapes.iterator();
280         while (itor.hasNext()) {
281             AbstractWorkflowShape shape = (AbstractWorkflowShape) itor.next();
282             if(shape instanceof StageShape) {
283                 if(!this.m_stages.contains(shape)) {
284                     this.m_stages.add(shape);
285                 }
286                 Iterator itor2 = ((StageShape)shape).getDependancies().iterator();
287                 while (itor2.hasNext()) {
288                     StageShape dependStage = (StageShape) itor2.next();
289                     if(!this.m_stages.contains(dependStage)) {
290                         this.m_stages.add(0, dependStage);
291                     } else if(this.m_stages.indexOf(dependStage)>this.m_stages.indexOf(shape)) {
292                         this.m_stages.remove(dependStage);
293                         this.m_stages.add(this.m_stages.indexOf(shape), dependStage);
294                     }
295                 }
296             } else if(shape instanceof ConnectionLine) {
297                 StageShape endStage = ((ConnectionLine)shape).getEndShape();
298                 if(!this.m_lines.keySet().contains(endStage)) {
299                     ArrayList lines = new ArrayList();
300                     lines.add(shape);
301                     this.m_lines.put(endStage, lines);
302                 } else {
303                     ArrayList lines = (ArrayList) this.m_lines.get(endStage);
304                     lines.add(shape);
305                 }
306             }
307         }
308
309         this.m_shapes.clear();
310         for(int i=this.m_stages.size()-1; i>=0; i--) {
311             StageShape stage = (StageShape) this.m_stages.get(i);
312             this.m_shapes.add(0,stage);
313             if(this.m_lines.get(stage)!=null) {
314                 ArrayList lines = ((ArrayList)this.m_lines.get(stage));
315                 for(int j=lines.size()-1; j>=0; j--) {
316                     ConnectionLine line = (ConnectionLine) lines.get(j);
317                     this.m_shapes.add(0,line);
318                 }
319             }
320         }
321
322         this.paintImmediately(this.getBounds());
323     }
324
325 }
326
Popular Tags