KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Tool.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 public class Tool extends XMLCollectionElement {
24    /**
25    * Defined application at the workflow process level
26    * where instance of object where this instance of
27    * element belongs. Applications defined at the Package
28    * level are also included.
29    */

30    private transient Applications definedApplications=null;
31
32    private ActualParameters refActualParameters; // min=0
33
private Description refDescription=new Description(); // min=0
34
private ExtendedAttributes refExtendedAttributes=new ExtendedAttributes(this); // min=0
35

36    /** Holds reference to choosen application for Tool instance. */
37    private XMLComplexChoice helperElement;
38
39    /** Enables canceling of changes to the actual param. collection. */
40    private ActualParameters clonedAPs;
41
42    /** Enables canceling of changes to the extended attributes collection. */
43    private ExtendedAttributes clonedEAs;
44
45    private XMLAttribute attrType=new XMLAttribute("Type",
46       new String JavaDoc[] {
47          "",
48          "APPLICATION",
49          "PROCEDURE"
50       },1);
51
52
53    /**
54    * Creates a new instance of the class.
55    *
56    * @param tls The reference to collection of tools where
57    * this instance will be put into.
58
59    * @param wp The workflow process that holds activity which
60    * member is this class instance.
61    */

62    public Tool (Tools tls,WorkflowProcess wp){
63       super(tls);
64       try {
65          definedApplications=(Applications)wp.get("Applications");
66       }
67       catch (NullPointerException JavaDoc npe) {
68          //
69
}
70
71       helperElement=new XMLComplexChoice("Application",null,0) {
72          public XMLPanel getPanel () {
73             if (definedApplications!=null) {
74                choices=definedApplications.getChoosable().toArray();
75             }
76             else {
77                choices=null;
78             }
79             return new XMLFormalParametersRelatedComboButtonPanel(this,definedApplications);
80          }
81       };
82       helperElement.setRequired(true);
83
84       refActualParameters=new ActualParameters(wp,this); // min=0
85

86       fillStructure();
87    }
88
89    /**
90    * Defines the super-class method. Read the explanation for
91    * this method within XMLComplexElement class.
92    */

93    protected void fillStructure () {
94       super.fillStructure();
95       setRequired(true);
96       attrId.setReadOnly(true);
97       complexStructure.add(helperElement);
98       complexStructure.add(attrType);
99       attributes.add(attrType);
100       complexStructure.add(refActualParameters);
101       complexStructure.add(refDescription);
102       complexStructure.add(refExtendedAttributes);
103    }
104
105
106    /**
107    * Overrides super-class method to realize this class specific
108    * writting to XML file.
109    *
110    * @return The string for a WfMC DTD Tool element tag.
111    */

112    public void toXML (Node parent) throws DOMException {
113       // When saving to file, setting ID to an application ID
114
// attrId allows Tool to be in DTDTablePanel, and after
115
// that returning all to the previous state
116
Object JavaDoc val=attrId.toValue();
117       try {
118          attrId.setValue(((XMLCollectionElement)helperElement.getChoosen()).
119             get("Id").toValue());
120       } catch (Exception JavaDoc ex) {}
121       complexStructure.remove(1);
122       super.toXML(parent);
123       complexStructure.add(1,helperElement);
124       attrId.setValue(val);
125    }
126
127    public void fromXML(Node node) {
128       attrType.setValue("");
129       super.fromXML(node);
130    }
131
132    /**
133    * Called after importing of XML file to set the proper Application
134    * objects for Tool references, and proper DataField objects for
135    * ActualParameters references, according on it's IDs read from XML.
136    */

137    protected void afterImporting () {
138       // setting choosen application
139
String JavaDoc wantedAppID=attrId.toValue().toString();
140       Iterator it=definedApplications.getChoosable().iterator();
141       while (it.hasNext()) {
142          Application app=(Application)it.next();
143          if (app.getID().equals(wantedAppID)) {
144             helperElement.setValue(app);
145             break;
146          }
147       }
148       // setting choosen data fields
149
it=refActualParameters.toCollection().iterator();
150       while (it.hasNext()) {
151          ActualParameter ap=(ActualParameter)it.next();
152          ap.afterImporting();
153       }
154    }
155
156
157    /**
158    * Uses a trick to set the cloned instance of actual parameters to
159    * be edited, so that canceling of changes to the actual parameter
160    * collection can be handled - the real instance of actual parameters
161    * is set to the cloned instance within the isValidData method.
162    */

163    public XMLPanel getPanel () {
164       clonedAPs=(ActualParameters)refActualParameters.clone();
165       clonedEAs=(ExtendedAttributes)refExtendedAttributes.clone();
166
167       XMLPanel p=new XMLGroupPanel(this,new XMLElement[] {helperElement,attrType,
168                clonedAPs,refDescription,clonedEAs},toLabel());
169       return p;
170    }
171
172
173    /**
174    * Used to create exact copy of instance of this class.
175    * The newly created instance will have all the properties
176    * same as the copied one.
177    *
178    * @return The newly created instance of this class.
179    */

180    public Object JavaDoc clone () {
181       Tool t=(Tool)super.clone();
182
183       t.attrType=(XMLAttribute)this.attrType.clone();
184       t.refActualParameters=(ActualParameters)this.refActualParameters.clone();
185       t.refActualParameters.setToolOrSubflow(t);
186       t.refDescription=(Description)this.refDescription.clone();
187       t.refExtendedAttributes=(ExtendedAttributes)this.refExtendedAttributes.clone();
188
189       t.definedApplications=this.definedApplications;
190
191       t.helperElement=new XMLComplexChoice("Application",null,0) {
192          public XMLPanel getPanel () {
193             if (definedApplications!=null) {
194                choices=definedApplications.getChoosable().toArray();
195             }
196             else {
197                choices=null;
198             }
199             return new XMLFormalParametersRelatedComboButtonPanel(this,definedApplications);
200          }
201       };
202       t.helperElement.setValue(helperElement.getChoosen());
203       t.helperElement.setRequired(true);
204       t.helperElement.setReadOnly(helperElement.isReadOnly());
205       t.clonedAPs=null;
206       t.clonedEAs=null;
207
208       t.fillStructure ();
209
210       return t;
211    }
212
213    /**
214     * Checks if actual and formal params are matched by type and number.
215     * This method is called only if user doesn't press Cancel button
216     * within the dialog for editing Tool properties, so the changes to
217     * the real collection of actual parameters are applied here.
218     * @param groupPanel The panel for editing parameters.
219     * @return <tt>true</tt> if actual and formal parameter types and number
220     * are matching, <tt>false</tt> otherwise.
221     */

222    public boolean isValidEnter (XMLPanel groupPanel) {
223       boolean isValidEnter=super.isValidEnter(groupPanel);
224       XMLComboButtonPanel cbp=(XMLComboButtonPanel)((XMLGroupPanel)groupPanel).getPanel(0);
225       Application app=(Application)cbp.getSelectedItem();
226
227       String JavaDoc message=XMLUtil.getLanguageDependentString("ErrorFormalAndActualParameterTypesMustMatch");
228       String JavaDoc dialogTitle=XMLUtil.getLanguageDependentString("DialogFormalAndActualParameterTypesDoNotMatch");
229       String JavaDoc elementTitle=toLabel()+": ";
230
231       if (((XMLComplexChoice)app.get("Choice")).getChoosen() instanceof FormalParameters) {
232          FormalParameters fps=(FormalParameters)((XMLComplexChoice)app.
233                get("Choice")).getChoices()[0];
234          int am=XMLUtil.checkParameterMatching(fps,clonedAPs);
235          if (am==1) {
236             message=XMLUtil.getLanguageDependentString("ErrorFormalAndActualParameterNoMustMatch");
237             dialogTitle=XMLUtil.getLanguageDependentString("DialogFormalAndActualParameterNoDoNotMatch");
238             isValidEnter=false;
239          } else if (am==2) {
240             isValidEnter=false;
241          }
242       }
243
244       if (!isValidEnter) {
245          XMLPanel.errorMessage(groupPanel.getDialog(),dialogTitle,elementTitle,message);
246          cbp.getComponent(2).requestFocus();
247       } else {
248          // if the check is passed, replace actual parameters with the one entered
249
if (clonedAPs!=null) {
250             complexStructure.remove(refActualParameters);
251             refActualParameters=clonedAPs;
252             complexStructure.add(3,refActualParameters);
253          }
254          if (clonedEAs!=null) {
255             complexStructure.remove(refExtendedAttributes);
256             refExtendedAttributes=clonedEAs;
257             complexStructure.add(5,refExtendedAttributes);
258          }
259       }
260
261       return isValidEnter;
262    }
263
264    public Package JavaDoc getPackage () {
265       return definedApplications.getPackage();
266    }
267
268 }
269
Popular Tags