KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > WorkflowGraph


1 package com.opensymphony.workflow.designer;
2
3 import java.awt.*;
4 import java.awt.datatransfer.DataFlavor JavaDoc;
5 import java.awt.datatransfer.Transferable JavaDoc;
6 import java.awt.dnd.*;
7 import java.util.List JavaDoc;
8 import java.util.Properties JavaDoc;
9 import javax.swing.*;
10
11 import com.opensymphony.workflow.designer.actions.*;
12 import com.opensymphony.workflow.designer.dnd.DragData;
13 import com.opensymphony.workflow.designer.layout.LayoutAlgorithm;
14 import com.opensymphony.workflow.designer.layout.SugiyamaLayoutAlgorithm;
15 import com.opensymphony.workflow.designer.views.*;
16 import com.opensymphony.workflow.loader.*;
17 import org.jgraph.JGraph;
18 import org.jgraph.graph.*;
19
20 public class WorkflowGraph extends JGraph implements DropTargetListener
21 {
22   private Layout layout = new Layout();
23   private Point menuLocation = new Point();
24
25   private WorkflowDescriptor descriptor;
26   private JPopupMenu genericMenu;
27   private JPopupMenu cellMenu;
28
29   private UndoManager undoManager = new UndoManager();
30   private static final Color GRID_COLOR = new Color(240, 240, 240);
31
32   public WorkflowGraph(GraphModel model, WorkflowDescriptor descriptor, Layout layout, boolean doAutoLayout)
33   {
34     super(model);
35     ToolTipManager.sharedInstance().registerComponent(this);
36     this.layout = layout;
37     setDescriptor(descriptor);
38     if(doAutoLayout)
39     {
40       autoLayout();
41     }
42     setSelectNewCells(true);
43
44     setGridEnabled(true);
45     setSizeable(true);
46     setGridColor(GRID_COLOR);
47     setGridSize(12);
48     setTolerance(2);
49     setGridVisible(true);
50     setGridMode(JGraph.LINE_GRID_MODE);
51
52     setBendable(true);
53     setMarqueeHandler(new WorkflowMarqueeHandler(this));
54     setCloneable(false);
55     setPortsVisible(true);
56
57     // one set of menu <==> one graph <==> one workflow descriptor
58
genericMenu = new JPopupMenu();
59     JMenu n = new JMenu(ResourceManager.getString("create.new"));
60     genericMenu.add(n);
61     n.add(new CreateStep(descriptor, getWorkflowGraphModel(), menuLocation));
62     // n.add(new CreateInitialAction(descriptor, getWorkflowGraphModel(), menuLocation));
63
n.add(new CreateJoin(descriptor, getWorkflowGraphModel(), menuLocation));
64     n.add(new CreateSplit(descriptor, getWorkflowGraphModel(), menuLocation));
65
66     cellMenu = new JPopupMenu();
67     cellMenu.add(new Delete(descriptor, this, menuLocation));
68     model.addUndoableEditListener(undoManager);
69     new DropTarget(this, this);
70   }
71
72   public void setDescriptor(WorkflowDescriptor descriptor)
73   {
74     if(descriptor != null)
75     {
76       this.descriptor = descriptor;
77       List JavaDoc initialActionList = descriptor.getInitialActions();
78       addInitialActions(initialActionList);
79       List JavaDoc stepsList = descriptor.getSteps();
80       for(int i = 0; i < stepsList.size(); i++)
81       {
82         StepDescriptor step = (StepDescriptor)stepsList.get(i);
83         addStepDescriptor(step);
84       }
85       List JavaDoc splitsList = descriptor.getSplits();
86       for(int i = 0; i < splitsList.size(); i++)
87
88       {
89         SplitDescriptor split = (SplitDescriptor)splitsList.get(i);
90         addSplitDescriptor(split);
91       }
92       List JavaDoc joinsList = descriptor.getJoins();
93       for(int i = 0; i < joinsList.size(); i++)
94       {
95         JoinDescriptor join = (JoinDescriptor)joinsList.get(i);
96         addJoinDescriptor(join);
97       }
98       getWorkflowGraphModel().insertResultConnections();
99     }
100   }
101
102   public Layout getGraphLayout()
103   {
104     return layout;
105   }
106
107   protected PortView createPortView(JGraph graph, CellMapper mapper, Object JavaDoc cell)
108   {
109     return new CustomPortView(cell, graph, mapper);
110   }
111
112   //protected EdgeView createEdgeView(JGraph graph, CellMapper mapper, Object cell)
113
//{
114
// return new CustomEdgeView(cell, (WorkflowGraph)graph, mapper);
115
//}
116

117   public void autoLayout()
118   {
119     if(descriptor.getSteps().size() > 0)
120     {
121       LayoutAlgorithm algo = new SugiyamaLayoutAlgorithm();
122       Properties JavaDoc p = new Properties JavaDoc();
123       p.put(SugiyamaLayoutAlgorithm.KEY_HORIZONTAL_SPACING, "110");
124       p.put(SugiyamaLayoutAlgorithm.KEY_VERTICAL_SPACING, "70");
125       algo.perform(this, true, p);
126     }
127   }
128
129   public void addInitialActions(List JavaDoc initialActionList)
130   {
131     InitialActionCell initialActionCell = new InitialActionCell(ResourceManager.getString("action.initial.step"));
132     // Create Vertex Attributes
133
if(layout != null)
134     {
135       Rectangle bounds = layout.getBounds(initialActionCell.getId(), "InitialActionCell");
136       if(bounds != null)
137       {
138         initialActionCell.getAttributes().put(GraphConstants.BOUNDS, bounds);
139       }
140       if(initialActionCell.getChildCount() == 0)
141       {
142         WorkflowPort port = new WorkflowPort();
143         initialActionCell.add(port);
144       }
145     }
146     else
147     {
148       WorkflowPort port = new WorkflowPort();
149       initialActionCell.add(port);
150
151     }
152     getWorkflowGraphModel().insertInitialActions(initialActionList, initialActionCell, null, null, null);
153   }
154
155   public void addJoinDescriptor(JoinDescriptor descriptor)
156   {
157     JoinCell join = new JoinCell(descriptor);
158     // Create Vertex Attributes
159
if(layout != null)
160     {
161       Rectangle bounds = layout.getBounds(join.getId(), "JoinCell");
162       if(bounds != null)
163         join.getAttributes().put(GraphConstants.BOUNDS, bounds);
164       if(join.getChildCount() == 0)
165       {
166         WorkflowPort port = new WorkflowPort();
167         join.add(port);
168       }
169
170     }
171     else
172     {
173       WorkflowPort port = new WorkflowPort();
174       join.add(port);
175
176     }
177     getWorkflowGraphModel().insertJoinCell(join, null, null, null);
178   }
179
180   public void addSplitDescriptor(SplitDescriptor descriptor)
181   {
182     SplitCell split = new SplitCell(descriptor);
183     if(layout != null)
184     {
185       Rectangle bounds = layout.getBounds(split.getId(), "SplitCell");
186       if(bounds != null)
187         split.getAttributes().put(GraphConstants.BOUNDS, bounds);
188       if(split.getChildCount() == 0)
189       {
190         WorkflowPort port = new WorkflowPort();
191         split.add(port);
192       }
193
194     }
195     else
196     {
197       WorkflowPort port = new WorkflowPort();
198       split.add(port);
199
200     }
201     getWorkflowGraphModel().insertSplitCell(split, null, null, null);
202   }
203
204   public void addStepDescriptor(StepDescriptor descriptor)
205   {
206     StepCell step = new StepCell(descriptor);
207     if(layout != null)
208     {
209       Rectangle bounds = layout.getBounds(step.getId(), "StepCell");
210       if(bounds != null)
211       {
212         step.getAttributes().put(GraphConstants.BOUNDS, bounds);
213       }
214       if(step.getChildCount() == 0)
215       {
216         WorkflowPort port = new WorkflowPort();
217         step.add(port);
218       }
219
220     }
221     else
222     {
223     }
224     // Insert into Model
225
getWorkflowGraphModel().insertStepCell(step, null, null, null);
226   }
227
228   public WorkflowGraphModel getWorkflowGraphModel()
229   {
230     return (WorkflowGraphModel)getModel();
231   }
232
233   /**
234    * Overriding method as required by JGraph API, In order to return right View object corresponding to Cell.
235    */

236   protected VertexView createVertexView(JGraph graph, CellMapper cm, Object JavaDoc v)
237   {
238     if(v instanceof StepCell)
239       return new StepView(v, graph, cm);
240     if(v instanceof SplitCell)
241       return new SplitView(v, graph, cm);
242     if(v instanceof JoinCell)
243       return new JoinView(v, graph, cm);
244     if(v instanceof InitialActionCell)
245       return new InitialActionView(v, graph, cm);
246     // Else Call Superclass
247
return super.createVertexView(graph, cm, v);
248   }
249
250   public void showMenu(int x, int y)
251   {
252     menuLocation.x = x;
253     menuLocation.y = y;
254     genericMenu.show(this, x, y);
255   }
256
257   public void showDelete(int x, int y)
258   {
259     menuLocation.x = x;
260     menuLocation.y = y;
261     cellMenu.show(this, x, y);
262   }
263
264   public boolean removeEdge(ResultEdge edge)
265   {
266     return getWorkflowGraphModel().removeEdge(edge);
267   }
268
269   public boolean removeStep(StepCell step)
270   {
271     getWorkflowGraphModel().removeStep(step);
272     return true;
273   }
274
275   public boolean removeJoin(JoinCell join)
276   {
277     return getWorkflowGraphModel().removeJoin(join);
278   }
279
280   public boolean removeSplit(SplitCell split)
281   {
282     return getWorkflowGraphModel().removeSplit(split);
283   }
284
285   public void dragEnter(DropTargetDragEvent dtde)
286   {
287   }
288
289   public void dragOver(DropTargetDragEvent dtde)
290   {
291   }
292
293   public void dropActionChanged(DropTargetDragEvent dtde)
294   {
295
296   }
297
298   public void drop(DropTargetDropEvent dtde)
299   {
300     try
301     {
302       //Ok, get the dropped object and try to figure out what it is.
303
Transferable JavaDoc tr = dtde.getTransferable();
304       DataFlavor JavaDoc[] flavors = tr.getTransferDataFlavors();
305       for(int i = 0; i < flavors.length; i++)
306       {
307         if(flavors[i].isFlavorSerializedObjectType())
308         {
309           if(flavors[i].equals(DragData.WORKFLOW_FLAVOR))
310           {
311             dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
312             DragData o = (DragData)tr.getTransferData(flavors[i]);
313             dtde.dropComplete(true);
314             CellFactory.createCell(this.descriptor, this.getWorkflowGraphModel(), dtde.getLocation(), o);
315             return;
316           }
317         }
318       }
319       dtde.rejectDrop();
320     }
321     catch(Exception JavaDoc e)
322     {
323       e.printStackTrace();
324       dtde.rejectDrop();
325     }
326   }
327
328   public void dragExit(DropTargetEvent dte)
329   {
330
331   }
332
333   public UndoManager getUndoManager()
334   {
335     return undoManager;
336   }
337 }
338
Popular Tags