KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > graph > Graph


1 /*====================================================================
2
3  Objectweb Explorer Framework
4  Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21
22  Initial developer(s): Laëtitia Lafeuille.
23  Contributor(s): ______________________________________.
24
25  ====================================================================*/

26 package org.objectweb.util.explorer.swing.graph;
27
28 import java.awt.Dimension JavaDoc;
29 import java.awt.geom.Rectangle2D JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Vector JavaDoc;
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 /**
49  *
50  * @version 0.2
51  */

52 public class Graph extends JGraph {
53
54     /** The connectionSet that serves to connect edges */
55     private ConnectionSet cs = new ConnectionSet();
56
57     private GraphModel model = new DefaultGraphModel();
58
59     /** The interface where are defined the graphics method to draw a port */
60     private static PortGraphicsInterface pg;
61
62     /** The interface where are defined the graphics method to draw a vertex */
63     private static VertexGraphicsInterface vg;
64
65     /** The Nested Map (from Cells to Attributes) */
66     private Map JavaDoc attributes;
67
68     /** The super component in the graph (the open composite vertex) */
69     private CompositeVertex superComponent;
70
71     /** The list of the verteces where they aren't into an open composite vertex */
72     private ArrayList JavaDoc primitiveVerteces;
73
74     /**
75      * The edges list contained in a graph where there hasn't an open composite
76      * vertex
77      */

78     private ArrayList JavaDoc primitiveEdges;
79
80     /**
81      * The vector that define the position of the verteces. It's used in the
82      * layout algorithm
83      */

84     private Vector JavaDoc vertecesPosition = new Vector JavaDoc();
85
86     /**
87      * Default constructor
88      * @param model The graph model
89      */

90     public Graph(GraphModel model) {
91         super(model);
92         attributes = new Hashtable JavaDoc();
93         this.model = model;
94         primitiveVerteces = new ArrayList JavaDoc();
95         primitiveEdges = new ArrayList JavaDoc();
96     }
97
98     /**
99      * Default constructor
100      * @param model The model
101      * @param portGraphics The graphic port
102      * @param vertexGraphics The graphic vertex
103      */

104     public Graph(GraphModel model, PortGraphicsInterface portGraphics,
105             VertexGraphicsInterface vertexGraphics) {
106         super(model);
107         this.model = model;
108         attributes = new Hashtable JavaDoc();
109         cs = new ConnectionSet();
110         setPortGraphics(portGraphics);
111         setVertexGraphics(vertexGraphics);
112         primitiveVerteces = new ArrayList JavaDoc();
113         primitiveEdges = new ArrayList JavaDoc();
114     }
115
116     /**
117      * Method that inserts all the objects created (verteces, ports, edges, ..)
118      * into the model. We have to call this method to display the graph.
119      *
120      * @param isPrimitif boolean that indicate if there is an open composite vertex in
121      * the graph
122      */

123     public void insertIntoModel(boolean isPrimitif) {
124         Object JavaDoc[] cells;
125         if (!isPrimitif) {
126             ArrayList JavaDoc edges = superComponent.getBinding();
127             ArrayList JavaDoc verteces = superComponent.getVerteces();
128             cells = new Object JavaDoc[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 JavaDoc edges = primitiveEdges;
138             ArrayList JavaDoc verteces = primitiveVerteces;
139             cells = new Object JavaDoc[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     /**
152      * The method updates the vector vertecesPosition (when it's empty) that
153      * serves for the layoutALgorithm. The method is called when an edge is
154      * connected.
155      *
156      * @param sourcePort the source port of the connected edge
157      * @param targetPort the target port of the connected edge
158      * @param isCompositeEdge boolean that indicates if the edges is connected to an open
159      * composite vertex
160      */

161     private void updateEmptyVertecesPosition(MyPort sourcePort,
162             MyPort targetPort, boolean isCompositeEdge) {
163         Object JavaDoc source = sourcePort.getParent();
164         Object JavaDoc 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 JavaDoc());
171                     Vector JavaDoc newLine = (Vector JavaDoc) 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 JavaDoc());
180                 Vector JavaDoc newLine = (Vector JavaDoc) vertecesPosition.get(0);
181                 newLine.setSize(1);
182                 newLine.add(0, source);
183             }
184         } else if (!(sourcePort.getType().equals(PortType.CONTROLLER_PORT))) {
185             //add a line
186
vertecesPosition.add(0, new Vector JavaDoc());
187             Vector JavaDoc newLine = (Vector JavaDoc) vertecesPosition.get(0);
188             newLine.setSize(2);
189             //add the two verteces in the line
190
newLine.add(0, source);
191             newLine.add(1, target);
192         } else {
193             //add 2 lines vertecesPosition.add(0, new Vector());
194
vertecesPosition.add(0, new Vector JavaDoc());
195             Vector JavaDoc newLine1 = (Vector JavaDoc) vertecesPosition.get(0);
196             newLine1.setSize(1);
197             vertecesPosition.add(1, new Vector JavaDoc());
198             Vector JavaDoc newLine2 = (Vector JavaDoc) vertecesPosition.get(1);
199             newLine2.setSize(2);
200             //add the two verteces in the lines
201
newLine1.add(0, source);
202             newLine2.add(1, target);
203         }
204     }
205
206     /**
207      * The method updates the vector vertecesPosition when the method is called
208      * in the moment that a composite edge is connecting. The method is called
209      * when an edge is connected.
210      *
211      * @param sourcePort the source port of the connected edge
212      * @param targetPort the target port of the connected edge
213      */

214     private void updateVertecesPositionWithCompositeEdge(MyPort sourcePort,
215             MyPort targetPort) {
216         Object JavaDoc source = sourcePort.getParent();
217         Object JavaDoc 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 JavaDoc targetLine = (Vector JavaDoc) 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 JavaDoc newLine = new Vector JavaDoc();
237                             newLine.setSize(1);
238                             vertecesPosition.setElementAt(newLine, i);
239                         }
240                         ((Vector JavaDoc) 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 JavaDoc sourceLine = (Vector JavaDoc) 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 JavaDoc newLine = new Vector JavaDoc();
264                         newLine.setSize(1);
265                         vertecesPosition.add(i, newLine);
266                     }
267                     Vector JavaDoc sourceLine = (Vector JavaDoc) vertecesPosition.get(index);
268                     sourceLine.setSize(getNbColumn() + 1);
269                     sourceLine.add(getNbColumn(), source);
270                 }
271             }
272         }
273
274     }
275
276     /**
277      * @return the number of columns in the verteces position vector
278      */

