KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > PackageGraph


1 /* PEGraph.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;
12
13 import org.enhydra.jawe.actions.*;
14 import org.enhydra.jawe.graph.*;
15 import org.enhydra.jawe.xml.*;
16 import org.enhydra.jawe.xml.elements.WorkflowProcess;
17
18 import org.jgraph.*;
19 import org.jgraph.graph.*;
20 import org.jgraph.plaf.basic.*;
21 import java.awt.*;
22 import java.awt.print.*;
23 import java.awt.event.*;
24 import java.util.*;
25 import java.io.*;
26 import javax.swing.*;
27 import javax.swing.border.*;
28 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
29 import org.enhydra.jawe.PackageEditor;
30
31 /**
32  * PackageEditor implementation of JGraph.
33  */

34 public class PackageGraph extends AbstractGraph {
35
36    private static final int processWidth=JaWEConfig.getInstance().getProcessWidth();
37    private static final int processHeight=JaWEConfig.getInstance().getProcessHeight();
38    /**
39     * Constructs package graph based on a given model.
40     */

41    public PackageGraph(GraphModel model,PackageEditor pe) {
42       super(model,pe);
43    }
44
45    public PackageGraph(GraphModel model,GraphLayoutCache view) {
46       super(model,view);
47    }
48
49    public org.enhydra.jawe.xml.elements.Package getXMLPackage () {
50       return (org.enhydra.jawe.xml.elements.Package)xmlObject;
51    }
52
53    public void setPropertyObject (XMLComplexElement ce) {
54       xmlObject=ce;
55    }
56
57    /**
58     * Returns the point where will be inserted the next process.
59     */

60    public Point getNextInsertionPoint() {
61       int hmp;
62       try {
63          hmp=JaWEGraphModel.getAllActivitiesInModel(graphModel).size();
64       } catch (Exception JavaDoc ex) {
65          hmp=0;
66       }
67
68       return new Point((int)(JaWEConstants.PROCESS_OFFSET+
69                                 processWidth*JaWEConstants.PROCESS_DISTANCE_COEFF*
70                                 (hmp%JaWEConstants.HOW_MANY_PROCESSES_IN_ONE_ROW)),
71                           (int)(JaWEConstants.PROCESS_OFFSET+
72                                    processHeight*JaWEConstants.PROCESS_DISTANCE_COEFF*
73                                    ((int)hmp/JaWEConstants.HOW_MANY_PROCESSES_IN_ONE_ROW)));
74    }
75
76    public void createWorkflowGraph (Window pFrame) {
77       org.enhydra.jawe.xml.elements.Package p=
78          (org.enhydra.jawe.xml.elements.Package)xmlObject;
79       org.enhydra.jawe.xml.elements.WorkflowProcesses wps=
80          (org.enhydra.jawe.xml.elements.WorkflowProcesses)
81          p.get("WorkflowProcesses");
82       Iterator it=wps.toCollection().iterator();
83
84       while (it.hasNext()) {
85          org.enhydra.jawe.xml.elements.WorkflowProcess wp=
86             (org.enhydra.jawe.xml.elements.WorkflowProcess)
87             it.next();
88          createWorkflowObject(pFrame,wp);
89          // decrement ID of WPs
90
wps.decrementID();
91       }
92    }
93
94    public void createWorkflowObject (Window pFrame,
95                                      org.enhydra.jawe.xml.elements.WorkflowProcess wp) {
96       org.enhydra.jawe.graph.Process pr=workflowManager.
97          insertProcess(getNextInsertionPoint(),false);
98       pr.setUserObject(wp);
99       pr.createWorkflowGraph(pFrame);
100       paintImmediately(getBounds());
101       ((PackageEditor)editor).putProcessObjectMapping(wp,pr);
102    }
103
104    /**
105     * Gets a tooltip text for element.
106     */

107    public String JavaDoc getTooltip () {
108       return "";
109    }
110
111    /**
112     * Override Superclass Method to Return Custom vertexView.
113     */

114    protected VertexView createVertexView(JGraph graph,CellMapper cm,Object JavaDoc cell) {//HM, JGraph3.4.1
115
if (cell instanceof org.enhydra.jawe.graph.Process) {
116          return new ProcessView(cell,this,cm);
117       } else {
118          //return super.createVertexView(cell,cm);
119
//return super.createVertexView(this,cm,cell);//HM, JGraph3.4.1
120
return super.createVertexView(graph,cm,cell);//HM, JGraph3.4.1
121
}
122    }
123
124    /**
125     * Overrides Superclass method.
126     */

127    public String JavaDoc convertValueToString(Object JavaDoc value) {
128       if (value instanceof CellView) {
129          value=((CellView)value).getCell();
130       }
131       if (value instanceof org.enhydra.jawe.graph.Process &&
132              ((DefaultMutableTreeNode JavaDoc)value).getUserObject() != null) {
133          return ((DefaultMutableTreeNode JavaDoc)value).getUserObject().toString();
134       } else if (value != null) {
135          return value.toString();
136       }
137       return null;
138    }
139
140    /**
141     * Overrides <code>JComponent</code>'s <code>getToolTipText</code>
142     * method in order to allow the graph controller to create a tooltip
143     * for the topmost cell under the mousepointer. This differs from JTree
144     * where the renderers tooltip is used.
145     * <p>
146     * NOTE: For <code>JGraph</code> to properly display tooltips of its
147     * renderers, <code>JGraph</code> must be a registered component with the
148     * <code>ToolTipManager</code>. This can be done by invoking
149     * <code>ToolTipManager.sharedInstance().registerComponent(graph)</code>.
150     * This is not done automatically!
151     * @param event the <code>MouseEvent</code> that initiated the
152     * <code>ToolTip</code> display
153     * @return a string containing the tooltip or <code>null</code>
154     * if <code>event</code> is null
155     */

156    public String JavaDoc getToolTipText(MouseEvent event) {
157       if(event != null) {
158          Object JavaDoc cell=getFirstCellForLocation(event.getX(),event.getY());
159          if (cell != null) {
160             String JavaDoc s=convertValueToString(cell);
161             if (cell instanceof WorkflowElement) {
162                s=((WorkflowElement)cell).getTooltip();
163             }
164             return s;
165          }
166       }
167       return null;
168    }
169
170    public boolean validateAgainsXPDLSchema () {
171       Save.updateExtendedAttributesForWorkflowProcesses();
172       PackageValidator pv=new PackageValidator(getXMLPackage(),true,true,false,true,
173                                                JaWEConfig.getInstance().getAllowUndefinedStartActivity(),
174                                                JaWEConfig.getInstance().getAllowUndefinedEndActivity(),
175                                                JaWEConfig.getInstance().getEncoding());
176       boolean isValid=pv.validateAgainstXPDLSchema();
177       xpdlSchemaValidationErrors=pv.getXPDLSchemaValidationErrors();
178       if (!isValid && xpdlSchemaValidationErrors.size()>0) {
179          basicXpdlSchemaValidationError=ResourceManager.
180             getLanguageDependentString("MessageThereAreSomeSchemaValidationErrors");
181       } else {
182          basicXpdlSchemaValidationError=null;
183       }
184       return isValid;
185    }
186
187    public boolean checkConnections (boolean fullCheck) {
188       updateXMLObjectsBeforeChecking();
189       PackageValidator pv=new PackageValidator(getXMLPackage(),true,true, false,true,
190                                                JaWEConfig.getInstance().getAllowUndefinedStartActivity(),
191                                                JaWEConfig.getInstance().getAllowUndefinedEndActivity(),
192                                                JaWEConfig.getInstance().getEncoding());
193       boolean areGraphsWellConnected=pv.checkGraphConnections(fullCheck);
194       basicGraphConnectionError=pv.getBasicGraphConnectionError(getXMLPackage());
195       graphConnectionErrors=pv.getGraphsConnectionErrors(getXMLPackage());
196       if (JaWEConfig.getInstance().getUseBubblesStatus() && (fullCheck || areGraphsWellConnected)) {
197          areGraphsWellConnected=checkStartAndEndsConnections(fullCheck) && areGraphsWellConnected;
198       }
199       return areGraphsWellConnected;
200    }
201
202    protected boolean checkStartAndEndsConnections (boolean fullCheck) {
203       boolean wellConnected=true;
204       Set activities=JaWEGraphModel.getAllActivitiesInModel(graphModel);
205       if (activities!=null) {
206          Iterator it=activities.iterator();
207          while(it.hasNext() && (fullCheck || wellConnected)) {
208             Activity act=(Activity)it.next();
209             ProcessEditor pe=act.getImplementationEditor();
210             if (pe!=null) {
211                WorkflowProcess wp=(WorkflowProcess)act.getUserObject();
212                ProcessGraph pg=(ProcessGraph)pe.getGraph();
213                if (pg.getImproperlyConnectedStarts(false).size()>0 ||
214                    pg.getImproperlyConnectedEnds(false).size()>0) {
215                   wellConnected=false;
216                   basicGraphConnectionError=ResourceManager.
217                      getLanguageDependentString("InformationOneOrMoreProcessesHaveImproperlyConnectedElements");
218                   if (!fullCheck){
219                      break;
220                   }
221                   if (graphConnectionErrors==null) {
222                      graphConnectionErrors=new HashMap();
223                   }
224
225                   graphConnectionErrors.put(wp,basicGraphConnectionError);
226                }
227                // find all block activities
228
Set blockActs=workflowManager.getBlockActivities(true);
229                Iterator itBas=blockActs.iterator();
230                while (itBas.hasNext()) {
231                   BlockActivity ba=(BlockActivity)itBas.next();
232                   ProcessEditor bwe=ba.getImplementationEditor();
233                   if (bwe!=null) {
234                      ProcessGraph bag=(ProcessGraph)bwe.getGraph();
235                      if (bag.getImproperlyConnectedStarts(false).size()>0 ||
236                          bag.getImproperlyConnectedEnds(false).size()>0) {
237                         wellConnected=false;
238                         basicGraphConnectionError=ResourceManager.
239                            getLanguageDependentString("InformationOneOrMoreProcessesHaveImproperlyConnectedElements");
240                         if (!fullCheck){
241                            break;
242                         }
243                         graphConnectionErrors.put(wp,basicGraphConnectionError);
244                      }
245
246                   }
247                }
248             }
249          }
250       }
251       return wellConnected;
252    }
253
254    protected void updateXMLObjectsBeforeChecking () {
255       // first update extended attributes for start and end for all processes
256
Set activities=JaWEGraphModel.getAllActivitiesInModel(graphModel);
257       if (activities!=null) {
258          Iterator it=activities.iterator();
259          while(it.hasNext()) {
260             Activity act=(Activity)it.next();
261             ProcessEditor pe=act.getImplementationEditor();
262             if (pe!=null) {
263                WorkflowProcess wp=(WorkflowProcess)act.getUserObject();
264                wp.setStartDescriptions(Utils.getStartDescriptions(pe));
265                wp.setEndDescriptions(Utils.getEndDescriptions(pe));
266                // find all block activities
267
Set blockActs=workflowManager.getBlockActivities(true);
268                Iterator itBas=blockActs.iterator();
269                while (itBas.hasNext()) {
270                   BlockActivity ba=(BlockActivity)itBas.next();
271                   ProcessEditor bwe=ba.getImplementationEditor();
272                   org.enhydra.jawe.xml.elements.Activity bap=
273                      (org.enhydra.jawe.xml.elements.Activity)
274                      ba.getUserObject();
275                   bap.setStartDescriptions(Utils.getStartDescriptions(bwe));
276                   bap.setEndDescriptions(Utils.getEndDescriptions(bwe));
277                }
278             }
279          }
280       }
281    }
282
283    /**
284     * Checks the graph for conformance.
285     * @param fullCheck true if the full check should be performed, and false
286     * if checking should stop on first conformance error
287     * @return true if the graph is conformant
288     */

289    public boolean checkGraphConformance(boolean fullCheck) {
290       PackageValidator pv=new PackageValidator(getXMLPackage(),true,true, false,true,
291                                                JaWEConfig.getInstance().getAllowUndefinedStartActivity(),
292                                                JaWEConfig.getInstance().getAllowUndefinedEndActivity(),
293                                                JaWEConfig.getInstance().getEncoding());
294       boolean areGraphsConformant=pv.checkGraphConformance(fullCheck);
295       basicGraphConformanceErrors=pv.getBasicGraphConformanceErrors(getXMLPackage());
296       graphConformanceErrors=pv.getGraphConformanceErrors(getXMLPackage());
297       return areGraphsConformant;
298    }
299
300    public boolean checkLogic (boolean fullCheck) {
301       PackageValidator pv=new PackageValidator(getXMLPackage(),true,true,false,true,
302                                                JaWEConfig.getInstance().getAllowUndefinedStartActivity(),
303                                                JaWEConfig.getInstance().getAllowUndefinedEndActivity(),
304                                                JaWEConfig.getInstance().getEncoding());
305       boolean isLogical=pv.checkPackage(fullCheck);
306       basicLogicError=pv.getBasicLogicError(getXMLPackage());
307       logicErrors=pv.getLogicErrors(getXMLPackage());
308       return isLogical;
309    }
310
311    /**
312     * Reacts upon the XML element change by setting isModified flag
313     * of PackageEditor if needed.
314     */

315    public void xmlElementChanged (XMLElement el) {
316       if (el instanceof org.enhydra.jawe.xml.elements.Activity) {
317          editor.getStatusBar().updateMessage();
318       }
319    }
320
321 }
322
Popular Tags