KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.workfloweditor.model.*;
29
30
31 /**
32  * Shape representing a workflow stage instance.
33  *
34  * @author Matthew Large
35  * @version $Revision: 1.1 $
36  *
37  */

38 public class StageShape extends AbstractMoveableShape {
39
40     /**
41      * Connection point for incoming dependencies.
42      */

43     private ConnectionPoint m_inConnectionPoint = null;
44     
45     /**
46      * Connection point for outgoing dependencies.
47      */

48     private ConnectionPoint m_outConnectionPoint = null;
49     
50     /**
51      * Label for this stage.
52      */

53     private JLabel m_label = null;
54     
55     /**
56      * Mandatory checkbox.
57      */

58     private JCheckBox m_isMandatory = null;
59     
60     /**
61      * Inheritence checkbox.
62      */

63     private JCheckBox m_isInheritable = null;
64     
65     /**
66      * Width.
67      */

68     private int m_nWidth = 100;
69     
70     /**
71      * Height.
72      */

73     private int m_nHeight = 80;
74     
75     /**
76      * List of {@link StageShape} dependencies.
77      */

78     private ArrayList m_dependancies = new ArrayList();
79     
80     /**
81      * Workflow stage instance this shape represents.
82      */

83     private WorkflowStage m_stage = null;
84
85     /**
86      * List of {@link RoleShape} objects.
87      */

88     private ArrayList m_roles = new ArrayList();
89     
90     /**
91      * true if this stage has its own roles and therefore does not inherit its stage definition's roles.
92      */

93     private boolean m_bHasOwnRoles = false;
94     
95     /**
96      * Constructs a new stage shape.
97      *
98      * @param x X co-ordinate
99      * @param y Y co-ordinate
100      * @param stage Workflow stage instance
101      */

102     public StageShape(float x, float y, WorkflowStage stage) {
103         super(x, y);
104         this.m_stage = stage;
105         m_label = new JLabel(this.m_stage.getTitle());
106         m_isMandatory = new JCheckBox("Mandatory");
107         if(this.m_stage.isMandatory()) {
108             m_isMandatory.setSelected(true);
109         }
110         m_isInheritable = new JCheckBox("Clear on edit");
111         if(!this.m_stage.isInheritable()) {
112             m_isInheritable.setSelected(true);
113         }
114         if(this.m_isInheritable.getPreferredSize().width>this.m_label.getPreferredSize().width) {
115             this.m_nWidth = this.m_isInheritable.getPreferredSize().width+40;
116         } else {
117             this.m_nWidth = this.m_label.getPreferredSize().width+40;
118         }
119         super.setMoveControl( new Point2D.Double(x, y), this.m_nWidth, 20);
120         Iterator itor = stage.getModel().getAvailableRoles().iterator();
121         while (itor.hasNext()) {
122             Role role = (Role) itor.next();
123             RoleShape roleShape = new RoleShape(role, stage);
124             this.m_roles.add(roleShape);
125             if(roleShape.isSelected()) {
126                 this.m_bHasOwnRoles=true;
127             }
128             m_nHeight = m_nHeight+20;
129         }
130         if(!this.m_bHasOwnRoles) {
131             Iterator itor2 = this.m_roles.iterator();
132             while (itor2.hasNext()) {
133                 RoleShape roleTemp = (RoleShape) itor2.next();
134                 if(this.m_stage.getDefinition().getRoles().contains(roleTemp.getRole())) {
135                     roleTemp.setSelected(true, false);
136                 }
137             }
138         }
139         this.setConnectionsPos();
140     }
141     
142     /* (non-Javadoc)
143      * @see com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape#drawSelf(java.awt.Graphics2D)
144      */

145     public void drawSelf(Graphics2D g) {
146         Graphics2D g1 = (Graphics2D) g.create();
147         g1.setStroke(new BasicStroke(3));
148         Rectangle2D.Float rect = new Rectangle2D.Float(this.getX(), this.getY(), this.m_nWidth, this.m_nHeight);
149         g1.setPaint(Color.WHITE);
150         g1.fill(rect);
151         g1.setPaint(Color.BLACK);
152         g1.draw(rect);
153         rect = new Rectangle2D.Float(this.getX(), this.getY(), this.m_nWidth, 20);
154         g1.setPaint(Color.LIGHT_GRAY);
155         g1.fill(rect);
156         g1.setPaint(Color.BLACK);
157         g1.draw(rect);
158         rect = new Rectangle2D.Float(this.getX()+this.m_nWidth-20, this.getY(), 20, 20);
159         g1.setPaint(Color.BLACK);
160         g1.draw(rect);
161         Line2D.Float line = new Line2D.Float(this.getX()+this.m_nWidth-15, this.getY()+5,this.getX()+this.m_nWidth-5, this.getY()+15);
162         g1.draw(line);
163         line = new Line2D.Float(this.getX()+this.m_nWidth-5, this.getY()+5,this.getX()+this.m_nWidth-15, this.getY()+15);
164         g1.draw(line);
165         
166         this.m_inConnectionPoint.draw(g1);
167         this.m_outConnectionPoint.draw(g1);
168         this.m_inConnectionPoint.draw(g1);
169         this.m_outConnectionPoint.draw(g1);
170         int x = new Float JavaDoc(this.getX()).intValue();
171         int y = new Float JavaDoc(this.getY()).intValue();
172         
173         this.m_label.setSize(this.m_label.getPreferredSize());
174         Graphics2D g2 = (Graphics2D) g.create();
175         g2.translate(x+10, y+2);
176         this.m_label.paint(g2);
177         
178         this.m_isMandatory.setSize(this.m_isMandatory.getPreferredSize());
179         this.m_isMandatory.setBackground(Color.WHITE);
180         Graphics2D g3 = (Graphics2D) g.create();
181         g3.translate(x+3, y+22);
182         this.m_isMandatory.paint(g3);
183         
184         this.m_isInheritable.setSize(this.m_isInheritable.getPreferredSize());
185         this.m_isInheritable.setBackground(Color.WHITE);
186         Graphics2D g4 = (Graphics2D) g.create();
187         g4.translate(x+3, y+42);
188         this.m_isInheritable.paint(g4);
189         
190         g.setPaint(Color.BLACK);
191         g.setStroke( new BasicStroke(1));
192         g.draw( new Line2D.Float(x+10, y+66, x+this.m_nWidth-10, y+66) );
193         
194         int nYpos = y+72;
195         
196         Iterator itor = this.m_roles.iterator();
197         while (itor.hasNext()) {
198             RoleShape roleShape = (RoleShape) itor.next();
199             roleShape.setSize(roleShape.getPreferredSize());
200             roleShape.setBackground(Color.WHITE);
201             Rectangle2D.Float rectTemp = new Rectangle2D.Float(x+2, nYpos, roleShape.getPreferredSize().width, roleShape.getPreferredSize().height);
202             roleShape.setRect(rectTemp);
203             Graphics2D gTemp = (Graphics2D) g.create();
204             gTemp.setPaint(Color.BLACK);
205             gTemp.translate(x+3, nYpos);
206             roleShape.paint(gTemp);
207             nYpos = nYpos+20;
208         }
209     }
210     
211     /**
212      * Configures the connection points.
213      *
214      */

215     private void setConnectionsPos() {
216         if(this.m_inConnectionPoint==null) {
217             this.m_inConnectionPoint = new ConnectionPoint(this.getX()+this.m_nWidth/2, this.getY()-5);
218         } else {
219             this.m_inConnectionPoint.setX(this.getX()+this.m_nWidth/2);
220             this.m_inConnectionPoint.setY(this.getY()-5);
221         }
222         if(this.m_outConnectionPoint==null) {
223             this.m_outConnectionPoint = new ConnectionPoint(this.getX()+this.m_nWidth/2, this.getY()+this.m_nHeight+5);
224             
225         } else {
226             this.m_outConnectionPoint.setX(this.getX()+this.m_nWidth/2);
227             this.m_outConnectionPoint.setY(this.getY()+this.m_nHeight+5);
228             
229         }
230     }
231     
232     /**
233      * Returns the incoming connection point.
234      *
235      * @return Incoming connection point
236      */

237     public ConnectionPoint getInConnectionPoint() {
238         return this.m_inConnectionPoint;
239     }
240     
241     /**
242      * Return the outgoing connection point.
243      *
244      * @return Outgoing connection point
245      */

246     public ConnectionPoint getOutConnectionPoint() {
247         return this.m_outConnectionPoint;
248     }
249     
250     /**
251      * Checks if the incoming connection point contains a given
252      * co-ordinate.
253      *
254      * @param x X co-ordinate to check
255      * @param y Y co-ordinate to check
256      * @return true if the incoming connection point contains the co-ordinate
257      */

258     public boolean inConnectionPointContains(double x, double y) {
259         return this.m_inConnectionPoint.contains(x, y);
260     }
261     
262     /**
263      * Checks if the outgoing connection point contains a given
264      * co-ordinate.
265      *
266      * @param x X co-ordinate to check
267      * @param y Y co-ordinate to check
268      * @return true if the outgoing connection point contains the co-ordinate
269      */

270     public boolean outConnectionPointContains(double x, double y) {
271         return this.m_outConnectionPoint.contains(x, y);
272     }
273     
274     /**
275      * Checks if the mandatory checkbox contains a given co-ordinate.
276      *
277      * @param x X co-ordinate to check
278      * @param y Y co-ordinate to check
279      * @return true if the mandatory checkbox contains the co-ordinate
280      */

281     public boolean mandatoryContains(double x, double y) {
282         Rectangle2D.Float rect = new Rectangle2D.Float(this.getX()+3, this.getY()+30, this.m_isMandatory.getWidth(), 15);
283         return rect.contains(new Double JavaDoc(x).intValue(), new Double JavaDoc(y).intValue());
284     }
285     
286     /**
287      * Checks if the inheritence checkbox contains a given co-ordinate.
288      *
289      * @param x X co-ordinate to check
290      * @param y Y co-ordinate to check
291      * @return true if the inheritence checkbox contains the co-ordinate
292      */

293     public boolean inheritableContains(double x, double y) {
294         Rectangle2D.Float rect = new Rectangle2D.Float(this.getX()+3, this.getY()+50, this.m_isInheritable.getWidth(), 15);
295         return rect.contains(new Double JavaDoc(x).intValue(), new Double JavaDoc(y).intValue());
296     }
297     
298     /**
299      * Checks if the close box contains a given co-ordinate.
300      *
301      * @param x X co-ordinate to check
302      * @param y Y co-ordinate to check
303      * @return true if the close box contains the co-ordinate
304      */

305     public boolean closeContains(double x, double y) {
306         Rectangle2D.Float rect = new Rectangle2D.Float(this.getX()+this.m_nWidth-20, this.getY(), 20, 20);
307         return rect.contains(new Double JavaDoc(x).intValue(), new Double JavaDoc(y).intValue());
308     }
309     
310     /**
311      * Called if hte mandatory checkbox is clicked.
312      *
313      */

314     public void mandatoryClicked() {
315         this.m_stage.setMandatory(!this.m_isMandatory.isSelected());
316         this.m_isMandatory.setSelected(this.m_stage.isMandatory());
317     }
318     
319     /**
320      * Called if hte inheritence checkbox is clicked.
321      *
322      */

323     public void inheritableClicked() {
324         this.m_stage.setInheritable(this.m_isInheritable.isSelected());
325         this.m_isInheritable.setSelected(!this.m_stage.isInheritable());
326     }
327     
328     /* (non-Javadoc)
329      * @see com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape#setX(float)
330      */

331     public void setX(float x) {
332         super.setX(x);
333         this.setConnectionsPos();
334     }
335
336     /* (non-Javadoc)
337      * @see com.simulacramedia.workfloweditor.flowchart.shapes.AbstractWorkflowShape#setY(float)
338      */

339     public void setY(float y) {
340         super.setY(y);
341         this.setConnectionsPos();
342     }
343
344     /**
345      * Adds a dependent shape.
346      *
347      * @param shape Shape to add
348      */

349     public void addDependancy(StageShape shape) {
350         this.m_dependancies.add(shape);
351         this.m_stage.addDependancy(shape.getStage());
352     }
353     
354     /**
355      * Removes a dependent shape.
356      *
357      * @param shape Shape to remove
358      */

359     public void removeDependancy(StageShape shape) {
360         this.m_dependancies.remove(shape);
361         this.m_stage.removeDependancy(shape.getStage());
362     }
363     
364     /**
365      * Returns list of depentent shapes.
366      *
367      * @return List of {@link StageShape} objects
368      */

369     public List JavaDoc getDependancies() {
370         return (List JavaDoc) this.m_dependancies.clone();
371     }
372     
373     /**
374      * Returns the workflow stage instance that this shape represents.
375      *
376      * @return Workflow stage instance
377      */

378     public WorkflowStage getStage() {
379         return this.m_stage;
380     }
381     
382     /**
383      * Called if there is a mouse click within the region of this shape.
384      *
385      * @param x X co-ordinate of mouse click
386      * @param y Y co-ordinate of mouse click
387      */

388     public void mouseClicked(double x, double y) {
389         if(this.inheritableContains(x, y)) {
390             this.inheritableClicked();
391         } else if(this.mandatoryContains(x, y)) {
392             this.mandatoryClicked();
393         } else {
394             Iterator itor = this.m_roles.iterator();
395             while (itor.hasNext()) {
396                 RoleShape roleShape = (RoleShape) itor.next();
397                 if(roleShape.contains(x, y)) {
398                     if(!this.m_bHasOwnRoles) {
399                         Iterator itor2 = this.m_roles.iterator();
400                         while (itor2.hasNext()) {
401                             RoleShape roleTemp = (RoleShape) itor2.next();
402                             if(roleTemp.isSelected()) {
403                                 roleTemp.setSelected(true, true);
404                             }
405                         }
406                         this.m_bHasOwnRoles = true;
407                     }
408                     if(roleShape.isSelected()) {
409                         
410                         int nSelectedCount = 0;
411                         Iterator itor2 = this.m_roles.iterator();
412                         while (itor2.hasNext()) {
413                             RoleShape checkRole = (RoleShape) itor2.next();
414                             if(checkRole.isSelected()) {
415                                 nSelectedCount++;
416                             }
417                         }
418                         if(nSelectedCount>1) {
419                             roleShape.setSelected(false, true);
420                         }
421                     } else {
422                         roleShape.setSelected(true, true);
423                     }
424                 }
425             }
426         }
427     }
428     
429     /**
430      * Checks if this shape contains a given co-ordinate.
431      *
432      * @param x X co-ordinate to check
433      * @param y Y co-ordinate to check
434      * @return true if this shape contains the co-ordinate
435      */

436     public boolean contains(double x, double y) {
437         Rectangle2D.Float rect = new Rectangle2D.Float(this.getX(), this.getY(), this.m_nWidth, this.m_nHeight);
438         return rect.contains(x, y);
439     }
440
441 }
442
Popular Tags