1 10 11 package org.enhydra.jawe; 12 13 import org.enhydra.jawe.graph.*; 14 15 import org.jgraph.JGraph; 16 import org.jgraph.graph.*; 17 import org.jgraph.event.*; 18 19 import java.io.*; 20 import java.util.*; 21 import javax.swing.undo.*; 22 import javax.swing.tree.*; 23 import java.awt.Rectangle ; 24 import javax.swing.event.*; 25 26 29 public class JaWEGraphModel 30 extends UndoableEditSupport 31 implements Serializable, GraphModel { 32 33 34 protected transient EventListenerList listenerList = 35 new EventListenerList(); 36 37 38 protected transient Iterator emptyIterator = new EmptyIterator(); 39 40 41 protected List roots = new ArrayList(); 42 43 44 protected boolean asksAllowsChildren = false; 45 46 49 public JaWEGraphModel() { 50 } 51 52 56 62 public int getRootCount() { 63 return roots.size(); 64 } 65 66 74 public Object getRootAt(int index) { 75 return roots.get(index); 76 } 77 78 85 public int getIndexOfRoot(Object root) { 86 return roots.indexOf(root); 87 } 88 89 95 public boolean contains(Object node) { 96 Object parentNode = null; 97 while ((parentNode = getParent(node)) 98 != null) 99 node = parentNode; 100 return roots.contains(node); 101 } 102 103 110 public Map getAttributes(Object node) { 111 if (node instanceof GraphCell) 112 return ((GraphCell) node).getAttributes(); 113 return null; 114 } 115 116 120 126 public Object getSource(Object edge) { 127 if (edge instanceof Edge) 128 return ((Edge) edge).getSource(); 129 return null; 130 } 131 132 138 public Object getTarget(Object edge) { 139 if (edge instanceof Edge) 140 return ((Edge) edge).getTarget(); 141 return null; 142 } 143 144 152 public boolean acceptsSource(Object edge, Object port) { 153 return true; 154 } 155 156 164 public boolean acceptsTarget(Object edge, Object port) { 165 return true; 166 } 167 168 176 public Iterator edges(Object port) { 177 if (port instanceof Port) 178 return ((Port) port).edges(); 179 return emptyIterator; 180 } 181 182 187 public boolean isEdge(Object edge) { 188 return edge instanceof Edge; 189 } 190 191 197 public boolean isPort(Object port) { 198 return port instanceof Port; 199 } 200 204 209 public Map cloneCells(Object [] cells) { 210 Map map = new Hashtable(); 211 ArrayList q = new ArrayList(); 213 for (int i = 0; i < cells.length; i++) 214 q.add(cells[i]); 215 while (!q.isEmpty()) { 217 Object node = q.remove(0); 219 if (node instanceof DefaultGraphCell) { 220 for (int i = 0; i < getChildCount(node); i++) 222 q.add(getChild(node, i)); 223 DefaultGraphCell cell = (DefaultGraphCell) node; 225 DefaultGraphCell clone = (DefaultGraphCell) cell.clone(); 226 Object par = getParent(cell); 227 if (par != null) { 228 DefaultMutableTreeNode p = 229 (DefaultMutableTreeNode) map.get(par); 230 if (p != null) 231 p.add(clone); 232 } 233 map.put(cell, clone); 235 } 236 } 237 Iterator it = map.values().iterator(); 239 while (it.hasNext()) { 240 Object obj = it.next(); 241 if (obj instanceof Port) { 243 Object anchor = ((Port) obj).getAnchor(); 244 if (anchor != null) 246 ((Port) obj).setAnchor((Port) map.get(anchor)); 247 } 248 } 249 return map; 250 } 251 252 253 262 public Object getParent(Object child) { 263 if (child != null && child instanceof TreeNode) 264 return ((TreeNode) child).getParent(); 265 return null; 266 } 267 268 276 public int getIndexOfChild(Object parent, Object child) { 277 if (parent == null || child == null) 278 return -1; 279 return ((TreeNode) parent).getIndex((TreeNode) child); 280 } 281 282 292 public Object getChild(Object parent, int index) { 293 if (parent instanceof TreeNode) 294 return ((TreeNode) parent).getChildAt(index); 295 return null; 296 } 297 298 306 public int getChildCount(Object parent) { 307 if (parent instanceof TreeNode) 308 return ((TreeNode) parent).getChildCount(); 309 return 0; 310 } 311 312 319 public boolean isLeaf(Object node) { 320 if (asksAllowsChildren && node instanceof TreeNode) 321 return !((TreeNode) node).getAllowsChildren(); 322 return ((TreeNode) node).isLeaf(); 323 } 324 325 326 330 345 public void insert( 346 Object [] roots, 347 Map attributes, 348 ConnectionSet cs, 349 ParentMap pm, 350 UndoableEdit[] edits) { 351 String undoMsg=ResourceManager.getLanguageDependentString("MessageInsertingObjects"); 352 GraphModelEdit edit = 353 createInsertEdit(roots, attributes, cs, pm, edits, undoMsg); 354 if (edit != null) { 355 edit.execute(); 356 if (edits != null) { 357 for (int i = 0; i < edits.length; i++) 358 if (edits[i] 359 instanceof GraphModelEvent.ExecutableGraphChange) 360 ((GraphModelEvent.ExecutableGraphChange) edits[i]) 361 .execute(); 362 } 363 postEdit(edit); 364 } 365 } 366 367 368 372 public void remove(Object [] roots) { 373 String undoMsg=ResourceManager.getLanguageDependentString("MessageRemovingObjects"); 374 GraphModelEdit edit = createRemoveEdit(roots,undoMsg); 375 if (edit != null) { 376 edit.execute(); 377 postEdit(edit); 378 } 379 } 380 381 394 public void edit( 395 Map attributes, 396 ConnectionSet cs, 397 ParentMap pm, 398 UndoableEdit[] edits) { 399 if ((attributes == null || attributes.isEmpty()) 400 && (cs == null || cs.isEmpty()) 401 && pm == null 402 && edits != null 403 && edits.length == 1) { 404 if (edits[0] instanceof GraphModelEvent.ExecutableGraphChange) 405 ((GraphModelEvent.ExecutableGraphChange) edits[0]).execute(); 406 postEdit(edits[0]); } else { 408 String undoMsg=ResourceManager.getLanguageDependentString("MessageEditingObject"); 409 GraphModelEdit edit = createCellEdit(attributes, cs, pm, edits, undoMsg); 410 if (edit != null) { 412 edit.execute(); 413 if (edits != null) { 414 for (int i = 0; i < edits.length; i++) 415 if (edits[i] 416 instanceof GraphModelEvent.ExecutableGraphChange) 417 ((GraphModelEvent.ExecutableGraphChange) edits[i]) 418 .execute(); 419 } 420 postEdit(edit); 421 } 422 } 423 } 424 425 428 public void editFonts(Map attributes) { 429 GraphModelEdit edit = createCellEdit(attributes, null, null, null, null); 430 if (edit != null) { 432 edit.execute(); 433 } 434 } 435 436 437 440 public void removeBubbles (Object [] bubbles,GraphModelListener gml) { 441 removeGraphModelListener(gml); 442 GraphModelEdit edit = createRemoveEdit(bubbles,null); 443 if (edit != null) { 444 edit.execute(); 445 } 446 addGraphModelListener(gml); 447 } 448 449 464 public void insertAndEdit( 465 Object [] roots, 466 Map attributes, 467 ConnectionSet cs, 468 ParentMap pm, 469 UndoableEdit[] edits, 470 String undoMsg) { 471 GraphModelEdit edit = 472 createInsertEdit(roots, attributes, cs, pm, edits, undoMsg); 473 if (edit != null) { 474 edit.execute(); 475 if (edits != null) { 476 for (int i = 0; i < edits.length; i++) 477 if (edits[i] 478 instanceof GraphModelEvent.ExecutableGraphChange) 479 ((GraphModelEvent.ExecutableGraphChange) edits[i]) 480 .execute(); 481 } 482 postEdit(edit); 483 } 484 } 485 486 492 public void removeAndEdit(Object [] roots,Map attributes,String name) { 493 GraphModelEdit edit = createRemoveAndCellEdit(roots,attributes,name); 494 if (edit!=null) { 495 edit.execute(); 496 postEdit(edit); 497 } 498 } 499 500 503 public void toBack(Object [] cells) { 504 GraphModelLayerEdit edit = 505 createLayerEdit(cells, GraphModelLayerEdit.BACK); 506 if (edit != null) { 507 edit.execute(); 508 postEdit(edit); 509 } 510 } 511 512 515 public void toFront(Object [] cells) { 516 GraphModelLayerEdit edit = 517 createLayerEdit(cells, GraphModelLayerEdit.FRONT); 518 if (edit != null) { 519 edit.execute(); 520 postEdit(edit); 521 } 522 } 523 524 528 protected GraphModelLayerEdit createLayerEdit(Object [] cells, int layer) { 529 return new GraphModelLayerEdit(cells, layer); 530 } 531 532 535 protected GraphModelEdit createInsertEdit( 536 Object [] cells, 537 Map attributeMap, 538 ConnectionSet cs, 539 ParentMap pm, 540 UndoableEdit[] edits, 541 String name) { 542 GraphModelEdit edit = 544 createEdit(cells, null, attributeMap, cs, pm, name); 545 if (edit != null) { 546 if (edits != null) 547 for (int i = 0; i < edits.length; i++) 548 edit.addEdit(edits[i]); 549 edit.end(); 550 } 551 return edit; 552 } 553 554 557 protected GraphModelEdit createRemoveEdit(Object [] cells,String name) { 558 ConnectionSet cs = ConnectionSet.create(this, cells, true); 560 ParentMap pm = ParentMap.create(this, cells, true, false); 562 GraphModelEdit edit = createEdit(null, cells, null, cs, pm, name); 565 if (edit != null) 566 edit.end(); 567 return edit; 568 } 569 570 573 protected GraphModelEdit createCellEdit( 574 Map attributes, 575 ConnectionSet cs, 576 ParentMap pm, 577 UndoableEdit[] edits, 578 String name) { 579 GraphModelEdit edit = createEdit(null, null, attributes, cs, pm, name); 581 if (edit != null) { 582 if (edits != null) 583 for (int i = 0; i < edits.length; i++) 584 edit.addEdit(edits[i]); 585 edit.end(); 586 } 587 return edit; 588 } 589 590 591 protected GraphModelEdit createEdit( 592 Object [] inserted, 593 Object [] removed, 594 Map attributes, 595 ConnectionSet cs, 596 ParentMap pm, 597 String name) { 598 return new GraphModelEdit( 599 inserted, 600 removed, 601 attributes, 602 cs, 603 pm, 604 name); 605 } 606 607 610 protected GraphModelEdit createRemoveAndCellEdit(Object [] cells, 611 Map attributes,String name) { 612 ConnectionSet cs = ConnectionSet.create(this, cells, true); 614 ParentMap pm = ParentMap.create(this, cells, true, false); 616 GraphModelEdit edit = createEdit (null,cells,attributes,cs,pm,name); 618 if (edit!=null) { 619 edit.end(); 620 } 621 return edit; 622 } 623 624 628 632 protected Object [] handleInsert(Object [] cells) { 633 Object [] inserted = null; 634 if (cells != null) { 635 for (int i = 0; i < cells.length; i++) 636 if (getParent(cells[i]) == null) 638 roots.add(cells[i]); 639 inserted = getDescendants(this, cells).toArray(); 641 } 642 return inserted; 643 } 644 645 649 protected Object [] handleRemove(Object [] cells) { 650 List removedRoots = new ArrayList(); 651 if (cells != null) 652 for (int i = 0; i < cells.length; i++) 653 if (getParent(cells[i]) == null && roots.remove(cells[i])) 654 removedRoots.add(cells[i]); 655 return removedRoots.toArray(); 656 } 657 658 662 protected ParentMap handleParentMap(ParentMap parentMap) { 663 if (parentMap != null) { 664 ParentMap undo = new ParentMap(); 665 Iterator it = parentMap.entries(); 666 while (it.hasNext()) { 667 ParentMap.Entry entry = (ParentMap.Entry) it.next(); 668 Object child = entry.getChild(); 669 Object parent = entry.getParent(); 670 undo.addEntry(child, getParent(child)); 671 if (parent == null){ 672 if (child instanceof MutableTreeNode){ 673 ((MutableTreeNode)child).removeFromParent(); 674 } 675 } else { 676 if (parent instanceof DefaultMutableTreeNode && 677 child instanceof MutableTreeNode){ 678 ((DefaultMutableTreeNode)parent).add((MutableTreeNode)child); 679 } 680 } 681 682 boolean isRoot = roots.contains(child); 683 if (parent == null && !isRoot) 684 roots.add(child); 685 else if (parent != null && isRoot) 686 roots.remove(child); 687 } 688 return undo; 689 } 690 return null; 691 } 692 693 697 protected Map handleAttributes(Map attributes) { 698 if (attributes != null) { 699 Hashtable undo = new Hashtable(); 700 Iterator it = attributes.entrySet().iterator(); 701 while (it.hasNext()) { 702 Map.Entry entry = (Map.Entry) it.next(); 703 Object cell = entry.getKey(); 704 Map deltaNew = (Map) entry.getValue(); 705 if (cell instanceof GraphCell){ 708 Map deltaOld = ((GraphCell)cell).changeAttributes(deltaNew); 709 undo.put(cell, deltaOld); 712 } else { 713 Map attr = getAttributes(cell); 714 if (attr != null){ 715 Map deltaOld = GraphConstants.applyMap(deltaNew, attr); 716 undo.put(cell, deltaOld); 719 } 720 } 721 } 722 return undo; 723 } 724 return null; 725 } 726 727 731 735 protected ConnectionSet handleConnectionSet(ConnectionSet cs) { 736 if (cs != null) { 737 ConnectionSet csundo = new ConnectionSet(); 738 Iterator it = cs.connections(); 739 while (it.hasNext()) { 740 ConnectionSet.Connection c = 741 (ConnectionSet.Connection) it.next(); 742 Object edge = c.getEdge(); 743 if (c.isSource()) 744 csundo.connect(edge, getSource(edge), true); 745 else 746 csundo.connect(edge, getTarget(edge), false); 747 handleConnection(c); 748 } 749 return csundo; 750 } 751 return null; 752 } 753 754 757 protected void handleConnection(ConnectionSet.Connection c) { 758 Object edge = c.getEdge(); 759 Object old = (c.isSource()) ? getSource(edge) : getTarget(edge); 760 Object port = c.getPort(); 761 if (port != old) { 762 connect(edge, old, c.isSource(), true); 763 if (contains(port) && contains(edge)) 764 connect(edge, port, c.isSource(), false); 765 } 766 } 767 768 773 protected void connect( 774 Object edge, 775 Object port, 776 boolean isSource, 777 boolean remove) { 778 if (port instanceof Port) 779 if (remove) 780 ((Port) port).removeEdge(edge); 781 else 782 ((Port) port).addEdge(edge); 783 if (remove) 784 port = null; 785 if (edge instanceof Edge) { 786 if (isSource) 787 ((Edge) edge).setSource(port); 788 else 789 ((Edge) edge).setTarget(port); 790 } 791 } 792 793 797 803 public void addGraphModelListener(GraphModelListener l) { 804 listenerList.add(GraphModelListener.class, l); 805 } 806 807 813 public void removeGraphModelListener(GraphModelListener l) { 814 listenerList.remove(GraphModelListener.class, l); 815 } 816 817 824 protected void fireGraphChanged( 825 Object source, 826 GraphModelEvent.GraphModelChange edit) { 827 Object [] listeners = listenerList.getListenerList(); 829 GraphModelEvent e = null; 830 for (int i = listeners.length - 2; i >= 0; i -= 2) { 833 if (listeners[i] == GraphModelListener.class) { 834 if (e == null) 836 e = new GraphModelEvent(source, edit); 837 ((GraphModelListener) listeners[i + 1]).graphChanged(e); 838 } 839 } 840 } 841 842 845 public GraphModelListener[] getGraphModelListeners() { 846 return (GraphModelListener[]) listenerList.getListeners( 847 GraphModelListener.class); 848 } 849 850 854 858 public class GraphModelEdit 859 extends CompoundEdit 860 implements 861 GraphModelEvent.GraphModelChange, 862 GraphModelEvent.ExecutableGraphChange { 863 864 865 protected String name; 866 867 868 protected Object [] insert, changed, remove, context; 869 870 871 protected Object [] inserted, removed; 872 873 875 protected Map attributes, previousAttributes; 876 877 878 protected ParentMap parentMap, previousParentMap; 879 880 881 protected ConnectionSet connectionSet, previousConnectionSet; 882 883 884 protected Map cellViews = new Hashtable(); 885 886 897 public GraphModelEdit( 898 Object [] inserted, 899 Object [] removed, 900 Map attributes, 901 ConnectionSet connectionSet, 902 ParentMap parentMap, 903 String name) { 904 super(); 905 this.insert = inserted; 906 this.remove = removed; 907 this.connectionSet = connectionSet; 908 this.attributes = attributes; 909 this.parentMap = parentMap; 910 this.name=name; 911 previousAttributes = attributes; 912 previousConnectionSet = connectionSet; 913 previousParentMap = parentMap; 914 if (parentMap != null) { 916 932 } 933 } 934 935 936 public Object [] filterParents(Map childCount, int children) { 937 ArrayList list = new ArrayList(); 938 Iterator it = childCount.entrySet().iterator(); 939 while (it.hasNext()) { 940 Map.Entry entry = (Map.Entry) it.next(); 941 if (entry.getValue() instanceof Integer ) { 942 if (((Integer ) entry.getValue()).intValue() == children) 943 list.add(entry.getKey()); 944 } 945 } 946 return list.toArray(); 947 } 948 949 protected void changeChildCount(Map childCount, Object parent, int change) { 950 if (parent != null) { 951 Integer count = (Integer ) childCount.get(parent); 952 if (count == null) { 953 count = new Integer (getChildCount(parent)); 954 } 955 int newValue = count.intValue() + change; 956 childCount.put(parent, new Integer (newValue)); 957 } 958 } 959 960 966 protected void handleEmptyGroups(Object [] groups) { 967 if (groups != null && groups.length > 0) { 968 if (remove == null) 969 remove = new Object [] { 970 }; 971 Object [] tmp = new Object [remove.length + groups.length]; 972 System.arraycopy(remove, 0, tmp, 0, remove.length); 973 System.arraycopy(groups, 0, tmp, remove.length, groups.length); 974 remove = tmp; 975 } 976 } 977 978 public boolean isSignificant() { 979 return true; 980 } 981 982 988 public String getPresentationName() { 989 return name; 990 } 991 992 996 public Object getSource() { 997 return JaWEGraphModel.this; 998 } 999 1000 1005 public Object [] getChanged() { 1006 return changed; 1007 } 1008 1009 1013 public Object [] getContext() { 1014 return context; 1015 } 1016 1017 1020 public Object [] getInserted() { 1021 return inserted; 1022 } 1023 1024 1027 public Object [] getRemoved() { 1028 return removed; 1029 } 1030 1031 1032 1036 public Map getPreviousAttributes() { 1037 return previousAttributes; 1038 } 1039 1040 1044 public Map getAttributes() { 1045 return attributes; 1046 } 1047 1048 1052 public ConnectionSet getConnectionSet() { 1053 return connectionSet; 1054 } 1055 1056 public ConnectionSet getPreviousConnectionSet() { 1057 return previousConnectionSet; 1058 } 1059 1060 1064 public ParentMap getParentMap() { 1065 return parentMap; 1066 } 1067 1068 public ParentMap getPreviousParentMap() { 1069 return previousParentMap; 1070 } 1071 1072 1077 public void redo() throws CannotRedoException { 1078 super.redo(); 1079 execute(); 1080 } 1081 1082 1087 public void undo() throws CannotUndoException { 1088 super.undo(); 1089 execute(); 1090 } 1091 1092 1096 public void execute() { 1097 Set tmp = new HashSet(); 1099 if (attributes != null) 1100 tmp.addAll(attributes.keySet()); 1101 if (parentMap != null) 1102 tmp.addAll(parentMap.getChangedNodes()); 1103 if (connectionSet != null) 1105 tmp.addAll(connectionSet.getChangedEdges()); 1106 if (remove != null) { 1107 for (int i = 0; i < remove.length; i++) 1108 tmp.remove(remove[i]); 1109 } 1110 changed = tmp.toArray(); 1111 Set ctx = getEdges(JaWEGraphModel.this, changed); 1113 context = ctx.toArray(); 1114 inserted = insert; 1116 removed = remove; 1117 remove = handleInsert(inserted); 1118 previousParentMap = parentMap; 1119 parentMap = handleParentMap(parentMap); 1120 if (parentMap != null) 1121 tmp.addAll(parentMap.getChangedNodes()); 1122 previousConnectionSet = connectionSet; 1123 connectionSet = handleConnectionSet(connectionSet); 1124 insert = handleRemove(removed); 1125 previousAttributes = attributes; 1126 attributes = handleAttributes(attributes); 1127 changed = tmp.toArray(); 1128 fireGraphChanged(JaWEGraphModel.this, this); 1130 } 1131 1132 public void putViews(GraphLayoutCache view, CellView[] views) { 1133 if (view != null && views != null) 1134 cellViews.put(view, views); 1135 } 1136 1137 public CellView[] getViews(GraphLayoutCache view) { 1138 return (CellView[]) cellViews.get(view); 1139 } 1140 1141 public String toString() { 1142 String s = new String (); 1143 if (inserted != null) { 1144 s += "Inserted:\n"; 1145 for (int i = 0; i < inserted.length; i++) 1146 s += " " + inserted[i] + "\n"; 1147 } else 1148 s += "None inserted\n"; 1149 if (removed != null) { 1150 s += "Removed:\n"; 1151 for (int i = 0; i < removed.length; i++) 1152 s += " " + removed[i] + "\n"; 1153 } else 1154 s += "None removed\n"; 1155 if (changed != null && changed.length > 0) { 1156 s += "Changed:\n"; 1157 for (int i = 0; i < changed.length; i++) 1158 s += " " + changed[i] + "\n"; 1159 } else 1160 s += "None changed\n"; 1161 if (parentMap != null) 1162 s += parentMap.toString(); 1163 else 1164 s += "No parent map\n"; 1165 return s; 1166 } 1167 1168 } 1170 1171 1174 public class GraphModelLayerEdit 1175 extends GraphLayoutCache.GraphViewLayerEdit 1176 implements GraphModelEvent.GraphModelChange { 1177 1178 protected Object [] parents; 1181 1182 1186 public GraphModelLayerEdit(Object [] cells, int layer) { 1187 super(JaWEGraphModel.this, cells, layer); 1188 Set par = new HashSet(); 1190 for (int i = 0; i < cells.length; i++) { 1191 if (cells[i] instanceof TreeNode) 1192 par.add(((TreeNode) cells[i]).getParent()); 1193 } 1194 parents = par.toArray(); 1195 } 1196 1197 public void execute () { 1198 super.execute(); 1199 } 1200 1201 1205 public Object getSource() { 1206 return JaWEGraphModel.this; 1207 } 1208 1209 1212 public Object [] getChanged() { 1213 return parents; 1214 } 1215 1216 1219 public Object [] getInserted() { 1220 return null; 1221 } 1222 1223 1226 public Object [] getRemoved() { 1227 return null; 1228 } 1229 1230 1233 public Map getPreviousAttributes() { 1234 return null; 1235 } 1236 public ConnectionSet getPreviousConnectionSet() { 1237 return null; 1238 } 1239 1240 public ParentMap getPreviousParentMap() { 1241 return null; 1242 } 1243 1255 public void addImplicitEdit(UndoableEdit edit) { 1256 } 1258 1259 1263 public CellView[] getViews(GraphLayoutCache view) { 1264 return null; 1265 } 1266 1267 1271 public void putViews(GraphLayoutCache view, CellView[] cellViews) { 1272 } 1274 1275 protected void updateListeners() { 1276 fireGraphChanged(JaWEGraphModel.this, this); 1277 } 1278 1279 1282 protected List getParentList(Object cell) { 1283 List list = null; 1284 if (cell instanceof DefaultMutableTreeNode) { 1285 Object parent = ((DefaultMutableTreeNode) cell).getParent(); 1286 if (parent instanceof DefaultGraphCell) 1287 list = ((DefaultGraphCell) parent).getChildren(); 1288 else 1289 list = roots; 1290 } 1291 return list; 1292 } 1293 1294 } 1295 1296 1300 1304 public static Object getSourceVertex(GraphModel model, Object edge) { 1305 if (model != null) 1306 return model.getParent(model.getSource(edge)); 1307 return null; 1308 } 1309 1310 1314 public static Object getTargetVertex(GraphModel model, Object edge) { 1315 if (model != null) 1316 return model.getParent(model.getTarget(edge)); 1317 return null; 1318 } 1319 1320 1323 public static Object [] getRoots(GraphModel model) { 1324 if (model instanceof JaWEGraphModel) 1325 return ((JaWEGraphModel) model).roots.toArray(); 1326 Object [] cells = null; 1327 if (model != null) { 1328 cells = new Object [model.getRootCount()]; 1329 for (int i = 0; i < cells.length; i++) 1330 cells[i] = model.getRootAt(i); 1331 } 1332 return cells; 1333 } 1334 1335 1338 public static Set getRootParticipants(GraphModel model) { 1339 Object [] roots=getRoots(model); 1340 if (roots==null || roots.length==0) return null; 1341 1342 Set rootDeps=new HashSet(); 1343 1344 for (int i=0; i<roots.length; i++) { 1346 if (roots[i] instanceof Participant) { 1347 rootDeps.add(roots[i]); 1348 } 1349 } 1350 1351 return rootDeps; 1352 } 1353 1354 1360 public static Set getEdges(GraphModel model, Object [] cells) { 1361 Set result = new HashSet(); 1362 Set allCells = getDescendants(model, cells); 1363 if (allCells != null) { 1364 Iterator it = allCells.iterator(); 1365 while (it.hasNext()) { 1366 Iterator edges = model.edges(it.next()); 1367 while (edges.hasNext()) 1368 result.add(edges.next()); 1369 } 1370 result.removeAll(allCells); 1371 } 1372 return result; 1373 } 1374 1375 1381 public static Set getDescendants(GraphModel model, Object [] cells) { 1382 if (cells != null) { 1383 Stack stack = new Stack(); 1384 for (int i = 0; i < cells.length; i++) 1385 stack.add(cells[i]); 1386 HashSet result = new HashSet(); 1387 while (!stack.isEmpty()) { 1388 Object tmp = stack.pop(); 1389 for (int i = 0; i < model.getChildCount(tmp); i++) 1390 stack.add(model.getChild(tmp, i)); 1391 if (tmp != null) 1392 result.add(tmp); 1393 } 1394 return result; 1395 } 1396 return null; 1397 } 1398 1399 public static List getDescendantList(GraphModel model, Object [] cells) { 1400 if (cells != null) { 1401 Stack stack = new Stack(); 1402 for (int i = cells.length-1; i >= 0; i--) 1403 stack.add(cells[i]); 1404 LinkedList result = new LinkedList(); 1405 while (!stack.isEmpty()) { 1406 Object tmp = stack.pop(); 1407 for (int i = model.getChildCount(tmp)-1; i >= 0; i--) 1408 stack.add(model.getChild(tmp, i)); 1409 if (tmp != null) 1410 result.add(tmp); 1411 } 1412 return result; 1413 } 1414 return null; 1415 } 1416 1417 1420 public static Set getAllCellsInModel (GraphModel model) { 1421 Set allCellsInModel=getDescendants(model,getRoots(model)); 1422 if (allCellsInModel == null || allCellsInModel.size()==0) { 1423 return null; 1424 } 1425 else { 1426 return allCellsInModel; 1427 } 1428 } 1429 1430 1433 public static Set getAllParticipantsInModel (GraphModel model) { 1434 if (!(model instanceof JaWEGraphModel)) return null; 1435 1436 Set allCellsInModel=getAllCellsInModel(model); 1437 if (allCellsInModel == null) { 1438 return null; 1439 } 1440 else { 1441 Set participants=new HashSet(); 1442 Iterator it=allCellsInModel.iterator(); 1443 while (it.hasNext()) { 1444 Object cell=it.next(); 1445 if (cell instanceof Participant) { 1446 participants.add(cell); 1447 } 1448 } 1449 if (participants.size()==0) { 1450 return null; 1451 } 1452 else { 1453 return participants; 1454 } 1455 } 1456 } 1457 1458 1462 public static Set getAllActivitiesInModel (GraphModel model) { 1463 if (!(model instanceof JaWEGraphModel)) return null; 1464 1465 Set allCellsInModel=getAllCellsInModel(model); 1466 if (allCellsInModel == null) { 1467 return null; 1468 } 1469 else { 1470 Set activities=new HashSet(); 1471 Iterator it=allCellsInModel.iterator(); 1472 while (it.hasNext()) { 1473 Object cell=it.next(); 1474 if (cell instanceof Activity) { 1475 activities.add(cell); 1476 } 1477 } 1478 if (activities.size()==0) { 1479 return null; 1480 } 1481 else { 1482 return activities; 1483 } 1484 } 1485 } 1486 1487 1490 public static Set getAllTransitionsInModel (GraphModel model) { 1491 if (!(model instanceof JaWEGraphModel)) return null; 1492 Object [] roots=getRoots(model); 1494 if (roots==null || roots.length==0) return null; 1495 1496 Set transitions=new HashSet(); 1497 1498 for (int i=0; i<roots.length; i++) { 1500 if (roots[i] instanceof Transition) { 1501 transitions.add(roots[i]); 1502 } 1503 } 1504 1505 return transitions; 1506 } 1507 1508 private void readObject(ObjectInputStream s) 1510 throws IOException, ClassNotFoundException { 1511 s.defaultReadObject(); 1512 listenerList = new EventListenerList(); 1513 emptyIterator = new EmptyIterator(); 1514 } 1515 1516 public static class EmptyIterator implements Iterator, Serializable { 1517 1518 public boolean hasNext() { 1519 return false; 1520 } 1521 1522 public Object next() { 1523 return null; 1524 } 1525 1526 public void remove() { 1527 } 1529 } 1530 1531} 1532 1533 1534 | Popular Tags |