279     private int getNbColumn() {
280         int nb = 0;
281         for (int i = 0; i < vertecesPosition.size(); i++) {
282             Vector JavaDoc line = (Vector JavaDoc) 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     /**
296      * @param source the source researched in the veteces position vector
297      * @return boolean that indicates if the source is inside the vector
298      */

299     private boolean haveSource(Object JavaDoc source) {
300         int haveSource = 0;
301         for (int i = 0; i < vertecesPosition.size(); i++) {
302             if (((Vector JavaDoc) vertecesPosition.get(i)).contains(source))
303                 haveSource++;
304         }
305         return (haveSource != 0);
306     }
307
308     /**
309      * @param target the target researched in the veteces position vector
310      * @return boolean that indicates if the taget is inside the vector
311      */

312     private boolean haveTarget(Object JavaDoc target) {
313         int haveTarget = 0;
314         for (int i = 0; i < vertecesPosition.size(); i++) {
315             if (((Vector JavaDoc) vertecesPosition.get(i)).contains(target))
316                 haveTarget++;
317         }
318         return (haveTarget != 0);
319     }
320
321     /**
322      * Add a target after a source int the verteces position vector
323      *
324      * @param vertecesPosition the vector
325      * @param sourceIndexLine
326      * @param sourceIndex
327      * @param target the target to insert
328      */

329     private void addTargetAfter(Vector JavaDoc vertecesPosition, int sourceIndexLine,
330             int sourceIndex, Object JavaDoc target) {
331         int add = 0;
332         for (int i = sourceIndexLine; i < vertecesPosition.size(); i++) {
333             Vector JavaDoc line = (Vector JavaDoc) 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 JavaDoc(),
346                     vertecesPosition.size() - 1);
347             Vector JavaDoc targetLine = (Vector JavaDoc) vertecesPosition.get(vertecesPosition
348                     .size() - 1);
349             targetLine.setSize(sourceIndex + 2);
350             targetLine.setElementAt(target, sourceIndex + 1);
351         }
352     }
353
354     /**
355      * Add a source before a target int the verteces position vector
356      *
357      * @param vertecesPosition the vector
358      * @param targetIndexLine
359      * @param targetIndex
360      * @param source the source to insert
361      */

362     private void addSourceBefore(Vector JavaDoc vertecesPosition, int targetIndexLine,
363             int targetIndex, Object JavaDoc source) {
364         int add = 0;
365         for (int i = targetIndexLine; i < vertecesPosition.size(); i++) {
366             Vector JavaDoc line = (Vector JavaDoc) 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 JavaDoc) 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 JavaDoc(),
380                     vertecesPosition.size() - 1);
381             Vector JavaDoc sourceLine = (Vector JavaDoc) vertecesPosition.get(vertecesPosition
382                     .size() - 1);
383             sourceLine.setSize(targetIndex);
384             sourceLine.add(targetIndex - 1, source);
385         }
386     }
387
388     /**
389      * @param vertecesPosition
390      * @param vertex
391      * @return the index of the line that contains the vertex
392      */

