1 package com.opensymphony.workflow.designer; 2 3 import java.util.*; 4 import javax.swing.undo.UndoableEdit ; 5 6 import com.opensymphony.workflow.designer.event.JoinChangedEvent; 7 import com.opensymphony.workflow.designer.event.JoinChangedListener; 8 import com.opensymphony.workflow.designer.proxy.ActionProxy; 9 import com.opensymphony.workflow.loader.*; 10 import org.jgraph.graph.*; 11 12 public class WorkflowGraphModel extends DefaultGraphModel 13 { 14 private Collection stepCells = new HashSet(); 15 private Collection splitCells = new HashSet(); 16 private Collection joinCells = new HashSet(); 17 private Collection initialActions = new ArrayList(); 18 private ResultHolderList results = new ResultHolderList(); 19 private IDGenerator resultIdGenerator = new IDGenerator(); 20 private Layout layout; 21 private Object context = new Object (); 22 private PaletteDescriptor palette; 23 24 public WorkflowGraphModel(Layout layout) 25 { 26 this.layout = layout; 27 } 28 29 public Object getContext() 30 { 31 return context; 32 } 33 34 public PaletteDescriptor getPalette() 35 { 36 return palette; 37 } 38 39 public void setPalette(PaletteDescriptor palette) 40 { 41 this.palette = palette; 42 } 43 44 private JoinCell getJoinCell(int id) 45 { 46 Iterator iter = joinCells.iterator(); 47 while(iter.hasNext()) 48 { 49 JoinCell cell = (JoinCell)iter.next(); 50 if(cell.getJoinDescriptor().getId() == id) 51 { 52 return cell; 53 } 54 } 55 return null; 56 } 57 58 public boolean acceptsTarget(Object edge, Object port) 59 { 60 if(port==null) return false; 61 WorkflowCell cell = (WorkflowCell)((WorkflowPort)port).getParent(); 62 if(cell instanceof InitialActionCell) return false; 63 return true; 64 } 65 66 public boolean acceptsSource(Object edge, Object port) 67 { 68 return false; 71 } 72 73 public void processJoinChangeEvent(JoinCell cell) 74 { 75 JoinDescriptor join = cell.getJoinDescriptor(); 76 77 List list = join.getConditions(); 78 for(int i = 0; i < list.size(); i++) 79 { 80 ConditionDescriptor cond = (ConditionDescriptor)list.get(i); 81 if(cond.getType().equals("class")) 82 { 83 String clazz = (String )cond.getArgs().get("class.name"); 84 try 85 { 86 Object obj = Class.forName(clazz).newInstance(); 87 if(obj instanceof JoinChangedListener) 88 { 89 JoinChangedEvent event = new JoinChangedEvent(cell, this); 90 event.setArgs(cond.getArgs()); 91 ((JoinChangedListener)obj).joinChanged(event); 92 cond.getArgs().putAll(event.getArgs()); 93 } 94 } 95 catch(Exception e) 96 { 97 } 98 } 99 } 100 } 101 102 public void insertInitialActions(List initialActions, InitialActionCell initialActionCell, Map attributes, ParentMap pm, UndoableEdit [] edits) 103 { 104 this.initialActions.add(initialActionCell); 105 for(int i = 0; i < initialActions.size() && i < 1; i++) 107 { 108 if(i == 0) 110 { 111 initialActionCell.setActionDescriptor((ActionDescriptor)initialActions.get(i)); 112 } 113 114 ActionDescriptor action = (ActionDescriptor)initialActions.get(i); 115 Utils.checkId(context, action); 116 List conResults = action.getConditionalResults(); 117 recordResults(initialActionCell, conResults, action); 118 ResultDescriptor result = action.getUnconditionalResult(); 119 if(result != null) 120 { 121 recordResult(initialActionCell, result, action); 122 } 123 Object [] cells = new Object []{initialActionCell}; 124 insert(cells, attributes, null, pm, edits); 126 } 127 } 128 129 public void insertStepCell(StepCell stepCell, Map attributes, ParentMap pm, UndoableEdit [] edits) 130 { 131 stepCells.add(stepCell); 132 Utils.checkId(context, stepCell.getDescriptor()); 133 Object [] cells = new Object []{stepCell}; 134 insert(cells, attributes, null, pm, edits); 136 recordResults(stepCell); 137 } 138 139 public void insertSplitCell(SplitCell splitCell, Map attributes, ParentMap pm, UndoableEdit [] edits) 140 { 141 splitCells.add(splitCell); 142 Utils.checkId(context, splitCell.getSplitDescriptor()); 143 Object [] cells = new Object []{splitCell}; 144 insert(cells, attributes, null, pm, edits); 146 recordResults(splitCell); 147 } 148 149 public void insertJoinCell(JoinCell joinCell, Map attributes, ParentMap pm, UndoableEdit [] edits) 150 { 151 joinCells.add(joinCell); 152 Utils.checkId(context, joinCell.getJoinDescriptor()); 153 Object [] cells = new Object []{joinCell}; 154 insert(cells, attributes, null, pm, edits); 156 recordResults(joinCell); 157 } 158 159 public void insertResultConnections() 160 { 161 Iterator steps = stepCells.iterator(); 162 while(steps.hasNext()) 163 { 164 StepCell stepCell = (StepCell)steps.next(); 165 processStepEndPointResult(stepCell); 166 } 167 Iterator splits = splitCells.iterator(); 168 while(splits.hasNext()) 169 { 170 SplitCell splitCell = (SplitCell)splits.next(); 171 processSplitEndPointResult(splitCell); 172 } 173 Iterator joins = joinCells.iterator(); 174 while(joins.hasNext()) 175 { 176 JoinCell joinCell = (JoinCell)joins.next(); 177 processJoinEndPointResult(joinCell); 178 this.processJoinChangeEvent(joinCell); 179 } 180 } 181 182 public void recordResults(JoinCell fromCell) 183 { 184 JoinDescriptor joinDescriptor = fromCell.getJoinDescriptor(); 185 ResultDescriptor result = joinDescriptor.getResult(); 186 if(result != null) 187 { 188 recordResult(fromCell, result, null); 189 } 190 } 191 192 public List getResultsToJoin(JoinCell joinCell) 193 { 194 return results.getResultsToJoin(joinCell.getJoinDescriptor().getId()); 195 } 196 197 private void processJoinEndPointResult(JoinCell joinCell) 198 { 199 int joinId = joinCell.getJoinDescriptor().getId(); 200 Iterator results = this.results.getResultsToJoin(joinId).iterator(); 201 while(results.hasNext()) 202 { 203 ResultHolder result = (ResultHolder)results.next(); 204 connectCells(result, joinCell); 205 } 206 } 207 208 private void processSplitEndPointResult(SplitCell splitCell) 209 { 210 int splitId = splitCell.getSplitDescriptor().getId(); 211 Iterator results = this.results.getResultsToSplit(splitId).iterator(); 212 while(results.hasNext()) 213 { 214 ResultHolder result = (ResultHolder)results.next(); 215 connectCells(result, splitCell); 216 } 217 218 } 219 220 public void recordResults(SplitCell fromCell) 221 { 222 SplitDescriptor splitDescriptor = fromCell.getSplitDescriptor(); 223 List results = splitDescriptor.getResults(); 224 recordResults(fromCell, results, null); 225 } 226 227 public List getResultsToStep(StepCell stepCell) 228 { 229 return results.getResultsToStep(stepCell.getDescriptor().getId()); 230 } 231 232 235 private void processStepEndPointResult(StepCell stepCell) 236 { 237 int stepId = stepCell.getDescriptor().getId(); 238 Iterator results = this.results.getResultsToStep(stepId).iterator(); 239 while(results.hasNext()) 240 { 241 ResultHolder result = (ResultHolder)results.next(); 242 connectCells(result, stepCell); 243 } 244 } 245 246 public void connectCells(WorkflowCell from, ActionDescriptor action, WorkflowCell to, ResultDescriptor result) 247 { 248 WorkflowPort fromPort = (WorkflowPort)from.getChildAt(0); 249 WorkflowPort toPort = (WorkflowPort)to.getChildAt(0); 250 251 resultIdGenerator.checkId(result.getId()); 252 if(result.getId() == 0) result.setId(resultIdGenerator.generateId()); 253 ResultEdge edge = new ResultEdge(result, layout!=null ? layout.getLabelPosition(result.getId()) : null); 255 edge.setUserObject(action==null ? null : new ActionProxy(action)); 256 257 ConnectionSet cs = new ConnectionSet(edge, fromPort, toPort); 259 Object [] cells = new Object []{edge}; 260 insert(cells, null, cs, null, null); 262 265 if(to instanceof JoinCell) 267 { 268 processJoinChangeEvent((JoinCell)to); 269 } 270 } 271 272 275 private void connectCells(ResultHolder resultCell, DefaultGraphCell toCell) 276 { 277 WorkflowPort fromPort = (WorkflowPort)resultCell.getFromCell().getChildAt(0); 278 WorkflowPort toPort = (WorkflowPort)toCell.getChildAt(0); 279 280 ResultDescriptor descriptor = resultCell.getDescriptor(); 282 resultIdGenerator.checkId(descriptor.getId()); 283 if(descriptor.getId()==0) descriptor.setId(resultIdGenerator.generateId()); 284 ResultEdge edge = new ResultEdge(descriptor, layout != null ? layout.getLabelPosition(descriptor.getId()) : null); 285 286 edge.setUserObject(new ActionProxy(resultCell.getAction())); 288 ConnectionSet cs = new ConnectionSet(edge, fromPort, toPort); 289 Object [] cells = new Object []{edge}; 290 insert(cells, null, cs, null, null); 292 } 295 296 300 public void recordResults(StepCell fromCell) 301 { 302 StepDescriptor stepDescriptor = fromCell.getDescriptor(); 303 List actionList = stepDescriptor.getActions(); 304 for(int i = 0; i < actionList.size(); i++) 305 { 306 ActionDescriptor action = (ActionDescriptor)actionList.get(i); 307 Utils.checkId(context, action); 308 List conResults = action.getConditionalResults(); 309 recordResults(fromCell, conResults, action); 310 ResultDescriptor result = action.getUnconditionalResult(); 311 if(result!=null) 312 recordResult(fromCell, result, action); 313 } 314 } 315 316 private void recordResults(WorkflowCell fromCell, List results, ActionDescriptor action) 317 { 318 for(int i = 0; i < results.size(); i++) 319 { 320 ResultDescriptor result = (ResultDescriptor)results.get(i); 321 recordResult(fromCell, result, action); 322 } 323 } 324 325 public ResultHolder recordResult(WorkflowCell fromCell, ResultDescriptor result, ActionDescriptor action) 326 { 327 ResultHolder newCell = new ResultHolder(fromCell, result, action); 328 results.add(newCell); 329 return newCell; 330 } 331 332 public Collection getActivitiesList() 333 { 334 List l = new ArrayList(); 335 l.addAll(initialActions); 336 l.addAll(stepCells); 337 l.addAll(splitCells); 338 l.addAll(joinCells); 339 Iterator i = l.iterator(); 340 Set edges = new HashSet(); 341 while(i.hasNext()) 342 { 343 WorkflowCell cell = (WorkflowCell)i.next(); 344 Iterator j = edges(cell.getFirstChild()); 346 while(j.hasNext()) 347 { 348 WorkflowEdge edge = (WorkflowEdge)j.next(); 349 edges.add(edge); 350 } 351 } 352 l.addAll(edges); 353 return l; 354 } 355 356 public boolean removeEdge(ResultEdge edge) 357 { 358 ResultDescriptor result = edge.getDescriptor(); 359 360 ResultHolder cell = results.getResultCell(result); 361 DefaultGraphCell from = cell.getFromCell(); 362 if(from instanceof ResultAware) 363 { 364 ResultAware remove = (ResultAware)from; 366 if(!remove.removeResult(result)) 367 { 368 return false; 369 } 370 Object [] objs = new Object []{edge}; 371 remove(objs); 373 374 376 results.remove(cell); 377 } 379 380 if(result.getJoin() > 0) 381 { 382 JoinCell join = getJoinCell(result.getJoin()); 383 if(join != null) 384 { 385 this.processJoinChangeEvent(join); 386 } 387 else 388 { 389 return false; 390 } 391 } 392 393 return true; 394 } 395 396 public boolean removeStep(StepCell cell) 397 { 398 StepDescriptor step = cell.getDescriptor(); 399 400 Set set = getEdges(this, new Object []{cell}); 402 Iterator iter = set.iterator(); 403 while(iter.hasNext()) 404 { 405 Object obj = iter.next(); 406 if(obj instanceof ResultEdge) 407 { 408 removeEdge((ResultEdge)obj); 409 } 410 } 411 412 WorkflowDescriptor workflow = (WorkflowDescriptor)step.getParent(); 414 List list = workflow.getSteps(); 415 list.remove(step); 416 417 419 list = cell.getChildren(); 421 for(int i = 0; i < list.size(); i++) 422 { 423 remove(new Object []{list.get(i)}); 424 } 425 remove(new Object []{cell}); 427 428 stepCells.remove(cell); 430 431 return true; 432 } 433 434 public boolean removeJoin(JoinCell cell) 435 { 436 JoinDescriptor join = cell.getJoinDescriptor(); 437 438 Set set = getEdges(this, new Object []{cell}); 440 Iterator iter = set.iterator(); 441 while(iter.hasNext()) 442 { 443 Object obj = iter.next(); 444 if(obj instanceof ResultEdge) 445 { 446 removeEdge((ResultEdge)obj); 447 } 448 } 449 450 WorkflowDescriptor workflow = (WorkflowDescriptor)join.getParent(); 452 List list = workflow.getJoins(); 453 list.remove(join); 454 455 457 list = cell.getChildren(); 459 for(int i = 0; i < list.size(); i++) 460 { 461 remove(new Object []{list.get(i)}); 462 } 463 remove(new Object []{cell}); 465 466 joinCells.remove(cell); 468 469 return true; 470 } 471 472 public boolean removeSplit(SplitCell cell) 473 { 474 475 SplitDescriptor split = cell.getSplitDescriptor(); 476 477 Set set = getEdges(this, new Object []{cell}); 479 Iterator iter = set.iterator(); 480 while(iter.hasNext()) 481 { 482 Object obj = iter.next(); 483 if(obj instanceof ResultEdge) 484 { 485 removeEdge((ResultEdge)obj); 486 } 487 } 488 489 WorkflowDescriptor workflow = (WorkflowDescriptor)split.getParent(); 491 List list = workflow.getSplits(); 492 list.remove(split); 493 494 496 list = cell.getChildren(); 498 for(int i = 0; i < list.size(); i++) 499 { 500 remove(new Object []{list.get(i)}); 501 } 502 remove(new Object []{cell}); 504 505 splitCells.remove(cell); 507 508 return true; 509 } 510 } 511 | Popular Tags |