KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* SubFlow.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 org.enhydra.jawe.xml.elements.specialpanels.*;
16
17 import java.util.*;
18 import org.w3c.dom.*;
19
20 /**
21  * Represents a WfMC DTD element that has the similar name.
22  *
23  * @see XML
24  */

25 public class SubFlow extends XMLComplexElement {
26    private ActualParameters refActualParameters; // min=0
27
private XMLAttribute attrId=new XMLAttribute("Id"); // required
28
private XMLAttribute attrExecution=new XMLAttribute("Execution",
29                                                        new String JavaDoc[] {
30             "",
31                "ASYNCHR",
32                "SYNCHR"
33          },2);
34
35    private transient Package JavaDoc myPackage=null;
36    private transient WorkflowProcess myWP=null;
37    private transient Activity myAct=null;
38
39    /** Choice of possible workflows. */
40    private transient XMLComplexChoice helperElement;
41
42    /** Enables canceling of changes to the actual param. collection. */
43    private ActualParameters clonedAPs;
44
45    /**
46     * Creates a new instance of the class.
47     *
48     * @param act The activity that holds this element.
49     */

50    public SubFlow (Activity act) {
51       super();
52
53       myAct=act;
54       if (myAct!=null) {
55          myWP=myAct.getOwnerProcess();
56          myPackage=myWP.getPackage();
57       }
58
59       helperElement=new XMLComplexChoice("WorkflowProcess",null,0) {
60          public XMLPanel getPanel () {
61             WorkflowProcesses wps=null;
62             if (myPackage!=null) {
63                wps=(WorkflowProcesses)myPackage.get("WorkflowProcesses");
64                ArrayList ws=new ArrayList();
65                if (!(this.value instanceof WorkflowProcess) && this.value!=null) {
66                   ws.add(this.value);
67                }
68                //ws.add(new String(XMLUtil.getLanguageDependentString("NewLabel")));
69
ws.addAll(wps.getChoosable());
70                //ws.remove(myWP);
71
choices=ws.toArray();
72             }
73             else {
74                choices=null;
75             }
76
77             if (this.getChoosen()==null && choices!=null) {
78                this.setValue(choices[0]);
79             }
80             return new XMLFormalParametersRelatedComboPanel(this,choices,true);
81          }
82
83          public void setValue (Object JavaDoc v) {
84             super.setValue(v);
85             try {
86                Object JavaDoc ch=this.getChoosen();
87                if (ch instanceof XMLCollectionElement) {
88                   attrId.setValue(((XMLCollectionElement)ch).getID());
89                } else {
90                   if (ch !=null) {
91                      attrId.setValue(ch.toString());
92                   }
93                }
94             } catch (Exception JavaDoc ex) {}
95          }
96
97       };
98       helperElement.setRequired(true);
99
100       refActualParameters=new ActualParameters(myWP,this);
101
102       attrId.setValue("-1");
103
104       fillStructure ();
105    }
106
107    /**
108     * Defines the super-class method. Read the explanation for
109     * this method within XMLComplexElement class.
110     */

111    protected void fillStructure () {
112       attrId.setRequired(true);
113       attrId.setReadOnly(true);
114
115       complexStructure.add(attrId);
116       attributes.add(attrId);
117       helperElement.setRequired(true);
118       complexStructure.add(helperElement);
119       complexStructure.add(attrExecution);
120       attributes.add(attrExecution);
121       complexStructure.add(refActualParameters);
122    }
123
124    public Package JavaDoc getPackage () {
125       return myPackage;
126    }
127
128    /**
129     * Overrides super-class method to realize this class specific
130     * writting to XML file.
131     *
132     * @return The string for a WfMC DTD Tool element tag.
133     */

134    public void toXML (Node parent) throws DOMException {
135       // When saving to file, setting ID to referenced workflow process ID
136
// If workflow process is not referenced, value is already set to -1
137
try {
138          Object JavaDoc ch=helperElement.getChoosen();
139          if (ch instanceof XMLCollectionElement) {
140             attrId.setValue(((XMLCollectionElement)ch).getID());
141          } else {
142             attrId.setValue(ch);
143          }
144       } catch (Exception JavaDoc ex) {}
145       complexStructure.remove(1);
146       super.toXML(parent);
147       complexStructure.add(1,helperElement);
148    }
149
150    public void fromXML(Node node) {
151       attrExecution.setValue("");
152       super.fromXML(node);
153    }
154
155    /**
156     * Called after importing of XML file to set the proper WorkflowProcess
157     * object for subflow reference, and proper DataField objects for
158     * ActualParameters references, according on it's IDs read from XML.
159     */

160    protected void afterImporting () {
161       // setting choosen workflow process
162
String JavaDoc wantedWpID=attrId.toValue().toString();
163       WorkflowProcess wp=((WorkflowProcesses)myPackage.get("WorkflowProcesses")).
164          getWorkflowProcess(wantedWpID);
165       if (wp!=null) {
166          helperElement.setValue(wp);
167       } else {
168          helperElement.setValue(wantedWpID);
169       }
170       // setting choosen data fields
171
Iterator it=refActualParameters.toCollection().iterator();
172       while (it.hasNext()) {
173          ActualParameter ap=(ActualParameter)it.next();
174          ap.afterImporting();
175       }
176    }
177
178
179    /**
180     * Uses a trick to set the cloned instance of actual parameters to
181     * be edited, so that canceling of changes to the actual parameter
182     * collection can be handled - the real instance of actual parameters
183     * is set to the cloned instance within the isValidData method.
184     */

185    public XMLPanel getPanel () {
186       clonedAPs=(ActualParameters)refActualParameters.clone();
187       XMLPanel p=new XMLGroupPanel(this,new XMLElement[] {helperElement,attrExecution,clonedAPs},toLabel());
188       return p;
189    }
190
191    /**
192     * Used to create exact copy of instance of this class.
193     * The newly created instance will have all the properties
194     * same as the copied one.
195     *
196     * @return The newly created instance of this class.
197     */

198    public Object JavaDoc clone () {
199       SubFlow s=(SubFlow)super.clone();
200
201       s.attrId=(XMLAttribute)this.attrId.clone();
202       s.attrExecution=(XMLAttribute)this.attrExecution.clone();
203       s.refActualParameters=(ActualParameters)this.refActualParameters.clone();
204
205       s.helperElement=new XMLComplexChoice("WorkflowProcess",null,0) {
206          public XMLPanel getPanel () {
207             WorkflowProcesses wps=null;
208             if (myPackage!=null) {
209                wps=(WorkflowProcesses)myPackage.get("WorkflowProcesses");
210                ArrayList ws=new ArrayList();
211                if (!(this.value instanceof WorkflowProcess) && this.value!=null) {
212                   ws.add(this.value);
213                }
214                //ws.add(new String(XMLUtil.getLanguageDependentString("NewLabel")));
215
ws.addAll(wps.getChoosable());
216                ws.remove(myWP);
217                choices=ws.toArray();
218             }
219             else {
220                choices=null;
221             }
222
223             if (this.getChoosen()==null && choices!=null) {
224                this.setValue(choices[0]);
225             }
226             return new XMLFormalParametersRelatedComboPanel(this,choices,true);
227          }
228
229          public void setValue (Object JavaDoc v) {
230             super.setValue(v);
231             try {
232                Object JavaDoc ch=this.getChoosen();
233                if (ch instanceof XMLCollectionElement) {
234                   attrId.setValue(((XMLCollectionElement)ch).getID());
235                } else {
236                   if (ch !=null) {
237                      attrId.setValue(ch.toString());
238                   }
239                }
240             } catch (Exception JavaDoc ex) {}
241          }
242
243       };
244       s.helperElement.setValue(helperElement.getChoosen());
245       s.helperElement.setRequired(true);
246       s.clonedAPs=null;
247
248       s.myPackage=this.myPackage;
249       s.myWP=this.myWP;
250
251       s.fillStructure ();
252
253       return s;
254    }
255
256    /**
257     * Checks if actual and formal params are matched by type and number.
258     * This method is called only if user doesn't press Cancel button
259     * within the dialog for editing Tool properties, so the changes to
260     * the real collection of actual parameters are applied here.
261     * @param groupPanel The panel for editing parameters.
262     * @return <tt>true</tt> if actual and formal parameter types and number
263     * are matching, <tt>false</tt> otherwise.
264     */

265    public boolean isValidEnter (XMLPanel groupPanel) {
266       boolean isValidEnter=super.isValidEnter(groupPanel);
267       XMLComboPanel cp=(XMLComboPanel)((XMLGroupPanel)groupPanel).getPanel(0);
268       WorkflowProcess wp=null;
269       Object JavaDoc remoteSubflow=null;
270       try {
271          Object JavaDoc sel=cp.getSelectedItem();
272          if(sel instanceof WorkflowProcess) {
273             wp=(WorkflowProcess)cp.getSelectedItem();
274          } else {
275             remoteSubflow=sel;
276          }
277       } catch (Exception JavaDoc ex) {
278       }
279
280       String JavaDoc message=XMLUtil.getLanguageDependentString("ErrorFormalAndActualParameterTypesMustMatch");
281       String JavaDoc dialogTitle=XMLUtil.getLanguageDependentString("DialogFormalAndActualParameterTypesDoNotMatch");
282       String JavaDoc elementTitle=toLabel()+": ";
283
284       if (wp==null && remoteSubflow==null){
285          if (clonedAPs.size()>0) {
286             message=XMLUtil.getLanguageDependentString("ErrorFormalAndActualParameterNoMustMatch");
287             dialogTitle=XMLUtil.getLanguageDependentString("DialogFormalAndActualParameterNoDoNotMatch");
288             XMLPanel.errorMessage(groupPanel.getDialog(),dialogTitle,elementTitle,message);
289             cp.getComponent(2).requestFocus();
290             return false;
291          } else {
292             return true;
293          }
294
295       }
296
297       if (wp!=null) {
298          FormalParameters fps=(FormalParameters)wp.get("FormalParameters");
299
300          int am=XMLUtil.checkParameterMatching(fps,clonedAPs);
301          if (am==1) {
302             message=XMLUtil.getLanguageDependentString("ErrorFormalAndActualParameterNoMustMatch");
303             dialogTitle=XMLUtil.getLanguageDependentString("DialogFormalAndActualParameterNoDoNotMatch");
304             isValidEnter=false;
305          } else if (am==2) {
306             isValidEnter=false;
307          }
308       }
309
310       if (!isValidEnter) {
311          XMLPanel.errorMessage(groupPanel.getDialog(),dialogTitle,elementTitle,message);
312          cp.getComponent(2).requestFocus();
313       } else {
314          // if the check is passed, replace actual parameters with the one entered
315
if (clonedAPs!=null) {
316             complexStructure.remove(refActualParameters);
317             refActualParameters=clonedAPs;
318             complexStructure.add(3,refActualParameters);
319          }
320       }
321
322       return isValidEnter;
323    }
324
325 }
326
Popular Tags