1 12 13 package org.enhydra.jawe; 14 15 import org.enhydra.jawe.graph.*; 16 17 import org.jgraph.JGraph; 18 import org.jgraph.graph.*; 19 import org.jgraph.plaf.basic.*; 20 21 import java.awt.datatransfer.*; 22 23 import java.awt.*; 24 import java.awt.geom.*; 25 import java.awt.event.*; 26 import java.util.*; 27 import javax.swing.*; 28 import javax.swing.tree.*; 29 import javax.swing.undo.UndoableEdit ; 30 import org.enhydra.jawe.PackageEditor; 31 32 40 public class JaWEGraphUI extends BasicGraphUI { 41 42 43 protected boolean canMouseReleaseSelectCell=true; 44 45 49 protected Point insertionPoint=null; 50 51 54 public AbstractGraph getGraph() { 55 return (AbstractGraph)graph; 56 } 57 58 61 public void setInsertionPoint(Point p) { 62 insertionPoint=p; 63 } 64 65 68 protected void paintBackground(Graphics g) { 69 Rectangle pageBounds = new Rectangle(0,0,graph.getWidth(),graph.getHeight()); 70 71 if (graph.isGridVisible()) { 72 paintGrid(graph.getGridSize(),g,pageBounds); 73 } 74 } 75 76 80 protected boolean startEditing(Object cell, MouseEvent event) { 81 if (cell instanceof WorkflowElement) { 82 Window parentW=getGraph().getEditor().getWindow(); 83 ((WorkflowElement)cell).showPropertyDialog(parentW,getGraph()); 84 return true; 85 } 86 return false; 87 } 88 89 public void startEditingAtCell(JGraph graph,Object cell) { 91 if(cell!=null) startEditing(cell,null); 92 } 93 94 98 protected MouseListener createMouseListener() { 99 return new PEMouseHandler(); 100 } 101 102 105 public class PEMouseHandler extends MouseHandler { 106 107 public void mousePressed(MouseEvent e) { 108 handler = null; 109 if (!e.isConsumed() && graph.isEnabled()) { 110 graph.requestFocus(); 111 int s = graph.getTolerance(); 112 Rectangle2D r = graph.fromScreen( GraphConstants.createRect(e.getX()-s, e.getY()-s, 2*s, 2*s)); 115 focus = (focus != null && focus.intersects(graph.getGraphics(), r)) 116 ? focus : null; 117 Point2D point = graph.fromScreen(GraphConstants.createPoint(e.getPoint())); cell = graph.getNextViewAt(focus, point.getX(), point.getY()); 120 if (focus == null) { 122 cell = graph.getNextViewAt(focus, point.getX(), point.getY()); focus = cell; 124 } 125 else { 126 cell = focus; 127 } 128 129 if (focus != null) { 132 133 Object fcell=focus.getCell(); 134 if (fcell instanceof Activity){ 135 DefaultMutableTreeNode child=(DefaultMutableTreeNode)fcell; 136 DefaultMutableTreeNode parent=(DefaultMutableTreeNode) 137 child.getParent(); 138 if (parent != null) { 142 parent.insert(child,parent.getChildCount()-1); 143 ((ParticipantView)focus.getParentView()).refreshChildViews(); 147 } 148 } 149 150 } 152 153 cancelEditing(graph); 154 155 if (cell != null && !graph.isCellSelected(cell.getCell())) { 157 selectCellForEvent(cell.getCell(),e); 158 canMouseReleaseSelectCell=false; 159 } 160 161 if (e.getClickCount()==graph.getEditClickCount() && focus!=null && 162 !SwingUtilities.isRightMouseButton(e)) { 163 graph.startEditingAtCell(focus.getCell()); 166 e.consume(); 167 cell = null; 168 } 169 else if (!isToggleSelectionEvent(e) && !isForceMarqueeEvent(e) && 170 focus != null) { 171 if (handle != null) { 173 handle.mousePressed(e); 174 handler = handle; 175 } 176 } 177 else if (!isToggleSelectionEvent(e) || focus == null) { 179 if (marquee != null) { 180 marquee.mousePressed(e); 181 if (focus==null) { 183 Object [] cells=(new ArrayList()).toArray(); 184 selectCellsForEvent(cells, e); 185 } 186 handler = marquee; 187 } 188 } 189 190 } 191 } 192 193 public void mouseDragged(MouseEvent e) { 194 Object [] sc=graph.getSelectionCells(); 196 if (sc != null) { 197 for (int i=0; i<sc.length; i++) { 198 if (sc[i] instanceof Participant) { 199 e.consume(); 200 return; 201 } 202 } 203 } 204 205 try { 206 autoscroll(graph, e.getPoint()); 207 if (handler == marquee) { 208 marquee.mouseDragged(e); 209 } 210 else if (handler == null && !isEditing(graph) && focus != null) { 211 if (!graph.isCellSelected(focus.getCell())) { 212 cell = null; 215 } 216 if (handle != null) { 217 handle.mousePressed(e); 218 } 219 handler = handle; 220 } 221 if (handle != null && handler == handle) { 222 handle.mouseDragged(e); 223 } 224 } finally { 225 } 227 } 228 229 public void mouseReleased(MouseEvent e) { 230 try { 231 if (e!=null && !e.isConsumed()) { 232 if (handler == marquee) { 233 marquee.mouseReleased(e); 234 } 235 else if (handler == handle && handle != null) { 236 handle.mouseReleased(e); 237 } 238 239 if (!e.isConsumed() && focus != null && 241 !SwingUtilities.isRightMouseButton(e)) { 242 if (canMouseReleaseSelectCell) { 243 selectCellForEvent(focus.getCell(),e); 244 } 245 } 246 canMouseReleaseSelectCell=true; 247 } 248 } 249 finally { 250 insertionLocation = null; 251 handler = null; 252 cell = null; 253 } 254 } 255 256 259 public void mouseMoved(MouseEvent e) { 260 if (graph != null && graph.isEnabled()) { 261 if (marquee != null) 262 marquee.mouseMoved(e); 263 if (handle != null) 264 handle.mouseMoved(e); 265 if (!e.isConsumed()) 266 graph.setCursor(Cursor.getDefaultCursor()); 267 } 268 } 269 } 270 271 276 public CellHandle createHandle(GraphContext context) { 277 if (context != null && !context.isEmpty() && graph.isEnabled()) 278 return new PERootHandle(context); 279 return null; 280 } 281 282 286 public class PERootHandle extends RootHandle { 287 293 public PERootHandle(GraphContext ctx) { 294 super(ctx); 295 } 296 297 protected Point2D getInitialLocation(Object [] cells) { 298 try { 299 return super.getInitialLocation(cells); 300 } catch (Throwable thr){ 301 return null; 302 } 303 } 304 305 public void mouseReleased(MouseEvent event) { 306 if (event != null && !event.isConsumed()) { 307 if (activeHandle != null) { 308 activeHandle.mouseReleased(event); 309 activeHandle = null; 310 } else if (isMoving&& !event.getPoint().equals(start)) { 311 if (cachedBounds != null) { 312 int dx = event.getX()-(int)start.getX(); int dy = event.getY()-(int)start.getY(); Point2D tmp = graph.fromScreen(GraphConstants.createPoint(dx, dy)); GraphLayoutCache.translateViews(views, tmp.getX(), tmp.getY()); } 317 318 319 if (JaWEConfig.getInstance().getGridStatus() && 321 views[0] instanceof ActivityView ){ 322 ActivityView view = (ActivityView)views[0]; 323 Rectangle2D rect = view.getBounds(); int dx = 0; 325 int dy = 0; 326 327 int gridsize = JaWEConfig.getInstance().getGridSize(); 328 int deltax = (int)rect.getX() % gridsize; 329 int deltay = (int)rect.getY() % gridsize; 330 int halfgrid = gridsize / 2; 331 if (deltax > halfgrid){ 332 dx += (gridsize - deltax); 333 } else { 334 dx -= deltax; 335 } 336 if (deltay > halfgrid){ 337 dy += (gridsize - deltay); 338 } else { 339 dy -= deltay; 340 } 341 Point2D tmp = graph.fromScreen(GraphConstants.createPoint(dx, dy)); GraphLayoutCache.translateViews(views, tmp.getX(), tmp.getY()); } 344 346 347 CellView[] all = graphLayoutCache.getAllDescendants(views); 348 349 if (event.isControlDown() && graph.isCloneable()) { Object [] cells = graph.getDescendants(context.getCells()); 351 ConnectionSet cs = ConnectionSet.create(graphModel,cells,false); 352 cs.addConnections(all); 353 Map propertyMap = GraphConstants.createAttributes(all, null); 354 insertCells(context.getCells(), propertyMap, cs, true, 0, 0); 355 } else if (graph.isMoveable()){ Map propertyMap = GraphConstants.createAttributes(all,null); 360 WorkflowManager dm=getGraph().getWorkflowManager(); 361 dm.moveCellsAndArrangeParticipants(propertyMap); 362 367 } 368 event.consume(); 369 } 370 } 371 start = null; 372 } 373 374 } 375 376 379 protected Observer createGraphViewObserver() { 380 return new PEGraphViewObserver(); 381 } 382 383 387 public class PEGraphViewObserver extends GraphViewObserver { 388 389 public void update(Observable o, Object arg) { 390 super.update(o,arg); 391 canMouseReleaseSelectCell=false; 393 } 394 } 395 396 397 401 protected TransferHandler createTransferHandler() { 402 return new PETransferHandler(); 403 } 404 405 412 public class PETransferHandler extends TransferHandler { 413 protected boolean isCut = false; 414 415 416 protected Object out, in; 417 418 419 protected int inCount = 0; 420 421 public boolean canImport(JComponent comp, DataFlavor[] flavors) { 422 return true; 423 } 424 425 protected final Transferable createTransferable(JComponent c) { 427 if (c instanceof JGraph) { 428 JGraph graph = (JGraph) c; 429 430 Object [] cells = graphLayoutCache.order(graph.getSelectionCells()); 431 if (cells != null && cells.length > 0) { 432 boolean dispMessage=false; 435 Set cellsToCopy=new HashSet(); 436 for (int i=0; i<cells.length; i++) { 437 if (!(cells[i] instanceof Participant) && 438 !(cells[i] instanceof Start) && 440 !(cells[i] instanceof End) && 441 !(cells[i] instanceof BlockActivity)) { 442 cellsToCopy.add(cells[i]); 443 } else { 444 if (!(cells[i] instanceof Transition)) dispMessage=true; 445 } 446 } 447 HashSet cellsToRemove = new HashSet(); 449 for (Iterator it = cellsToCopy.iterator(); it.hasNext();) { 450 Object obj = (Object ) it.next(); 451 if (obj instanceof Transition){ 452 boolean sourceOK = false, targetOK = false; 453 454 for (Iterator it2 = cellsToCopy.iterator(); it2.hasNext();) { 455 Object obj2 = (Object ) it2.next(); 456 if (obj2 instanceof Activity){ 457 if ( ((Transition)obj).getSourceActivity() == obj2 ){ 458 sourceOK = true; 459 } 460 if ( ((Transition)obj).getTargetActivity() == obj2 ){ 461 targetOK = true; 462 } 463 } 464 } 465 466 if (!sourceOK || !targetOK){ 467 cellsToRemove.add(obj); 468 } 469 } 470 } 471 cellsToCopy.removeAll(cellsToRemove); 472 473 if (cellsToCopy.size() != cells.length) { 474 if (dispMessage) { 476 String appTitle=JaWE.getAppTitle(); 477 JOptionPane.showMessageDialog(getGraph(). 478 getEditor().getWindow(), 479 ResourceManager.getLanguageDependentString( 480 "WarningOnlyGenericSubflowAndRouteActivitiesCanBeCopied"), 481 appTitle,JOptionPane.WARNING_MESSAGE); 482 } 483 cells=cellsToCopy.toArray(); 484 if (cells.length == 0) return null; 485 } 486 out = cells; 487 ParentMap pm = ParentMap.create(graphModel, cells, false, true); 488 ConnectionSet cs = ConnectionSet.create(graphModel, cells, false); 491 Map viewAttributes = GraphConstants.createAttributes(cells,graphLayoutCache); 493 Rectangle2D bounds = graph.getCellBounds((Object [])out); return create(graph, cells, viewAttributes, bounds, cs, pm); 495 496 } 497 } 498 return null; 499 } 500 501 protected GraphTransferable create( 502 JGraph graph, 503 Object [] cells, 504 Map viewAttributes, 505 Rectangle2D bounds, ConnectionSet cs, 507 ParentMap pm) { 508 return new GraphTransferable(cells, viewAttributes, bounds, cs, pm); 509 } 510 511 protected void exportDone( 512 JComponent comp, 513 Transferable data, 514 int action) { 515 if (comp instanceof JGraph && data instanceof GraphTransferable) { 516 if (action == TransferHandler.MOVE) { 517 JGraph graph = (JGraph) comp; 518 Object [] cells = ((GraphTransferable) data).getCells(); 519 removeCells(graph,cells); } 522 } 523 updateHandle(); 524 setInsertionLocation(null); 525 } 526 527 public void exportToClipboard(JComponent compo, Clipboard clip, int action) { 528 isCut = (action == TransferHandler.MOVE); 529 super.exportToClipboard(compo, clip, action); 530 } 531 532 public int getSourceActions(JComponent c) { 533 return COPY_OR_MOVE; 534 } 535 536 public boolean importData(JComponent comp, Transferable t) { 538 try { 539 if (comp instanceof JGraph) { 540 JGraph graph = (JGraph)comp; 541 if (t.isDataFlavorSupported(GraphTransferable.dataFlavor)) { 542 Object obj = t.getTransferData (GraphTransferable.dataFlavor); 543 GraphTransferable gt = (GraphTransferable)obj; 544 if (out != gt.getCells() || insertionLocation == null) 545 return handleInsert(graph, gt); 546 return true; 547 } 548 } 549 } catch (Exception exception) { 550 } finally { 551 isCut = false; 552 } 553 return false; 554 } 555 556 559 protected boolean handleInsert(JGraph graph, GraphTransferable t) { 560 Object [] cells = t.getCells(); 561 boolean c = out != cells || !isCut; 562 if (in != cells) { 563 inCount = (c) ? 1 : 0; 564 } 565 in = cells; 566 int dx = inCount * (int)graph.getGridSize(); int dy = inCount * (int)graph.getGridSize(); 569 Map attributeMap=null; 570 571 if ((insertionLocation != null) || (insertionPoint != null)) { 573 Point p = t.getBounds().getBounds().getLocation(); Point insPoint=insertionLocation; 575 if (insPoint == null) { 576 insPoint=insertionPoint; 577 } 578 579 if (JaWEConfig.getInstance().getGridStatus()){ 581 int gridsize = JaWEConfig.getInstance().getGridSize(); 582 int deltax = insPoint.x % gridsize; 583 int deltay = insPoint.y % gridsize; 584 int halfgrid = gridsize / 2; 585 if (deltax > halfgrid){ 586 insPoint.x += (gridsize - deltax); 587 } else { 588 insPoint.x -= deltax; 589 } 590 if (deltay > halfgrid){ 591 insPoint.y += (gridsize - deltay); 592 } else { 593 insPoint.y -= deltay; 594 } 595 } 596 598 dx = insPoint.x - p.x; 599 dy = insPoint.y - p.y; 600 601 if (insertionPoint != null) { 602 attributeMap=t.getAttributeMap(); 604 GraphConstants.translate(attributeMap.values(),dx,dy); 605 606 Rectangle rect = t.getBounds().getBounds(); rect.translate(dx,dy); t.getBounds().setRect(rect); 611 dx=0; dy=0; 613 inCount=0; 615 } 616 } 617 inCount++; 618 if (cells == out || graph.isDropEnabled()) { 619 if (attributeMap==null) attributeMap=t.getAttributeMap(); 620 insertCells(cells,attributeMap,t.getConnectionSet(),c,dx,dy); 621 return true; 622 } 623 return false; 624 } 625 626 protected void removeCells(JGraph graph, Object [] cells) { 627 WorkflowManager dm=getGraph().getWorkflowManager(); 628 dm.removeCellsAndArrangeParticipants(cells); 629 } 630 } 631 632 642 public void insertCells(Object [] cells, Map viewAttributeMap, 643 ConnectionSet cs, boolean clone, int dx, int dy) { 644 645 Object [] insert; 646 WorkflowManager wm=getGraph().getWorkflowManager(); 648 649 Object wp=getGraph().getPropertyObject(); 651 652 String appTitle=JaWE.getAppTitle(); 653 654 Set cellsToCloneOrPaste = new HashSet(); 656 boolean dispMessage=false; 657 for (int i=0; i<cells.length; i++) { 658 if (!(cells[i] instanceof Participant) && 659 !(cells[i] instanceof Start) && 661 !(cells[i] instanceof End) && 662 !(cells[i] instanceof BlockActivity)) { 663 if ( cells[i] instanceof Activity && ((org.enhydra.jawe.xml.elements.Activity) 667 ((Activity)cells[i]).getUserObject()).getOwnerProcess()!=wp) { 668 JOptionPane.showMessageDialog(getGraph().getEditor().getWindow(), 669 ResourceManager.getLanguageDependentString("ErrorCrossGraphCopyIsForbidden"), 670 appTitle,JOptionPane.ERROR_MESSAGE); 671 return; 672 } else { 673 cellsToCloneOrPaste.add(cells[i]); 674 } 675 676 if (cells[i] instanceof Activity && 678 (((Activity)cells[i]).getChildren() == null || 679 ((Activity)cells[i]).getPort() == null)){ 680 DefaultPort port = new DefaultPort("Center"); 681 ((Activity)cells[i]).add(port); 682 } 683 685 } else { 686 if (!(cells[i] instanceof Transition)) dispMessage=true; 687 } 688 } 689 if (cellsToCloneOrPaste.size() != cells.length) { 690 if (dispMessage){ 693 JOptionPane.showMessageDialog(getGraph().getEditor().getWindow(), 694 ResourceManager.getLanguageDependentString( 695 "WarningOnlyGenericSubflowAndRouteActivitiesCanBePastedOrCloned"), 696 appTitle,JOptionPane.WARNING_MESSAGE); 697 } 698 cells=cellsToCloneOrPaste.toArray(); 699 } 700 701 if (cellsToCloneOrPaste.size()>0) { 702 Map cellMap = graph.cloneCells(cells); 705 706 viewAttributeMap = GraphConstants.replaceKeys(cellMap, viewAttributeMap); 707 708 cs = new ConnectionSet(); 710 if (clone) { 714 Iterator it=viewAttributeMap.entrySet().iterator(); 715 while (it.hasNext()) { 716 Map.Entry entry = (Map.Entry)it.next(); 717 Object userObject=((DefaultMutableTreeNode)entry.getKey()). 718 getUserObject(); 719 Map map=(Map)entry.getValue(); 720 GraphConstants.setValue(map,userObject); 721 722 if (entry.getKey() instanceof org.enhydra.jawe.graph.Transition){ 724 Transition tr = (Transition)entry.getKey(); 725 org.enhydra.jawe.xml.elements.Transition trUo = 726 (org.enhydra.jawe.xml.elements.Transition)tr.getUserObject(); 727 Activity source = null, target = null; 728 for (Iterator it1 = cellMap.entrySet().iterator(); it1.hasNext();) { 729 Map.Entry en1 = (Map.Entry)it1.next(); 730 if (en1.getKey() instanceof Activity){ 731 org.enhydra.jawe.xml.elements.Activity actUo = 732 (org.enhydra.jawe.xml.elements.Activity)((Activity)en1.getKey()).getUserObject(); 733 if (trUo.getFrom() == actUo){ 734 source = (Activity)en1.getValue(); 735 tr.setSource(null); 736 trUo.setFrom( 737 (org.enhydra.jawe.xml.elements.Activity)source.getUserObject() ); 738 } 739 if (trUo.getTo() == actUo){ 740 target = (Activity)en1.getValue(); 741 tr.setTarget(null); 742 trUo.setTo( 743 (org.enhydra.jawe.xml.elements.Activity)target.getUserObject()); 744 } 745 } 746 } 747 if (source != null && target != null){ 748 cs.connect(tr,source.getPort(),target.getPort()); 749 } 750 } 751 } 752 } 753 754 insert = new Object [cells.length]; 756 757 for (int i = 0; i < cells.length; i++) { 758 insert[i] = cellMap.get(cells[i]); 759 } 760 761 if (dx!=0 || dy!=0) { 763 GraphConstants.translate(viewAttributeMap.values(), dx, dy); 764 } 765 766 wm.insertCellsAndArrangeParticipants(insert,viewAttributeMap, cs); 770 } 771 } 772 773 } 774 775 | Popular Tags |