1 26 package org.objectweb.util.explorer.swing.graph; 27 28 import java.awt.Dimension ; 29 import java.awt.geom.Rectangle2D ; 30 import java.util.ArrayList ; 31 import java.util.Hashtable ; 32 import java.util.Map ; 33 import java.util.Vector ; 34 35 import org.jgraph.JGraph; 36 import org.jgraph.graph.AttributeMap; 37 import org.jgraph.graph.CellMapper; 38 import org.jgraph.graph.CellView; 39 import org.jgraph.graph.ConnectionSet; 40 import org.jgraph.graph.DefaultEdge; 41 import org.jgraph.graph.DefaultGraphModel; 42 import org.jgraph.graph.Edge; 43 import org.jgraph.graph.GraphConstants; 44 import org.jgraph.graph.GraphModel; 45 import org.jgraph.graph.PortView; 46 import org.jgraph.graph.VertexView; 47 48 52 public class Graph extends JGraph { 53 54 55 private ConnectionSet cs = new ConnectionSet(); 56 57 private GraphModel model = new DefaultGraphModel(); 58 59 60 private static PortGraphicsInterface pg; 61 62 63 private static VertexGraphicsInterface vg; 64 65 66 private Map attributes; 67 68 69 private CompositeVertex superComponent; 70 71 72 private ArrayList primitiveVerteces; 73 74 78 private ArrayList primitiveEdges; 79 80 84 private Vector vertecesPosition = new Vector (); 85 86 90 public Graph(GraphModel model) { 91 super(model); 92 attributes = new Hashtable (); 93 this.model = model; 94 primitiveVerteces = new ArrayList (); 95 primitiveEdges = new ArrayList (); 96 } 97 98 104 public Graph(GraphModel model, PortGraphicsInterface portGraphics, 105 VertexGraphicsInterface vertexGraphics) { 106 super(model); 107 this.model = model; 108 attributes = new Hashtable (); 109 cs = new ConnectionSet(); 110 setPortGraphics(portGraphics); 111 setVertexGraphics(vertexGraphics); 112 primitiveVerteces = new ArrayList (); 113 primitiveEdges = new ArrayList (); 114 } 115 116 123 public void insertIntoModel(boolean isPrimitif) { 124 Object [] cells; 125 if (!isPrimitif) { 126 ArrayList edges = superComponent.getBinding(); 127 ArrayList verteces = superComponent.getVerteces(); 128 cells = new Object [verteces.size() + edges.size()]; 129 130 int j = 0; 131 for (j = 0; j < verteces.size(); j++) 132 cells[j] = verteces.get(j); 133 134 for (int k = j; k < j + edges.size(); k++) 135 cells[k] = edges.get(k - j); 136 } else { 137 ArrayList edges = primitiveEdges; 138 ArrayList verteces = primitiveVerteces; 139 cells = new Object [verteces.size() + edges.size()]; 140 141 int j = 0; 142 for (j = 0; j < verteces.size(); j++) 143 cells[j] = verteces.get(j); 144 145 for (int k = j; k < j + edges.size(); k++) 146 cells[k] = edges.get(k - j); 147 } 148 model.insert(cells, attributes, cs, null, null); 149 } 150 151 161 private void updateEmptyVertecesPosition(MyPort sourcePort, 162 MyPort targetPort, boolean isCompositeEdge) { 163 Object source = sourcePort.getParent(); 164 Object target = targetPort.getParent(); 165 if (isCompositeEdge) { 166 if ((source instanceof CompositeVertex) 167 && (((CompositeVertex) source).isOpen())) { 168 if (!((target instanceof CompositeVertex) && (((CompositeVertex) target) 169 .isOpen()))) { 170 vertecesPosition.add(0, new Vector ()); 171 Vector newLine = (Vector ) vertecesPosition.get(0); 172 newLine.setSize(1); 173 newLine.add(0, target); 174 } 175 } else if (((target instanceof CompositeVertex) && (((CompositeVertex) target) 176 .isOpen())) 177 && (!((source instanceof CompositeVertex) && (((CompositeVertex) source) 178 .isOpen())))) { 179 vertecesPosition.add(0, new Vector ()); 180 Vector newLine = (Vector ) vertecesPosition.get(0); 181 newLine.setSize(1); 182 newLine.add(0, source); 183 } 184 } else if (!(sourcePort.getType().equals(PortType.CONTROLLER_PORT))) { 185 vertecesPosition.add(0, new Vector ()); 187 Vector newLine = (Vector ) vertecesPosition.get(0); 188 newLine.setSize(2); 189 newLine.add(0, source); 191 newLine.add(1, target); 192 } else { 193 vertecesPosition.add(0, new Vector ()); 195 Vector newLine1 = (Vector ) vertecesPosition.get(0); 196 newLine1.setSize(1); 197 vertecesPosition.add(1, new Vector ()); 198 Vector newLine2 = (Vector ) vertecesPosition.get(1); 199 newLine2.setSize(2); 200 newLine1.add(0, source); 202 newLine2.add(1, target); 203 } 204 } 205 206 214 private void updateVertecesPositionWithCompositeEdge(MyPort sourcePort, 215 MyPort targetPort) { 216 Object source = sourcePort.getParent(); 217 Object target = targetPort.getParent(); 218 if ((source instanceof CompositeVertex) 219 && (((CompositeVertex) source).isOpen())) { 220 if (!((target instanceof CompositeVertex) && (((CompositeVertex) target) 221 .isOpen()))) { 222 int index = sourcePort.getNumber(); 223 if (!(haveTarget(target))) { 224 if (vertecesPosition.size() > index) { 225 Vector targetLine = (Vector ) vertecesPosition 226 .get(index); 227 if (targetLine.get(0) == null) 228 targetLine.setElementAt(target, 0); 229 else 230 targetLine.add(0, target); 231 } else { 232 int oldSize = vertecesPosition.size(); 233 int newSize = index + 1; 234 vertecesPosition.setSize(index + 1); 235 for (int i = oldSize; i < vertecesPosition.size(); i++) { 236 Vector newLine = new Vector (); 237 newLine.setSize(1); 238 vertecesPosition.setElementAt(newLine, i); 239 } 240 ((Vector ) vertecesPosition.get(index)).setElementAt( 241 target, 0); 242 } 243 } 244 } 245 } else if ((target instanceof CompositeVertex) 246 && (((CompositeVertex) target).isOpen())) { 247 if (!((target instanceof CompositeVertex) && (((CompositeVertex) target) 248 .isOpen()))) { 249 int index = targetPort.getNumber(); 250 if (vertecesPosition.size() > index) { 251 Vector sourceLine = (Vector ) vertecesPosition.get(index); 252 if (sourceLine.get(sourceLine.size()) == null) 253 sourceLine.setElementAt(source, sourceLine.size()); 254 else { 255 int oldSize = sourceLine.size(); 256 sourceLine.setSize(oldSize + 1); 257 sourceLine.add(oldSize, source); 258 } 259 } else { 260 int oldSize = vertecesPosition.size(); 261 vertecesPosition.setSize(index + 1); 262 for (int i = 0; i < vertecesPosition.size(); i++) { 263 Vector newLine = new Vector (); 264 newLine.setSize(1); 265 vertecesPosition.add(i, newLine); 266 } 267 Vector sourceLine = (Vector ) vertecesPosition.get(index); 268 sourceLine.setSize(getNbColumn() + 1); 269 sourceLine.add(getNbColumn(), source); 270 } 271 } 272 } 273 274 } 275 276 279 private int getNbColumn() { 280 int nb = 0; 281 for (int i = 0; i < vertecesPosition.size(); i++) { 282 Vector line = (Vector ) vertecesPosition.get(i); 283 int lineSize = 0; 284 for (int j = 0; j < line.size(); j++) { 285 if (line.get(j) != null) 286 lineSize++; 287 } 288 if (lineSize > nb) { 289 nb = lineSize; 290 } 291 } 292 return nb; 293 } 294 295 299 private boolean haveSource(Object source) { 300 int haveSource = 0; 301 for (int i = 0; i < vertecesPosition.size(); i++) { 302 if (((Vector ) vertecesPosition.get(i)).contains(source)) 303 haveSource++; 304 } 305 return (haveSource != 0); 306 } 307 308 312 private boolean haveTarget(Object target) { 313 int haveTarget = 0; 314 for (int i = 0; i < vertecesPosition.size(); i++) { 315 if (((Vector ) vertecesPosition.get(i)).contains(target)) 316 haveTarget++; 317 } 318 return (haveTarget != 0); 319 } 320 321 329 private void addTargetAfter(Vector vertecesPosition, int sourceIndexLine, 330 int sourceIndex, Object target) { 331 int add = 0; 332 for (int i = sourceIndexLine; i < vertecesPosition.size(); i++) { 333 Vector line = (Vector ) vertecesPosition.get(i); 334 if (line.size() < sourceIndex + 2) { 335 line.setSize(sourceIndex + 2); 336 line.add(sourceIndex + 1, target); 337 add++; 338 } else if (line.get(sourceIndex + 1) == null) { 339 line.setElementAt(target, sourceIndex + 1); 340 add++; 341 } 342 } 343 if (add == 0) { 344 vertecesPosition.setSize(vertecesPosition.size() + 1); 345 vertecesPosition.setElementAt(new Vector (), 346 vertecesPosition.size() - 1); 347 Vector targetLine = (Vector ) vertecesPosition.get(vertecesPosition 348 .size() - 1); 349 targetLine.setSize(sourceIndex + 2); 350 targetLine.setElementAt(target, sourceIndex + 1); 351 } 352 } 353 354 362 private void addSourceBefore(Vector vertecesPosition, int targetIndexLine, 363 int targetIndex, Object source) { 364 int add = 0; 365 for (int i = targetIndexLine; i < vertecesPosition.size(); i++) { 366 Vector line = (Vector ) vertecesPosition.get(i); 367 if (line.size() < targetIndex) { 368 line.setSize(targetIndex); 369 line.add(line.size() - 1, source); 370 add++; 371 } else if (line.get(targetIndex - 1) == null) { 372 ((Vector ) vertecesPosition.get(i)).setElementAt(source, 373 targetIndex - 1); 374 add++; 375 } 376 } 377 if (add == 0) { 378 vertecesPosition.setSize(vertecesPosition.size() + 1); 379 vertecesPosition.setElementAt(new Vector (), 380 vertecesPosition.size() - 1); 381 Vector sourceLine = (Vector ) vertecesPosition.get(vertecesPosition 382 .size() - 1); 383 sourceLine.setSize(targetIndex); 384 sourceLine.add(targetIndex - 1, source); 385 } 386 } 387 388 393 private int getIndexLineOf(Vector vertecesPosition, Object vertex) { 394 int index = 0; 395 for (int i = 0; i < vertecesPosition.size(); i++) { 396 if (((Vector ) vertecesPosition.get(i)).contains(vertex)) 397 index = i; 398 } 399 return index; 400 } 401 402 410 public void updateVertecesPosition(Object edge, MyPort sourcePort, 411 MyPort targetPort, boolean isCompositeEdge) { 412 if ((sourcePort != null) && (targetPort != null)) { 413 Object source = sourcePort.getParent(); 414 Object target = targetPort.getParent(); 415 416 if (vertecesPosition.isEmpty()) { 417 updateEmptyVertecesPosition(sourcePort, targetPort, 418 isCompositeEdge); 419 } else { 420 if (isCompositeEdge) { 421 updateVertecesPositionWithCompositeEdge(sourcePort, 422 targetPort); 423 } else { 424 if (!(sourcePort.getType().equals(PortType.CONTROLLER_PORT))) { 425 if ((!haveSource(source)) && (!haveTarget(target))) { 430 int newIndexLine = vertecesPosition.size(); 432 vertecesPosition.add(newIndexLine, new Vector ()); 433 Vector newLine = (Vector ) vertecesPosition 434 .get(newIndexLine); 435 newLine.setSize(2); 436 newLine.add(0, source); 438 newLine.add(1, target); 439 } else if (!haveSource(source)) { 440 int targetIndexLine = getIndexLineOf( 441 vertecesPosition, target); 442 Vector targetLine = (Vector ) vertecesPosition 443 .get(targetIndexLine); 444 int targetIndex = targetLine.indexOf(target); 445 if (targetIndex == 0) { 446 targetLine.add(0, source); 447 } else if (targetLine.get(targetIndex - 1) == null) { 448 targetLine 449 .setElementAt(source, targetIndex - 1); 450 } else { 451 addSourceBefore(vertecesPosition, 452 targetIndexLine, targetIndex, source); 453 } 454 } else if (!(haveTarget(target))) { 455 int sourceIndexLine = getIndexLineOf( 456 vertecesPosition, source); 457 Vector sourceLine = (Vector ) vertecesPosition 458 .get(sourceIndexLine); 459 int sourceIndex = sourceLine.indexOf(source); 460 if (sourceIndex == sourceLine.size()) { 461 sourceLine.setSize(sourceIndex + 2); 462 sourceLine.add(sourceLine.size() - 1, target); 463 } else if ((sourceLine.size() > sourceIndex + 1) 464 && (sourceLine.get(sourceIndex + 1) == null)) { 465 sourceLine.add(sourceIndex + 1, target); 466 } else { 467 addTargetAfter(vertecesPosition, 468 sourceIndexLine, sourceIndex, target); 469 } 470 } 471 474 } else { 475 if ((!(haveTarget(source))) && (!(haveTarget(target)))) { 476 int newIndexLine1 = vertecesPosition.size(); 478 vertecesPosition.add(newIndexLine1, new Vector ()); 479 Vector newLine1 = (Vector ) vertecesPosition 480 .get(newIndexLine1); 481 int newIndexLine2 = vertecesPosition.size(); 482 vertecesPosition.add(newIndexLine2, new Vector ()); 483 Vector newLine2 = (Vector ) vertecesPosition 484 .get(newIndexLine2); 485 newLine1.setSize(1); 486 newLine2.setSize(2); 487 newLine1.add(0, source); 489 newLine2.add(1, target); 490 } else if (!(haveTarget(source))) { 491 int targetIndexLine = getIndexLineOf( 492 vertecesPosition, target); 493 Vector targetLine = (Vector ) vertecesPosition 494 .get(targetIndexLine); 495 int targetIndex = targetLine.indexOf(target); 496 if (targetIndexLine == 0) { 497 if (targetIndex > 0) { 498 Vector sourceLine = new Vector (); 499 sourceLine.setSize(targetIndex); 500 sourceLine.setElementAt(source, 501 targetIndex - 1); 502 vertecesPosition.add(0, sourceLine); 503 } else { 504 Vector sourceLine = new Vector (); 505 sourceLine.setSize(1); 506 sourceLine.setElementAt(source, 0); 507 ((Vector ) vertecesPosition 508 .get(targetIndexLine)).add(0, null); 509 vertecesPosition.add(0, sourceLine); 510 } 511 } else { 512 Vector sourceLine = ((Vector ) vertecesPosition 513 .get(targetIndexLine - 1)); 514 if (sourceLine.size() < targetIndex) { 515 sourceLine.setSize(targetIndex); 516 sourceLine.setElementAt(source, 517 targetIndex - 1); 518 } else { 519 if (sourceLine.get(targetIndex - 1) == null) 520 sourceLine.setElementAt(source, 521 targetIndex - 1); 522 else 523 sourceLine.add(targetIndex - 1, source); 524 } 525 } 526 } else if (!(haveTarget(target))) { 527 int sourceIndexLine = getIndexLineOf( 528 vertecesPosition, source); 529 Vector sourceLine = (Vector ) vertecesPosition 530 .get(sourceIndexLine); 531 int sourceIndex = sourceLine.indexOf(source); 532 addTargetAfter(vertecesPosition, sourceIndexLine, 533 sourceIndex, target); 534 } 535 } 536 } 537 } 538 } 539 } 540 541 547 public void applyLayout(CompositeVertex component) { 548 LayoutAlgorithm algo = new LayoutAlgorithm(getVertecesPosition(),vg); 549 if ((component.getOrigin().x == 0) && (component.getOrigin().y == 0)) 550 component.setOrigin(VertexGraphicsInterface.SUPER_COMPOSITE_ORIGIN); 551 algo.applyLayout(this, component.getOrigin(), component.getSize(), 552 component); 553 } 554 555 561 public void applyLayout(Dimension frameSize) { 562 if (getVertecesPosition().size() != 0) { 563 LayoutAlgorithm algo = new LayoutAlgorithm(getVertecesPosition(),vg); 564 algo.applyLayout(this, VertexGraphicsInterface.PRIMITIVE_ORIGIN, 565 frameSize, null); 566 } 567 } 568 569 574 public void addSuperComponent(CompositeVertex component) { 575 AttributeMap componentAttributes = getModel().createAttributes(); 576 attributes.put(component, componentAttributes); 577 superComponent = component; 578 } 579 580 585 public void addPrimitiveComponent(PrimitiveVertex component) { 586 AttributeMap vertexAttributes = model.createAttributes(); 587 attributes.put(component, vertexAttributes); 588 Rectangle2D c1Bounds = vertexAttributes.createRect(10, 10, vg 589 .getDefaultSize().width, vg.getDefaultSize().height); 590 GraphConstants.setBounds(vertexAttributes, c1Bounds); 591 primitiveVerteces.add(component); 592 } 593 594 598 public void addPrimitiveEdge() { 599 DefaultEdge edge = new DefaultEdge(); 600 AttributeMap edgeAttrib = model.createAttributes(); 601 attributes.put(edge, edgeAttrib); 602 int style = GraphConstants.STYLE_ORTHOGONAL; 603 GraphConstants.setLineStyle(edgeAttrib, style); 604 } 605 606 609 public Map getAttributes() { 610 return attributes; 611 } 612 613 616 public Vector getVertecesPosition() { 617 return vertecesPosition; 618 } 619 620 623 public GraphModel getModel() { 624 return model; 625 } 626 627 630 public static PortGraphicsInterface getPortGraphics() { 631 return pg; 632 } 633 634 637 public static VertexGraphicsInterface getVertexGraphics() { 638 return vg; 639 } 640 641 649 public void compositeConnect(MyPort sourcePort, MyPort targetPort, 650 boolean isCompositeEdge, CompositeVertex component) { 651 Edge edge = component.addBinding(model); 652 cs.connect(edge, sourcePort, targetPort); 653 updateVertecesPosition(edge, sourcePort, targetPort, isCompositeEdge); 654 } 655 656 664 public void primitiveConnect(MyPort sourcePort, MyPort targetPort, 665 boolean isCompositeEdge) { 666 Edge edge = new DefaultEdge(); 667 primitiveEdges.add(edge); 668 AttributeMap eAttributes = model.createAttributes(); 669 attributes.put(edge, eAttributes); 670 int style = GraphConstants.STYLE_ORTHOGONAL; 671 GraphConstants.setLineStyle(eAttributes, style); 672 GraphConstants.setDisconnectable(eAttributes, false); 673 cs.connect(edge, sourcePort, targetPort); 674 updateVertecesPosition(edge, sourcePort, targetPort, isCompositeEdge); 675 } 676 677 680 public ConnectionSet getConnectionSet() { 681 return cs; 682 } 683 684 688 public void setPortGraphics(PortGraphicsInterface portGraphics) { 689 pg = portGraphics; 690 } 691 692 696 public void setVertexGraphics(VertexGraphicsInterface vertexGraphics) { 697 vg = vertexGraphics; 698 } 699 700 703 protected PortView createPortView(JGraph g, CellMapper cm, Object cell) { 704 return new MyPortView(g, cm, (MyPort) cell); 705 } 706 707 private boolean isPrimitiveVertex(Object cell) { 708 return cell instanceof PrimitiveVertex; 709 } 710 711 private boolean isCompositeVertex(Object cell) { 712 return cell instanceof CompositeVertex; 713 } 714 715 718 protected VertexView createVertexView(JGraph g, CellMapper cm, Object cell) { 719 if (isCompositeVertex(cell)) 720 return new CompositeVertexView(g, cm, (CompositeVertex) cell); 721 else if (isPrimitiveVertex(cell)) 722 return new PrimitiveVertexView(g, cm, (PrimitiveVertex) cell); 723 else 724 return null; 725 } 726 727 public CellView createView(JGraph graph, CellMapper mapper, Object cell) { 728 CellView view = null; 729 if (graph.getModel().isPort(cell)) 730 view = createPortView(graph, mapper, cell); 731 else if (graph.getModel().isEdge(cell)) 732 view = createEdgeView(graph, mapper, cell); 733 else 734 view = createVertexView(graph, mapper, cell); 735 mapper.putMapping(cell, view); 736 view.refresh(true); 737 view.update(); 738 return view; 739 } 740 741 } | Popular Tags |