KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > elements > Transition


1 /* Transition.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.jawe.xml.elements;
12
13 import org.enhydra.jawe.xml.*;
14 import org.enhydra.jawe.xml.panels.*;
15 import javax.swing.*;
16 import java.util.*;
17 import java.awt.*;
18 import org.w3c.dom.*;
19
20 /**
21  * Represents a WfMC DTD element that has the similar name.
22  */

23 public class Transition extends XMLCollectionElement {
24    public static final String JavaDoc ROUTING_TYPE="RoutingType";
25    public static final String JavaDoc NO_ROUTING="NOROUTING";
26    public static final String JavaDoc SIMPLE_ROUTING="SIMPLEROUTING";
27
28    private transient Package JavaDoc myPackage;
29    private Condition refCondition=new Condition(); // min==0
30
private Description refDescription=new Description(); // min=0
31
private ExtendedAttributes refExtendedAttributes=new ExtendedAttributes(this); // min=0
32

33    private XMLAttribute attrFrom=new XMLAttribute("From"); //required
34
private XMLAttribute attrTo=new XMLAttribute("To"); //required
35
private XMLAttribute attrName=new XMLAttribute("Name");
36
37    /** The workflow element which is source for transition */
38    private transient XMLComplexElement from=null;
39    /** The workflow element which is tareget for transition */
40    private transient XMLComplexElement to=null;
41    /** The map that holds data about break points for graph object. */
42    private Map ordNoToPoint=new HashMap();
43    /** The type of graphical routing of transition. */
44    private String JavaDoc routingType;
45
46    /** Enables canceling of changes to the extended attributes collection. */
47    private ExtendedAttributes clonedEAs;
48
49    /**
50     * Creates a new instance of the class.
51     *
52     * @param ts The reference to collection of Transitions where
53     * this instance will be put into.
54     */

55    public Transition (Transitions ts) {
56       super(ts);
57
58       try {
59          if (ts.getOwner() instanceof WorkflowProcess) {
60             this.myPackage=((WorkflowProcess)ts.getOwner()).getPackage();
61          } else {
62             this.myPackage=((ActivitySet)ts.getOwner()).getOwnerProcess().getPackage();
63          }
64       } catch (Exception JavaDoc ex) {}
65       fillStructure ();
66    }
67
68    /**
69     * Defines the super-class method. Read the explanation for
70     * this method within XMLComplexElement class.
71     */

72    protected void fillStructure () {
73       super.fillStructure();
74       attrFrom.setReadOnly(true);
75       attrTo.setReadOnly(true);
76
77       complexStructure.add(attrName);
78       attributes.add(attrName);
79       attrFrom.setRequired(true);
80       complexStructure.add(attrFrom);
81       attributes.add(attrFrom);
82       attrTo.setRequired(true);
83       complexStructure.add(attrTo);
84       attributes.add(attrTo);
85       complexStructure.add(refCondition);
86       complexStructure.add(refDescription);
87       complexStructure.add(refExtendedAttributes);
88    }
89
90    public void setRoutingType (String JavaDoc rType) {
91       routingType=rType;
92    }
93
94    public String JavaDoc getRoutingType () {
95       return routingType;
96    }
97
98    /**
99     * Returns the map of break points of transition graph object. The map
100     * keys are point indexes (the position of point on transition graph object),
101     * and the values are Point objects.
102     */

103    public Map getBreakPoints () {
104       return ordNoToPoint;
105    }
106
107    /**
108     * Sets the map of break points of transition graph object. The map
109     * keys are point indexes (the position of point on transition graph object),
110     * and the values are Point objects.
111     */

112    public void setBreakPoints (Map breakPoints) {
113       ordNoToPoint=breakPoints;
114    }
115
116    /*
117     * Sets the source element for transition.
118     *
119     * @param from The source element for the transition.
120     */

121    public void setFrom (XMLElement from) {
122       this.from=(XMLComplexElement)from;
123       attrFrom.setValue(this.from.get("Id").toValue());
124    }
125
126    /*
127     * Sets the target element for transition.
128     *
129     * @param to The target element for the transition.
130     */

131    public void setTo (XMLElement to) {
132       this.to=(XMLComplexElement)to;
133       attrTo.setValue(this.to.get("Id").toValue());
134    }
135
136    /*
137     * Gets the source element for transition.
138     *
139     * @return The source element for the transition.
140     */

141    public XMLComplexElement getFrom () {
142       return from;
143    }
144
145    /*
146     * Gets the target element for transition.
147     *
148     * @return The target element for the transition.
149     */

150    public XMLComplexElement getTo () {
151       return to;
152    }
153
154    /**
155     * Prepares the panel with all editable (and non-editable)
156     * elements which defines a transition.
157     *
158     * @return XMLPanel to be shown.
159     */

160    public XMLPanel getPanel () {
161       try {
162          XMLElement f=new XMLAttribute("From");
163          f.setValue(from.toString());
164          f.setReadOnly(true);
165          XMLElement t=new XMLAttribute("To");
166          t.setValue(to.toString());
167          t.setReadOnly(true);
168          clonedEAs=(ExtendedAttributes)refExtendedAttributes.clone();
169          return new XMLGroupPanel(this,
170                                   new XMLElement[] {
171                   attrId,
172                      attrName,
173                      f,
174                      t,
175                      refCondition,
176                      refDescription,
177                      clonedEAs
178                },toLabel());
179       } catch (Exception JavaDoc ex) {
180          XMLElement el=new XMLElement("ErrorMessage") {
181             public XMLPanel getPanel() {
182                setReadOnly(true);
183                value=XMLUtil.getLanguageDependentString("ErrorGraphObjectIsNotDefined");
184                return new XMLMultiLineTextPanel(this);
185             }
186          };
187          return el.getPanel();
188       }
189
190    }
191
192    /**
193     * Overrides super-class method to retreive the name of
194     * target activity for this transition.
195     * This is used when displaying instance of this class
196     * within combo box at one of Activities tab panel.
197     *
198     * @return The name of target activity for transition.
199     */

200    public String JavaDoc toString () {
201       if (to!=null) {
202          return to.toString();
203       }
204       else {
205          return "";
206       }
207    }
208
209    /**
210     * Indicates wether the condition for transition is
211     * defined or not.
212     *
213     * @return <tt>true</tt> if this transition has condition
214     * that needs to be accomplished before the control
215     * is given to next activity, <tt>false</tt> otherwise.
216     */

217    public boolean hasCondition () {
218       return refCondition.toString().trim().length()>0;
219    }
220
221    /**
222     * Gets the tooltip for transition. The tooltip consists of
223     * property names and values.
224     *
225     * @return The tooltip to be displayed when user holds the
226     * mouse above transition's graph object.
227     */

228    public String JavaDoc getTooltip () {
229       XMLElement f=new XMLElement("From");
230       //f.setLabelName(from.get(nm).toValue().toString());
231
f.setValue(from.toString());
232       XMLElement t=new XMLElement("To");
233       //t.setLabelName(to.get(nm).toValue().toString());
234
t.setValue(to.toString());
235
236       String JavaDoc cType=refCondition.get("Type").toString();
237       XMLElement ct=new XMLElement("Condition");
238       ct.setLabelName(ct.toLabel()+" - "+XMLUtil.getLanguageDependentString("TypeKey"));
239       ct.setValue(cType);
240
241       String JavaDoc cExpr=refCondition.toString();
242       XMLElement ce=new XMLElement("Condition");
243       ce.setLabelName(ce.toLabel()+" - "+XMLUtil.getLanguageDependentString("XpressionKey"));
244       ce.setValue(cExpr);
245
246       return XMLUtil.makeTooltip(new XMLElement[] {attrId,attrName, f, t,
247                   ct, ce, refDescription});
248    }
249
250    /**
251     * Must be called after importing from XML file to read the break-points
252     * from the extended attributes.
253     */

254    public void afterImporting () {
255       // set from and to activities
256
Activities acts=(Activities)getCollection().getOwner().get("Activities");
257       String JavaDoc actFromId=attrFrom.toValue().toString();
258       String JavaDoc actToId=attrTo.toValue().toString();
259       this.from=acts.getActivity(actFromId);
260       this.to=acts.getActivity(actToId);
261
262       ordNoToPoint=new Hashtable();
263       routingType=null;
264       Set easToRemove=new HashSet();
265       if (refExtendedAttributes.size()>0) {
266          ExtendedAttribute ea;
267          Iterator it=refExtendedAttributes.toCollection().iterator();
268          Point p;
269          String JavaDoc[] pPos;
270          int i=1;
271          while (it.hasNext()) {
272             ea=(ExtendedAttribute)it.next();
273             if (ea.get("Name").toValue().toString().equals("BreakPoint")) {
274                pPos=XMLUtil.
275                   tokenize(ea.get("Value").toValue().toString(),";");
276                if (pPos==null || pPos.length!=3) {
277                   continue;
278                }
279                try {
280                   p=new Point(Integer.parseInt(pPos[0]),Integer.parseInt(pPos[1]));
281                   int index;
282                   try {
283                      index=Integer.parseInt(pPos[2]);
284                   } catch (Exception JavaDoc exInner) {
285                      index=i;
286                   }
287                   ordNoToPoint.put(new Integer JavaDoc(index),p);
288                   easToRemove.add(ea);
289                } catch (Exception JavaDoc ex) {}
290                i++;
291             }
292             if (ea.get("Name").toValue().toString().equals(ROUTING_TYPE) && routingType==null) {
293                routingType=ea.get("Value").toValue().toString();
294                easToRemove.add(ea);
295             }
296          }
297       }
298       //removing internally used ext. attribs - otherwise, it would
299
// be shown within ea list
300
refExtendedAttributes.toCollection().removeAll(easToRemove);
301
302    }
303
304    /**
305     * Overrides super-class method to realize this class specific
306     * writting to XML file.
307     *
308     * @return The string for a WfMC DTD Transition element tag.
309     */

310    public void toXML (Node parent) throws DOMException {
311       // update attrFrom and attrTo values
312
try {
313          attrFrom.setValue(this.from.get("Id").toValue());
314       } catch (Exception JavaDoc ex) {}
315       try {
316          attrTo.setValue(this.to.get("Id").toValue());
317       } catch (Exception JavaDoc ex) {}
318
319       // remove all internally used ext. attribs
320
Set easToRemove=new HashSet();
321
322       ExtendedAttribute ea;
323       if (routingType==null || routingType.equals(NO_ROUTING)) {
324          int noOfPoints=ordNoToPoint.size();
325          Point p;
326          String JavaDoc pPos;
327
328          for (int i=1; i<=noOfPoints; i++) {
329             p=(Point)ordNoToPoint.get(new Integer JavaDoc(i));
330             pPos=String.valueOf(p.x)+";"+String.valueOf(p.y)+";"+String.valueOf(i);
331             ea=new ExtendedAttribute(refExtendedAttributes);
332             ((ArrayList)refExtendedAttributes.toCollection()).add(0,ea);
333             ea.set("Name","BreakPoint");
334             ea.set("Value",pPos);
335             easToRemove.add(ea);
336          }
337       }
338
339       if (routingType!=null) {
340          ea=new ExtendedAttribute(refExtendedAttributes);
341          ((ArrayList)refExtendedAttributes.toCollection()).add(0,ea);
342          ea.set("Name",ROUTING_TYPE);
343          ea.set("Value",routingType);
344          easToRemove.add(ea);
345       }
346       super.toXML(parent);
347
348       //removing internally used ext. attribs - otherwise, it would be duplicated
349
refExtendedAttributes.toCollection().removeAll(easToRemove);
350
351    }
352
353
354    /**
355     * Used to create exact copy of instance of this class.
356     * The newly created instance will have all the properties
357     * same as the copied one.
358     *
359     * @return The newly created instance of this class.
360     */

361    public Object JavaDoc clone () {
362       Transition t=(Transition)super.clone();
363       t.attrId.setValue(myCollection.generateID()); //HM: enable Transition-copy/paste
364

365       t.attrName=(XMLAttribute)this.attrName.clone();
366       t.attrFrom=(XMLAttribute)this.attrFrom.clone();
367       t.attrTo=(XMLAttribute)this.attrTo.clone();
368       t.refCondition=(Condition)this.refCondition.clone();
369       t.refDescription=(Description)this.refDescription.clone();
370       //t.refExtendedAttributes=(ExtendedAttributes)this.refExtendedAttributes.clone();
371

372       t.from=this.from;
373       t.to=this.to;
374       t.clonedEAs=null;
375
376       t.fillStructure();
377
378       return t;
379    }
380
381    /**
382     * Checks if an ID entered by the user is unique.
383     */

384    public boolean isIDUniqueAndValid (XMLPanel groupPanel) {
385       XMLTextPanel tp=(XMLTextPanel)((XMLGroupPanel)groupPanel).getPanel(0);
386       String JavaDoc IDToCheck=tp.getText();
387       // if there is an element with given ID, return false
388
Transition tr=getOwnerProcess().getTransition(IDToCheck);
389       boolean isOK=true;
390       String JavaDoc message=null;
391       String JavaDoc dialogTitle=null;
392       if (tr!=null && tr!=this) {
393          message=XMLUtil.getLanguageDependentString("ErrorIDMustBeUnique");
394          dialogTitle=XMLUtil.getLanguageDependentString("DialogIDIsNotUnique");
395          isOK=false;
396       } else if (!XMLCollection.isIdValid(IDToCheck)) {
397          message=XMLUtil.getLanguageDependentString("ErrorIDMustBeValid");
398          dialogTitle=XMLUtil.getLanguageDependentString("DialogIDIsNotValid");
399          isOK=false;
400       }
401       if (!isOK) {
402          XMLPanel.errorMessage(groupPanel.getDialog(),dialogTitle,"",message);
403          ((JTextField)tp.getComponent(2)).requestFocus();
404       }
405       return isOK;
406    }
407
408    /**
409     * This method is called only if user doesn't press Cancel button
410     * within the dialog for editing Transition properties, so
411     * the changes to the real collection of Transition are applied here.
412     * @param groupPanel The panel for editing parameters.
413     * @return always returns <tt>true</tt>.
414     */

415    public boolean isValidEnter (XMLPanel groupPanel) {
416       if (clonedEAs!=null) {
417          complexStructure.remove(refExtendedAttributes);
418          refExtendedAttributes=clonedEAs;
419          complexStructure.add(6,refExtendedAttributes);
420       }
421       return true;
422    }
423
424    public Package JavaDoc getPackage () {
425       return myPackage;
426    }
427
428    public WorkflowProcess getOwnerProcess () {
429       Object JavaDoc own=getCollection().getOwner();
430       if (own instanceof ActivitySet) {
431          return ((ActivitySet)own).getOwnerProcess();
432       } else {
433          return (WorkflowProcess)own;
434       }
435    }
436
437    /**
438     * Used to set the collection that transition belongs to. The transition can
439     * change its collection when it is beeing copied from the process to
440     * the block or vice-versa.
441     */

442    protected void setCollection (Transitions newCollection) {
443       this.myCollection=newCollection;
444    }
445
446 }
447
Popular Tags