KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.opensymphony.workflow.designer;
2
3 import java.util.List JavaDoc;
4
5 import javax.swing.JOptionPane JavaDoc;
6
7 import com.opensymphony.workflow.loader.AbstractDescriptor;
8 import com.opensymphony.workflow.loader.ActionDescriptor;
9 import com.opensymphony.workflow.loader.DescriptorBuilder;
10 import com.opensymphony.workflow.loader.JoinDescriptor;
11 import com.opensymphony.workflow.loader.ResultDescriptor;
12 import com.opensymphony.workflow.loader.SplitDescriptor;
13 import com.opensymphony.workflow.loader.StepDescriptor;
14 import com.opensymphony.workflow.designer.dialogs.ActionTypeDialog;
15
16 /**
17  * @author baab
18  */

19 public class ConnectHelper
20 {
21
22   public static final int CONDITIONAL = 0;
23   public static final int UNCONDITIONAL = 1;
24   private static final int UNKNOWN = -1;
25
26   private static boolean isConnectable(WorkflowCell source, WorkflowCell target)
27   {
28
29     if(source == null || target == null)
30     {
31       return false;
32     }
33
34     if(source instanceof InitialActionCell && target instanceof StepCell)
35     {
36       return true;
37     }
38     else if(source instanceof StepCell && !(target instanceof InitialActionCell))
39     {
40       return true;
41     }
42     else if(source instanceof JoinCell && target instanceof StepCell)
43     {
44       return true;
45     }
46     else if(source instanceof SplitCell && target instanceof StepCell)
47     {
48       return true;
49     }
50
51     return false;
52   }
53
54   public static boolean connect(WorkflowCell source, WorkflowCell target, WorkflowGraphModel model)
55   {
56
57     if(!isConnectable(source, target))
58     {
59       return false;
60     }
61
62     if(source instanceof SplitCell && target instanceof StepCell)
63     {
64       return connectSplitToStep((SplitCell)source, (StepCell)target, model);
65     }
66     else if(source instanceof InitialActionCell && target instanceof StepCell)
67     {
68       return connectStartToStep((InitialActionCell)source, (StepCell)target, model);
69     }
70     else if(source instanceof JoinCell && target instanceof StepCell)
71     {
72       return connectJoinToStep((JoinCell)source, (StepCell)target, model);
73     }
74     else if(source instanceof StepCell && !(target instanceof InitialActionCell))
75     {
76       return connectStepTo((StepCell)source, target, model);
77     }
78     return true;
79   }
80
81   private static boolean isConnected(List JavaDoc results, AbstractDescriptor desc)
82   {
83     if(results == null || results.size() == 0)
84     {
85       return false;
86     }
87
88     if(desc instanceof StepDescriptor)
89     {
90       return _isConnected(results, (StepDescriptor)desc);
91     }
92     else if(desc instanceof SplitDescriptor)
93     {
94       return _isConnected(results, (SplitDescriptor)desc);
95     }
96     else if(desc instanceof JoinDescriptor)
97     {
98       return _isConnected(results, (JoinDescriptor)desc);
99     }
100     return false;
101   }
102
103   private static boolean _isConnected(List JavaDoc results, StepDescriptor step)
104   {
105     if(results == null)
106     {
107       return false;
108     }
109     for(int i = 0; i < results.size(); i++)
110     {
111       ResultDescriptor result = (ResultDescriptor)results.get(i);
112       if(result.getStep() == step.getId())
113       {
114         // already connected
115
return true;
116       }
117     }
118     return false;
119   }
120
121   private static boolean _isConnected(List JavaDoc results, JoinDescriptor join)
122   {
123     if(results == null)
124     {
125       return false;
126     }
127     for(int i = 0; i < results.size(); i++)
128     {
129       ResultDescriptor result = (ResultDescriptor)results.get(i);
130       if(result.getJoin() == join.getId())
131       {
132         // already connected
133
return true;
134       }
135     }
136     return false;
137   }
138
139   private static boolean _isConnected(List JavaDoc results, SplitDescriptor split)
140   {
141     if(results == null)
142     {
143       return false;
144     }
145     for(int i = 0; i < results.size(); i++)
146     {
147       ResultDescriptor result = (ResultDescriptor)results.get(i);
148       if(result.getSplit() == split.getId())
149       {
150         // already connected
151
return true;
152       }
153     }
154     return false;
155   }
156
157   public static boolean isConnected(ResultDescriptor result, AbstractDescriptor desc)
158   {
159     if(result == null)
160     {
161       return false;
162     }
163     if(desc instanceof StepDescriptor)
164     {
165       return _isConnected(result, (StepDescriptor)desc);
166     }
167     else if(desc instanceof JoinDescriptor)
168     {
169       return _isConnected(result, (JoinDescriptor)desc);
170     }
171     else if(desc instanceof SplitDescriptor)
172     {
173       return _isConnected(result, (SplitDescriptor)desc);
174     }
175
176     return false;
177   }
178
179   private static boolean _isConnected(ResultDescriptor result, StepDescriptor step)
180   {
181     return result.getStep() == step.getId();
182   }
183
184   private static boolean _isConnected(ResultDescriptor result, JoinDescriptor join)
185   {
186     return result.getJoin() == join.getId();
187   }
188
189   private static boolean _isConnected(ResultDescriptor result, SplitDescriptor split)
190   {
191     return result.getSplit() == split.getId();
192   }
193
194   private static int getConnectType(ActionDescriptor from, AbstractDescriptor to)
195   {
196     List JavaDoc results = from.getConditionalResults();
197     if(isConnected(results, to))
198     {
199       return UNKNOWN;
200     }
201
202     ResultDescriptor result = from.getUnconditionalResult();
203     if(isConnected(result, to))
204     {
205       return UNKNOWN;
206     }
207
208     int type;
209     if(result != null)
210     {
211       type = CONDITIONAL;
212     }
213     else
214     {
215       // choose unconditional or conditional
216
String JavaDoc conditional = ResourceManager.getString("result.conditional");
217       String JavaDoc unconditional = ResourceManager.getString("result.unconditional");
218       String JavaDoc cancel = ResourceManager.getString("cancel");
219       type = JOptionPane.showOptionDialog(null, ResourceManager.getString("result.select.long"), ResourceManager.getString("result.select"), JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object JavaDoc[]{conditional, unconditional, cancel}, conditional);
220       if(type != 0 && type != 1)
221       {
222         // cancel
223
return UNKNOWN;
224       }
225     }
226     return type;
227   }
228
229   private static boolean connectStepTo(StepCell source, WorkflowCell target, WorkflowGraphModel model)
230   {
231     AbstractDescriptor to;
232     if(target instanceof StepCell)
233     {
234       to = ((StepCell)target).getDescriptor();
235     }
236     else if(target instanceof SplitCell)
237     {
238       to = ((SplitCell)target).getSplitDescriptor();
239     }
240     else if(target instanceof JoinCell)
241     {
242       to = ((JoinCell)target).getJoinDescriptor();
243     }
244     else
245     {
246       return false;
247     }
248
249     ResultDescriptor result;
250
251     ActionTypeDialog selectType = new ActionTypeDialog(WorkflowDesigner.INSTANCE, source.getDescriptor());
252     selectType.setModel(model);
253     if(!selectType.ask(WorkflowDesigner.INSTANCE)) return false;
254
255     ActionDescriptor sourceAction = selectType.getRelatedAction();
256
257     int type = selectType.getType();
258
259     if(type == CONDITIONAL)
260     {
261       result = DescriptorBuilder.createConditionalResult(model, to);
262     }
263     else if(type == UNCONDITIONAL)
264     {
265       result = DescriptorBuilder.createResult(model, to);
266     }
267     else
268     {
269       return false;
270     }
271
272     result.setParent(sourceAction);
273     if(type == CONDITIONAL)
274     {
275       sourceAction.getConditionalResults().add(result);
276     }
277     else
278     {
279       sourceAction.setUnconditionalResult(result);
280     }
281
282     model.recordResult(source, result, sourceAction);
283     model.connectCells(source, sourceAction, target, result);
284
285     return true;
286   }
287
288   private static boolean connectStartToStep(InitialActionCell source, StepCell target, WorkflowGraphModel model)
289   {
290     ActionDescriptor start = source.getActionDescriptor();
291     StepDescriptor step = target.getDescriptor();
292     ResultDescriptor result;
293
294     int type = getConnectType(start, step);
295
296     if(type == CONDITIONAL)
297     {
298       result = DescriptorBuilder.createConditionalResult(model, step);
299     }
300     else if(type == UNCONDITIONAL)
301     {
302       result = DescriptorBuilder.createResult(model, step);
303     }
304     else
305     {
306       return false;
307     }
308
309     result.setParent(start);
310     if(type == CONDITIONAL)
311     {
312       start.getConditionalResults().add(result);
313     }
314     else
315     {
316       start.setUnconditionalResult(result);
317     }
318
319     // create new resultCell
320
model.recordResult(source, result, start);
321
322     model.connectCells(source, start, target, result);
323
324     return true;
325   }
326
327   private static boolean connectSplitToStep(SplitCell source, StepCell target, WorkflowGraphModel model)
328   {
329     SplitDescriptor split = source.getSplitDescriptor();
330     StepDescriptor step = target.getDescriptor();
331
332     List JavaDoc results = split.getResults();
333
334     if(isConnected(results, step))
335     {
336       return false;
337     }
338
339     // create new unconditional result
340
ResultDescriptor result = DescriptorBuilder.createResult(model, step);
341     result.setParent(split);
342
343     // add to split's result list
344
results.add(result);
345
346     model.recordResult(source, result, null);
347
348     // update the graph
349
model.connectCells(source, null, target, result);
350
351     return true;
352   }
353
354   private static boolean connectJoinToStep(JoinCell source, StepCell target, WorkflowGraphModel model)
355   {
356     JoinDescriptor join = source.getJoinDescriptor();
357     StepDescriptor step = target.getDescriptor();
358
359     ResultDescriptor result = join.getResult();
360     if(result != null)
361     {
362       return false;
363     }
364
365     // create new unconditional result
366
result = DescriptorBuilder.createResult(model, step);
367     result.setParent(join);
368     join.setResult(result);
369
370     model.recordResult(source, result, null);
371
372     // update the graph
373
model.connectCells(source, null, target, result);
374
375     return true;
376   }
377
378 }
379
Popular Tags