393     private int getIndexLineOf(Vector JavaDoc vertecesPosition, Object JavaDoc vertex) {
394         int index = 0;
395         for (int i = 0; i < vertecesPosition.size(); i++) {
396             if (((Vector JavaDoc) vertecesPosition.get(i)).contains(vertex))
397                 index = i;
398         }
399         return index;
400     }
401
402     /**
403      * Updates the vertecesPosition vector that serve for the layout algorithm
404      *
405      * @param edge
406      * @param sourcePort
407      * @param targetPort
408      * @param isCompositeEdge
409      */

410     public void updateVertecesPosition(Object JavaDoc edge, MyPort sourcePort,
411             MyPort targetPort, boolean isCompositeEdge) {
412         if ((sourcePort != null) && (targetPort != null)) {
413             Object JavaDoc source = sourcePort.getParent();
414             Object JavaDoc 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 the source and the target not in the
426
// vertecesPosition
427
//add a new line in the vertecesPosition
428
//and add the two verteces in this line
429
if ((!haveSource(source)) && (!haveTarget(target))) {
430                             //add a line
431
int newIndexLine = vertecesPosition.size();
432                             vertecesPosition.add(newIndexLine, new Vector JavaDoc());
433                             Vector JavaDoc newLine = (Vector JavaDoc) vertecesPosition
434                                     .get(newIndexLine);
435                             newLine.setSize(2);
436                             //add the two verteces in the line
437
newLine.add(0, source);
438                             newLine.add(1, target);
439                         } else if (!haveSource(source)) {
440                             int targetIndexLine = getIndexLineOf(
441                                     vertecesPosition, target);
442                             Vector JavaDoc targetLine = (Vector JavaDoc) 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 JavaDoc sourceLine = (Vector JavaDoc) 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                         //else, the target and the source are already in the
472
// vertecesPosition
473

474                     } else {
475                         if ((!(haveTarget(source))) && (!(haveTarget(target)))) {
476                             //add 2 lines
477
int newIndexLine1 = vertecesPosition.size();
478                             vertecesPosition.add(newIndexLine1, new Vector JavaDoc());
479                             Vector JavaDoc newLine1 = (Vector JavaDoc) vertecesPosition
480                                     .get(newIndexLine1);
481                             int newIndexLine2 = vertecesPosition.size();
482                             vertecesPosition.add(newIndexLine2, new Vector JavaDoc());
483                             Vector JavaDoc newLine2 = (Vector JavaDoc) vertecesPosition
484                                     .get(newIndexLine2);
485                             newLine1.setSize(1);
486                             newLine2.setSize(2);
487                             //add the two verteces in the lines
488
newLine1.add(0, source);
489                             newLine2.add(1, target);
490                         } else if (!(haveTarget(source))) {
491                             int targetIndexLine = getIndexLineOf(
492                                     vertecesPosition, target);
493                             Vector JavaDoc targetLine = (Vector JavaDoc) vertecesPosition
494                                     .get(targetIndexLine);
495                             int targetIndex = targetLine.indexOf(target);
496                             if (targetIndexLine == 0) {
497                                 if (targetIndex > 0) {
498                                     Vector JavaDoc sourceLine = new Vector JavaDoc();
499                                     sourceLine.setSize(targetIndex);
500                                     sourceLine.setElementAt(source,
501                                             targetIndex - 1);
502                                     vertecesPosition.add(0, sourceLine);
503                                 } else {
504                                     Vector JavaDoc sourceLine = new Vector JavaDoc();
505                                     sourceLine.setSize(1);
506                                     sourceLine.setElementAt(source, 0);
507                                     ((Vector JavaDoc) vertecesPosition
508                                             .get(targetIndexLine)).add(0, null);
509                                     vertecesPosition.add(0, sourceLine);
510                                 }
511                             } else {
512                                 Vector JavaDoc sourceLine = ((Vector JavaDoc) 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 JavaDoc sourceLine = (Vector JavaDoc) 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     /**
542      * Apply the layout algorithm. This method is called when there is an open
543      * composite vertex in the graph.
544      *
545      * @param component the open composite vertex
546      */

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     /**
556      * Apply the layout algorithm. This method is called when there isn't an
557      * open composite vertex in the graph.
558      *
559      * @param frameSize the size of the frame
560      */

561     public void applyLayout(Dimension JavaDoc 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     /**
570      * Add an open composite vertex int he graph
571      *
572      * @param component the open composite vertex
573      */

574     public void addSuperComponent(CompositeVertex component) {
575         AttributeMap componentAttributes = getModel().createAttributes();
576         attributes.put(component, componentAttributes);
577         superComponent = component;
578     }
579
580     /**
581      * Add a primitive vertex or a closed composite vertex in the graph.
582      *
583      * @param component the vertex
584      */

585     public void addPrimitiveComponent(PrimitiveVertex component) {
586         AttributeMap vertexAttributes = model.createAttributes();
587         attributes.put(component, vertexAttributes);
588         Rectangle2D JavaDoc c1Bounds = vertexAttributes.createRect(10, 10, vg
589                 .getDefaultSize().width, vg.getDefaultSize().height);
590         GraphConstants.setBounds(vertexAttributes, c1Bounds);
591         primitiveVerteces.add(component);
592     }
593
594     /**
595      * Add an edge in the graph in the case that the graph don't have an open
596      * composite vertex.
597      */

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     /**
607      * @return The attributes map.
608      */

609     public Map JavaDoc getAttributes() {
610         return attributes;
611     }
612
613     /**
614      * @return The position of the verteces
615      */

616     public Vector JavaDoc getVertecesPosition() {
617         return vertecesPosition;
618     }
619
620     /**
621      * @see org.jgraph.JGraph#getModel()
622      */

623     public GraphModel getModel() {
624         return model;
625     }
626
627     /**
628      * @return The graphic port.
629      */

630     public static PortGraphicsInterface getPortGraphics() {
631         return pg;
632     }
633
634     /**
635      * @return The graphic vertex.
636      */

637     public static VertexGraphicsInterface getVertexGraphics() {
638         return vg;
639     }
640
641     /**
642      * Connect an edge in the case that the graph have an open composite vertex
643      *
644      * @param sourcePort
645      * @param targetPort
646      * @param isCompositeEdge
647      * @param component
648      */

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     /**
657      * Connect an edge in the case that the graph don't have an open composite
658      * vertex
659      *
660      * @param sourcePort
661      * @param targetPort
662      * @param isCompositeEdge
663      */

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     /**
678      * @return The set of connections
679      */

680     public ConnectionSet getConnectionSet() {
681         return cs;
682     }
683
684     /**
685      * Fixes the graphic port
686      * @param portGraphics The graphic port
687      */

688     public void setPortGraphics(PortGraphicsInterface portGraphics) {
689         pg = portGraphics;
690     }
691
692     /**
693      * Fixes the graphic vertex
694      * @param vertexGraphics The graphic vertex
695      */

696     public void setVertexGraphics(VertexGraphicsInterface vertexGraphics) {
697         vg = vertexGraphics;
698     }
699
700     /**
701      * Constructs a PortView view for the specified object.
702      */

703     protected PortView createPortView(JGraph g, CellMapper cm, Object JavaDoc cell) {
704         return new MyPortView(g, cm, (MyPort) cell);
705     }
706
707     private boolean isPrimitiveVertex(Object JavaDoc cell) {
708         return cell instanceof PrimitiveVertex;
709     }
710
711     private boolean isCompositeVertex(Object JavaDoc cell) {
712         return cell instanceof CompositeVertex;
713     }
714
715     /**
716      * Constructs a PortView view for the specified object.
717      */

718     protected VertexView createVertexView(JGraph g, CellMapper cm, Object JavaDoc 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 JavaDoc 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