1 package hero.client.grapheditor; 2 3 11 12 import hero.interfaces.Constants; 13 import hero.net.ProjectSession.StrutsNodeValue; 14 import hero.net.ProjectSession.BnIterationLightValue; 15 16 import java.awt.Color ; 17 import java.awt.Dimension ; 18 import java.awt.Font ; 19 import java.awt.Graphics ; 20 import java.awt.Graphics2D ; 21 import java.awt.Point ; 22 import java.awt.Rectangle ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.MouseAdapter ; 26 import java.awt.event.MouseEvent ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Observable ; 33 import java.util.Observer ; 34 import java.util.Set ; 35 36 import javax.swing.BorderFactory ; 37 import javax.swing.JPopupMenu ; 38 import javax.swing.JScrollPane ; 39 import javax.swing.JOptionPane ; 40 41 import com.jgraph.JGraph; 42 import com.jgraph.event.GraphModelEvent; 43 import com.jgraph.event.GraphModelListener; 44 import com.jgraph.event.GraphSelectionEvent; 45 import com.jgraph.event.GraphSelectionListener; 46 import com.jgraph.graph.BasicMarqueeHandler; 47 import com.jgraph.graph.CellView; 48 import com.jgraph.graph.ConnectionSet; 49 import com.jgraph.graph.DefaultEdge; 50 import com.jgraph.graph.DefaultGraphCell; 51 import com.jgraph.graph.DefaultGraphModel; 52 import com.jgraph.graph.DefaultPort; 53 import com.jgraph.graph.EdgeView; 54 import com.jgraph.graph.GraphConstants; 55 import com.jgraph.graph.GraphView; 56 import com.jgraph.graph.Port; 57 import com.jgraph.graph.PortView; 58 import com.jgraph.graph.VertexView; 59 import java.awt.Toolkit ; 60 import java.awt.Image ; 61 import javax.swing.ImageIcon ; 62 import java.net.URL ; 63 64 import java.util.Collection ; 65 66 import com.jgraph.graph.CellMapper; 67 68 public class WFGraph extends JGraph { 69 70 static java.util.ResourceBundle resource = java.util.ResourceBundle.getBundle("resources.Traduction"); 71 72 private DefaultGraphModel model; 73 private WFManager manager; 74 private WFPersistence persistence; 75 public Hashtable nodes; 76 private Hashtable edges; 77 private DefaultGraphCell sourceCell; 78 private boolean NEWEDGE = false; 79 private Touch layout2; 80 private boolean automaticLayout=false; 81 private LayoutThread expand; 82 private Image background; 83 84 public final static String imageBase = "images/"; 85 public final static ImageIcon icon = new ImageIcon (Thread.currentThread().getContextClassLoader().getResource(imageBase + "icon.png")); 86 87 public WFGraph ( final WFManager manager,WFPersistence persist){ 88 super(); 89 this.manager=manager; 90 this.persistence=persist; 91 nodes=new Hashtable (); 92 edges=new Hashtable (); 93 setMarqueeHandler(new WFMarqueeHandler()); 94 expand= new LayoutThread(this); 95 layout2 = new Touch(this); 96 WFListener listener =new WFListener(); 97 getView().addObserver(listener); 98 addGraphSelectionListener(listener); 99 getModel().addGraphModelListener(listener); 100 101 setDragEnabled(false); 102 setDropEnabled(false); 103 104 setSizeable(false); 106 setEditable(false); 107 setConnectable(false); 108 setDisconnectable(false); 109 setBendable(false); 110 setAntiAliased(true); 111 setCloneable(false); 112 113 115 setAutomaticLayout(true); 116 this.addMouseListener(new MouseAdapter () { 117 public void mousePressed(MouseEvent e){ 118 if (e.getClickCount() == 1) 119 { 120 try{ 121 if (!(persistence.soapclient.getProjectName()).equals(persistence.getProjectName())) 122 persistence.openProject(persistence.getProjectName()); 123 }catch(Exception openProject){} 124 } 125 if (e.getClickCount() == 2) 126 { 127 DefaultGraphCell myCell =(DefaultGraphCell) getFirstCellForLocation(e.getX(), e.getY()); 128 if (myCell != null && isVertex(myCell)) 129 { 130 Map props = myCell.getAttributes(); 131 String subProcess = (String )props.get("subProcess"); 132 if (subProcess.equals("no")) { 133 try { 134 BrowserControl.displayURL(java.lang.System.getProperty("bonita.host")+"/bonita/protected/Action.jsp?projectname="+persistence.getProjectName()+"&nodename="+(String )myCell.getUserObject()); 135 }catch(Exception ex){} 136 }else 137 { 138 try{ 139 Frame frame = new hero.client.grapheditor.Frame((String )props.get("name"),persistence.soapclient, true); 140 frame.setSize(new Dimension (500, 400)); 141 frame.setVisible(true); 142 frame.requestFocus(); 143 }catch(Exception exce){exce.printStackTrace();}; 144 } 145 } 146 if (myCell != null && isEdge(myCell)) 147 { 148 try { 149 Map props = myCell.getAttributes(); 150 manager.setEdgeCondition((String )props.get("name")); 151 }catch(Exception ex){ex.printStackTrace();} 152 } 153 } 154 } 155 }); 156 157 URL backgroundUrl = Thread.currentThread().getContextClassLoader().getResource(imageBase + "background.png"); 159 this.background = Toolkit.getDefaultToolkit().getImage(backgroundUrl); 160 setUI(new BonitaGraphUI()); 161 162 setBackground(Color.decode("#F7F7F7")); 164 165 } 166 167 public Image getBackgroundImage() { 168 return background; 169 } 170 171 public void setAutomaticLayout(boolean value){ 172 automaticLayout=value; 173 if (value){ 174 layout2.start(); 175 layout2.resetDamper(); 176 }else{ 177 layout2.stop(); 178 } 179 } 180 181 protected VertexView createVertexView(Object v, CellMapper cm) { 182 if (v instanceof BonitaActivityCell) { 183 return new BonitaActivityView(v, this, cm); 184 } 185 return super.createVertexView(v, cm); 186 } 187 188 public void addNewNode(String name, int state, int type){ 189 Map map; 190 191 if (!nodes.containsKey(name)){ 192 Hashtable attributes = new Hashtable (); 193 BonitaActivityCell vertex = new BonitaActivityCell(name); 195 Map vertexAttrib = GraphConstants.createMap(); 196 Map props = new HashMap (); 197 props.put("type", "node"); 198 props.put("state", new Integer (state).toString()); 199 props.put("name", name); 200 props.put("iterate", "false"); 201 202 203 if (type != Constants.Nd.SUB_PROCESS_NODE) 204 { 205 GraphConstants.setBounds(vertexAttrib, new Rectangle (20, 100, 85, 27)); 206 props.put("subProcess","no"); 207 } 208 else 209 { 210 GraphConstants.setBounds(vertexAttrib, new Rectangle (20, 100, 85, 33)); 211 props.put("subProcess","yes"); 212 } 213 GraphConstants.setBorder(vertexAttrib, BorderFactory.createRaisedBevelBorder()); 214 GraphConstants.setForeground(vertexAttrib, Color.white); 215 GraphConstants.setFontStyle(vertexAttrib, Font.BOLD); 216 GraphConstants.setFontSize(vertexAttrib,(float) 11); 217 GraphConstants.setOpaque(vertexAttrib, true); 218 219 221 vertex.setAttributes(props); 222 223 switch (state) { 224 case Constants.Nd.INITIAL: 225 GraphConstants.setBackground(vertexAttrib,(Color.lightGray).darker()); break; 226 case Constants.Nd.READY: 227 GraphConstants.setBackground(vertexAttrib,(Color.yellow).darker()); break; 228 case Constants.Nd.ANTICIPABLE: 229 GraphConstants.setBackground(vertexAttrib,(Color.green).darker()); break; 230 case Constants.Nd.ANTICIPATING: 231 GraphConstants.setBackground(vertexAttrib,(Color.magenta).darker()); break; 232 case Constants.Nd.EXECUTING: 233 GraphConstants.setBackground(vertexAttrib,(Color.red).darker()); break; 234 case Constants.Nd.EXECUTED: 235 GraphConstants.setBackground(vertexAttrib,(Color.orange).darker()); break; 236 case Constants.Nd.TERMINATED: 237 GraphConstants.setBackground(vertexAttrib,(Color.cyan).darker()); break; 238 case Constants.Nd.ANT_SUSPENDED: 239 GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break; 240 case Constants.Nd.EXEC_SUSPENDED: 241 GraphConstants.setBackground(vertexAttrib,(Color.gray).darker()); break; 242 case Constants.Nd.DEAD: 243 GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break; 244 } 245 246 GraphConstants.setSizeable(vertexAttrib,false); 247 248 attributes.put(vertex, vertexAttrib); 249 vertex.add(new DefaultPort()); 250 nodes.put(name,vertex); 251 Object [] cell = new Object []{vertex}; 252 model.insert(cell,null,null,attributes); 253 } 254 } 255 256 public void updateIterationNodes(String from, String to) 257 { 258 BonitaActivityCell vertexFrom = (BonitaActivityCell)nodes.get(from); 259 if(nodes.containsKey(from)){ 260 Hashtable attributes = new Hashtable (); 261 262 Map propsFrom = vertexFrom.getAttributes(); 263 propsFrom.put("iterate","from"); 264 attributes.put(nodes.get(from), propsFrom); 265 model.insert(null,null,null,propsFrom); 266 repaint(); 267 } 268 BonitaActivityCell vertexTo = (BonitaActivityCell)nodes.get(to); 269 if(nodes.containsKey(to)){ 270 Hashtable attributes = new Hashtable (); 271 272 Map propsTo = vertexTo.getAttributes(); 273 propsTo.put("iterate","to"); 274 attributes.put(nodes.get(to), propsTo); 275 model.insert(null,null,null,propsTo); 276 repaint(); 277 } 278 } 279 280 public void addNewEdge(DefaultGraphCell in, DefaultGraphCell out,String name){ 281 Hashtable attributes = new Hashtable (); 282 DefaultEdge e = new DefaultEdge(); 283 284 Map edgeAttrib = GraphConstants.createMap(); 285 GraphConstants.setLineBegin(edgeAttrib, GraphConstants.TECHNICAL); 286 GraphConstants.setBeginFill(edgeAttrib, true); 287 GraphConstants.setBeginSize(edgeAttrib, 8); 288 GraphConstants.setBendable(edgeAttrib,false); 289 GraphConstants.setDisconnectable(edgeAttrib,false); 290 attributes.put(e, edgeAttrib); 291 292 Map inn = in.getAttributes(); 293 Map outn = out.getAttributes(); 294 295 Map props = new HashMap (); 296 props.put("type", "edge"); 297 props.put("name", name); 298 props.put("nodein", inn.get("name")); 299 props.put("nodeout", outn.get("name")); 300 e.setAttributes(props); 301 302 ConnectionSet cs = new ConnectionSet(); 303 cs.connect(e,(DefaultPort)in.getChildAt(0),false); 304 cs.connect(e,(DefaultPort)out.getChildAt(0),true); 305 edges.put(name,e); 306 Object [] cell = new Object []{e}; 307 model.insert(cell,cs,null,attributes); 308 } 309 310 public void openGraph(String projectName) { 311 try { 312 newGraph(); 313 nodes.clear(); 314 edges.clear(); 315 316 Object [] nd = persistence.getNodes(); 317 for (int i = 0; i < nd.length; i++) { 318 String nodeName = (String ) nd[i]; 319 int state = persistence.getNodeState(nodeName); 320 int type = persistence.getNodeType(nodeName); 321 addNewNode(nodeName, state, type); 322 } 323 324 Object [] ed = persistence.getEdges(); 325 for (int i = 0; i < ed.length; i++) { 326 String nEdge = (String ) ed[i]; 327 String nodeIn = persistence.getEdgeInNode(nEdge); 328 String nodeOut = persistence.getEdgeOutNode(nEdge); 329 addNewEdge((DefaultGraphCell) nodes.get(nodeIn),(DefaultGraphCell) nodes.get(nodeOut),nEdge); 330 } 331 332 if (automaticLayout) 333 layout2.resetDamper(); 334 335 Collection iterations = persistence.getIterations(); 336 Iterator ite = iterations.iterator(); 337 while (ite.hasNext()) 338 { 339 BnIterationLightValue i = (BnIterationLightValue)ite.next(); 340 this.updateIterationNodes(i.getFromNode(),i.getToNode()); 341 } 342 343 355 356 } catch (Exception e1) {e1.printStackTrace(); 357 } 358 359 } 360 361 364 365 private ArrayList getItNodes(String prop) 366 throws Exception { 367 ArrayList nodes = new ArrayList (); 368 int index = prop.indexOf("iterate_"); 369 prop = prop.substring(index+8); 370 index = prop.indexOf("-"); 371 String to = prop.substring(index+1); 372 String from = prop.substring(0,index); 373 nodes.add(from); 374 nodes.add(to); 375 return(nodes); 376 } 377 378 public void newGraph(){ 379 nodes.clear(); 380 edges.clear(); 381 model = new DefaultGraphModel(); 382 setModel(model); 383 manager.setStatusBar(" "); 384 }; 385 386 387 public void setLayout1(){ 388 basicLayout(this); 389 } 390 public void setExpand(){ 391 expand.step(); 392 } 393 public void setZoom(int value){ 394 setScale(1); 395 } 396 public void zoomIn(){ 397 setScale(getScale()*2); 398 } 399 400 public void zoomOut(){ 401 if (getScale() > 0.01) 402 setScale(getScale()/2); 403 404 } 405 406 public void fitWindow(JScrollPane scrollPane){ 407 Dimension p = getPreferredSize(); 408 Dimension s = scrollPane.getViewport().getBounds().getSize(); 409 if (Math.abs (s.getWidth () - p.getWidth ()) < 410 Math.abs (s.getHeight () - p.getHeight ())) 411 setScale ((double) s.getWidth () / p.getWidth ()); 412 else setScale ((double) s.getHeight () / p.getHeight ()); 413 } 414 415 public void selectAll(){ 416 addSelectionCells(getRoots()); 417 418 } 419 420 public boolean isGroup(Object cell) { 421 CellView view = getView().getMapping(cell, false); 423 if (view != null) 424 return !view.isLeaf(); 425 return false; 426 } 427 428 429 430 public boolean isVertex(Object object) { 431 if (!(object instanceof Port) && !(object instanceof com.jgraph.graph.Edge)) 432 return !isGroup(object); 433 return false; 434 } 435 436 public boolean isEdge(Object object) { 437 return (object instanceof com.jgraph.graph.Edge); 438 } 439 440 public Object [] getSelectionNodes() { 441 Object [] tmp = getSelectionCells(); 442 Object [] all = DefaultGraphModel.getDescendants(getModel(), tmp).toArray(); 443 Object [] cells = getVertices(all); 444 Object [] elems = new Object [cells.length]; 445 for(int i=0;i<cells.length;i++){ 446 elems[i]=(String )(((DefaultGraphCell)cells[i]).getUserObject()); 447 } 448 return elems; 449 } 450 451 public boolean isSubProcess(String name) { 452 Object [] tmp = getSelectionCells(); 453 Object [] all = DefaultGraphModel.getDescendants(getModel(), tmp).toArray(); 454 Object [] cells = getVertices(all); 455 Object [] elems = new Object [cells.length]; 456 for(int i=0;i<cells.length;i++){ 457 if (name.equals((String )((DefaultGraphCell)cells[i]).getUserObject())) 458 { 459 DefaultGraphCell myCell = (DefaultGraphCell)cells[i]; 460 Map props = myCell.getAttributes(); 461 String subProcess = (String )props.get("subProcess"); 462 if (subProcess.equals("yes")) 463 return(true); 464 } 465 } 466 return false; 467 } 468 469 public Object [] getVertices(Object [] cells) { 470 if (cells != null) { 471 ArrayList result = new ArrayList (); 472 for (int i = 0; i < cells.length; i++) 473 if (isVertex(cells[i])) 474 result.add(cells[i]); 475 return result.toArray(); 476 } 477 return null; 478 } 479 480 public Object [] getSelectionEdges() { 481 Object [] all= getSelectionCells(); 482 Object [] cells = getEdges(all); 483 Object [] elems = new Object [cells.length]; 484 for(int i=0;i<cells.length;i++){ 485 elems[i]=(String )(((DefaultEdge)cells[i]).getAttributes().get("name")); 486 } 487 return elems; 488 489 } 490 491 public Object [] getEdges(Object [] cells) { 492 if (cells != null) { 493 ArrayList result = new ArrayList (); 494 for (int i = 0; i < cells.length; i++) 495 if (isEdge(cells[i])) 496 result.add(cells[i]); 497 return result.toArray(); 498 } 499 return null; 500 } 501 502 public Object [] getEdge(Object [] cells, String name) { 503 if (cells != null) { 504 ArrayList result = new ArrayList (); 505 for (int i = 0; i < cells.length; i++) 506 { 507 if (isEdge(cells[i])) 508 { 509 Map a = ((DefaultEdge)cells[i]).getAttributes(); 510 if (name.equals(a.get("name"))) 511 result.add(cells[i]); 512 } 513 } 514 return result.toArray(); 515 } 516 return null; 517 } 518 519 public void removeNodeEdges(Object [] cells, String name) { 520 if (cells != null) { 521 for (int i = 0; i < cells.length; i++) 522 { 523 if (isEdge(cells[i])) 524 { 525 Map a = ((DefaultEdge)cells[i]).getAttributes(); 526 if (name.equals(a.get("nodein")) || name.equals(a.get("nodeout"))) 527 deleteEdge((String )a.get("name")); 528 } 529 } 530 } 531 } 532 533 public void deleteNode(String node) throws Exception { 534 if(nodes.containsKey(node)){ 535 Object [] all=getRoots(); 536 removeNodeEdges(all,node); 537 getModel().remove(new Object []{nodes.get(node)}); 538 nodes.remove(node); 539 } 540 } 541 542 public void deleteEdge(String edge){ 543 if(edges.containsKey(edge)){ 544 Object [] all=getRoots(); 545 getModel().remove(getEdge(all,edge)); 546 edges.remove(edge); 547 } 548 } 549 550 public boolean existEdge(String name){ 551 return(edges.containsKey(name)); 552 } 553 554 public void restoreEdges() { 555 try { 556 Object [] all = getRoots(); 557 Object [] ed = getEdges(all); 558 559 edges.clear(); 560 if (ed != null) { 561 getModel().remove(ed); 562 Object [] edv = persistence.getEdges(); 563 for (int i = 0; i < edv.length; i++) { 564 String nEdge = (String ) ed[i]; 565 String nodeIn = persistence.getEdgeInNode(nEdge); 566 String nodeOut = persistence.getEdgeOutNode(nEdge); 567 addNewEdge( 568 (DefaultGraphCell) nodes.get(nodeIn), 569 (DefaultGraphCell) nodes.get(nodeOut), 570 nEdge); 571 } 572 } 573 } catch (Exception e1) { 574 JOptionPane.showMessageDialog( 575 null, 576 resource.getString("wfgraph.edgeerror")); 577 } 578 579 } 580 581 public void restoreEdge(String nEdge, String nodeIn, String nodeOut){ 582 583 addNewEdge((DefaultGraphCell)nodes.get(nodeIn),(DefaultGraphCell)nodes.get(nodeOut),nEdge); 584 } 585 586 587 588 public void changeNodeState(String node,int state){ 589 if(nodes.containsKey(node)){ 590 Hashtable attributes = new Hashtable (); 591 Map vertexAttrib = GraphConstants.createMap(); 592 switch (state) { 593 case Constants.Nd.INITIAL: 594 GraphConstants.setBackground(vertexAttrib,(Color.lightGray).darker()); break; 595 case Constants.Nd.READY: 596 GraphConstants.setBackground(vertexAttrib,(Color.yellow).darker()); break; 597 case Constants.Nd.ANTICIPABLE: 598 GraphConstants.setBackground(vertexAttrib,(Color.green).darker()); break; 599 case Constants.Nd.ANTICIPATING: 600 GraphConstants.setBackground(vertexAttrib,(Color.magenta).darker()); break; 601 case Constants.Nd.EXECUTING: 602 GraphConstants.setBackground(vertexAttrib,(Color.red).darker()); break; 603 case Constants.Nd.EXECUTED: 604 GraphConstants.setBackground(vertexAttrib,(Color.orange).darker()); break; 605 case Constants.Nd.TERMINATED: 606 GraphConstants.setBackground(vertexAttrib,(Color.cyan).darker()); break; 607 case Constants.Nd.ANT_SUSPENDED: 608 GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break; 609 case Constants.Nd.EXEC_SUSPENDED: 610 GraphConstants.setBackground(vertexAttrib,(Color.gray).darker()); break; 611 case Constants.Nd.DEAD: 612 GraphConstants.setBackground(vertexAttrib,(Color.blue).darker()); break; 613 } 614 attributes.put(nodes.get(node), vertexAttrib); 615 model.insert(null,null,null,attributes); 616 repaint(); 617 } 618 } 619 620 public Object getNeighbour(Object edge, Object vertex) { 621 Object source = getSourceVertex(edge); 622 if (vertex == source) 623 return getTargetVertex(edge); 624 else 625 return source; 626 } 627 628 public Object getSourceVertex(Object edge) { 629 Object sourcePort = graphModel.getSource(edge); 630 return graphModel.getParent(sourcePort); 631 } 632 633 public Object getTargetVertex(Object edge) { 634 Object targetPort = graphModel.getTarget(edge); 635 return graphModel.getParent(targetPort); 636 } 637 638 public CellView getSourceView(Object edge) { 639 Object source = getSourceVertex(edge); 640 return getView().getMapping(source, false); 641 } 642 643 public CellView getTargetView(Object edge) { 644 Object target = getTargetVertex(edge); 645 return getView().getMapping(target, false); 646 } 647 648 public Object [] getEdgesBetween(Object vertex1, Object vertex2) { 649 ArrayList result = new ArrayList (); 650 Set edges = DefaultGraphModel.getEdges(graphModel, new Object []{vertex1}); 651 Iterator it = edges.iterator(); 652 while (it.hasNext()) { 653 Object edge = it.next(); 654 Object source = getSourceVertex(edge); 655 Object target = getTargetVertex(edge); 656 if ((source == vertex1 && target == vertex2) || 657 (source == vertex2 && target == vertex1)) 658 result.add(edge); 659 } 660 return result.toArray(); 661 } 662 663 public String getToolTipText(MouseEvent e) { 665 try { 666 667 if (e != null) { 668 if (!(persistence.soapclient.getProjectName()).equals(persistence.getProjectName())) 670 persistence.openProject(persistence.getProjectName()); 671 Object c = getFirstCellForLocation(e.getX(), e.getY()); 672 if (c != null && isVertex(c)) { 673 String nodeName = convertValueToString(c); 674 StrutsNodeValue nl = persistence.getProjectNode(nodeName); 675 676 String s = 677 "<html><center><b>" 678 + nodeName 679 + "</b></center>" 680 + resource.getString("wfgraph.state") 681 + nl.getState() 682 + "<br>" 683 + resource.getString("wfgraph.role") 684 + nl.getRole() 685 + "<br>" 686 + resource.getString("wfgraph.type") 687 + nl.getType() 688 + "<br>" 689 + resource.getString("wfgraph.deadline") 690 + nl.getDeadline() 691 + "<br></html>"; 695 696 return s; 697 } 698 } 699 } catch (Exception e1) { 700 JOptionPane.showMessageDialog(null, resource.getString("wfgraph.errorgettool")); 701 } 702 703 return null; 704 } 705 706 public Object [] getAll() { 707 return getDescendants(getRoots()); 708 } 709 710 public void setAddEdgeState(boolean b){ 711 NEWEDGE = b; 712 } 713 714 public void toFront(String nodeName){ 715 Object [] cells = new Object []{nodes.get(nodeName)}; 716 if (cells != null) { 718 GraphView gv = getView(); 719 CellView[] views = gv.getMapping(cells); 720 gv.toFront(views); 721 } 722 } 723 724 public void toBack(String nodeName){ 725 Object [] cells = new Object []{nodes.get(nodeName)}; 726 if (cells != null) { 728 GraphView gv = getView(); 729 CellView[] views = gv.getMapping(cells); 730 gv.toBack(views); 731 } 732 } 733 734 735 public static java.awt.image.BufferedImage toImage(JGraph graph) { 736 Object [] cells = graph.getRoots(); 737 738 if (cells.length > 0) { 739 Rectangle bounds = graph.getCellBounds(cells); 740 graph.toScreen(bounds); 741 742 Dimension d = bounds.getSize(); 744 java.awt.image.BufferedImage img = new java.awt.image.BufferedImage (d.width+10, 745 d.height+10, java.awt.image.BufferedImage.TYPE_INT_RGB); 746 Graphics2D graphics = img.createGraphics(); 747 graphics.translate(-bounds.x+5, -bounds.y+5); 748 graph.paint(graphics); 749 750 return img; 751 } 752 return null; 753 } 754 755 private static String addNodeToVRML(Point p,Color c){ 756 String vrml="Transform{\n"+ 757 "translation "+p.getX()+" "+p.getY()+" 0\n"+ 758 " children[\n"+ 759 " Shape{\n"+ 760 " appearance Appearance\n"+ 761 " {\n"+ 762 " material Material\n"+ 763 " {"+ 764 " diffuseColor "+c.getRed()+" "+c.getGreen()+" "+c.getBlue()+"\n"+ 765 " shininess 0.01\n"+ 766 " }"+ 767 " }"+ 768 " geometry Box { size 25 25 25 }\n"+ 769 " }\n"+ 770 "]\n"+ 771 "}\n"; 772 return vrml; 773 } 774 private static String addEdgeToVRML(Point in,Point out){ 775 String vrml= "Shape{appearance Appearance { \n"+ 776 "material Material {\n"+ 777 " emissiveColor 1 1 1 }\n"+ 778 " }\n"+ 779 " geometry IndexedLineSet {\n"+ 780 " coord Coordinate {point [ "+in.getX()+" "+in.getY()+" 0\n"+ 781 " "+out.getX()+" "+out.getY()+" 0\n"+ 782 " ] }\n"+ 783 " coordIndex [0 1 ]\n"+ 784 " color Color { color [1 0 0, 0 1 0, 0 0 1]}\n"+ 785 " colorIndex [0 1 ]\n"+ 786 " colorPerVertex TRUE\n"+ 787 " }\n"+ 788 "}\n"; 789 790 return vrml; 791 } 792 793 public static String vrmlView(JGraph graph){ 794 CellView[] views = graph.getView().getRoots(); 795 int max= 0; 796 String vrml="#VRML V2.0 utf8\n Background{skyColor 0 0 0.5}\n"; 797 for(int i=0; i<views.length; i++){ 798 if (views[i] instanceof VertexView){ 799 Point p = ((VertexView)views[i]).getCenterPoint(); 800 Color c=(Color )((Map )((VertexView)views[i]).getAttributes()).get("backgroundColor"); 801 vrml= vrml+addNodeToVRML(p,c); 802 }else if (views[i] instanceof EdgeView){ 803 Point in=((EdgeView)views[i]).getPoint(0); 804 Point out=((EdgeView)views[i]).getPoint(((EdgeView)views[i]).getPointCount()-1); 805 vrml= vrml+addEdgeToVRML(in,out); 806 } 807 } 808 return vrml; 809 810 } 811 812 813 public static void basicLayout (JGraph graph){ 815 CellView[] views = graph.getView().getRoots(); 817 java.util.List vertices = new ArrayList (); 819 int max= 0; 820 for(int i=0; i<views.length; i++){ 822 if (views[i] instanceof VertexView){ 824 vertices.add(views[i]); 825 Rectangle b = views[i].getBounds(); 827 if(b !=null) 829 max = Math.max(Math.max(b.width, b.height), max); 830 } 831 } 832 int r= (int) Math.max(vertices.size()*max/Math.PI, 100); 834 double phi = 2*Math.PI/vertices.size(); 836 for (int i= 0; i<vertices.size(); i++){ 838 CellView view = (CellView) vertices.get(i); 840 Rectangle bounds = view.getBounds(); 842 if(bounds !=null) 844 bounds.setLocation(r+(int) (r*Math.sin(i*phi)), r+(int) (r*Math.cos(i*phi))); 845 } 846 graph.repaint(); 847 } 848 849 850 public class WFListener implements Observer ,GraphModelListener,GraphSelectionListener{ 851 public WFListener(){} 852 856 public void update(Observable o, Object obj){ 857 if(automaticLayout) 858 layout2.resetDamper(); 859 } 860 861 public void valueChanged(GraphSelectionEvent e) { 863 if (!isSelectionEmpty()){ 864 if(automaticLayout) 865 layout2.setDamper(0); 866 } 868 manager.updateStatus(); 869 870 } 871 872 public void graphChanged(GraphModelEvent e) { 874 if(automaticLayout) 875 layout2.resetDamper(); 876 } 877 } 878 879 882 public class WFMarqueeHandler extends BasicMarqueeHandler { 883 884 protected Point start, current; 885 886 protected Rectangle bounds; 887 888 protected PortView port, firstPort, lastPort; 889 890 protected DefaultGraphCell sourceCell,destCell; 891 892 protected boolean exception=false; 893 894 895 public boolean isForceMarqueeEvent(MouseEvent e) { 896 if ((e.getModifiers() & java.awt.event.InputEvent.BUTTON3_MASK) == 0) 897 return NEWEDGE || super.isForceMarqueeEvent(e); 898 return true; 899 } 900 901 public void mousePressed(MouseEvent event) { 902 903 if (NEWEDGE) { 904 start = snap(event.getPoint()); 905 sourceCell = (DefaultGraphCell)getFirstCellForLocation(event.getX(), event.getY()); 906 event.consume(); 907 }else{ 908 if ((event.getModifiers() & java.awt.event.InputEvent.BUTTON3_MASK) != 0) { 910 911 Object cell = getFirstCellForLocation(event.getX(), event.getY()); 912 JPopupMenu menu = WFContextMenu.popupMenu(manager.getFrame(), event.getPoint(), convertValueToString(cell),manager); 913 914 int x = event.getX(); 916 int y = event.getY(); 917 918 menu.show(manager.getGraph(), x, y); 919 } 920 } 921 922 super.mousePressed(event); 923 } 924 925 public void mouseDragged(MouseEvent event) { 926 if(NEWEDGE){ 927 Graphics g = getGraphics(); 928 Color bg = getBackground(); 929 Color fg = Color.black; 930 g.setColor(fg); 931 g.setXORMode(bg); 932 current = snap(event.getPoint()); 933 overlay(g); 934 } 935 super.mouseDragged(event); 936 } 937 938 public void mouseReleased(MouseEvent event) { 939 try { 940 if (NEWEDGE) { 941 destCell =(DefaultGraphCell) getFirstCellForLocation(event.getX(), event.getY()); 942 if ((sourceCell != null) && (destCell != null)) { 943 String name = persistence.addEdge((String ) sourceCell.getUserObject(),(String ) destCell.getUserObject()); 944 if (name != null) { 945 addNewEdge(sourceCell, destCell, name); 946 } else { 947 exception = true; 948 } 949 } 950 NEWEDGE = false; 951 sourceCell = null; 952 destCell = null; 953 } 954 955 super.mouseReleased(event); 956 if (exception) { 957 exception = false; 958 } 959 960 } catch (Exception e1) { 961 } 962 963 } 964 965 public void mouseMoved(MouseEvent event) { 966 super.mouseMoved(event); 967 } 968 969 public void overlay(Graphics g) { 970 try{ 971 super.overlay(g); 972 if(NEWEDGE){ 973 repaint(); 974 g.drawLine(start.x, start.y, current.x, current.y); 975 } 976 }catch(Exception e){} 977 } 978 } 979 980 public class LayoutThread extends javax.swing.Timer { 981 982 public LayoutThread(final WFGraph graph) { 983 super(200,new ActionListener () { 984 public void actionPerformed(ActionEvent e) { 985 int n = graph.getModel().getRootCount(); 986 Spring s = new Spring(10*n); 987 s.compute(graph); 988 graph.repaint(); 989 } 990 }); 991 } 992 993 public void step() { 994 super.fireActionPerformed(null); 995 } 996 997 } 998 999 1000} | Popular Tags |