KickJava   Java API By Example, From Geeks To Geeks.

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


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.Point JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.jgraph.graph.AttributeMap;
34 import org.jgraph.graph.DefaultEdge;
35 import org.jgraph.graph.DefaultGraphCell;
36 import org.jgraph.graph.Edge;
37 import org.jgraph.graph.GraphConstants;
38 import org.jgraph.graph.GraphModel;
39
40 /**
41  *
42  * @version 0.2
43  */

44 public class CompositeVertex
45      extends PrimitiveVertex
46 {
47
48     /** If the vertex is open, the elements inside are visible */
49     private boolean isOpen = false;
50
51     /** List of the verteces inside the composite */
52     private ArrayList JavaDoc verteces;
53
54     /** The origin point of the vertex */
55     private Point JavaDoc origin;
56
57     /** List of the edges inside the composite */
58     private ArrayList JavaDoc binding;
59
60     /** The list of the internal ports */
61     private ArrayList JavaDoc internalPorts;
62
63     private ArrayList JavaDoc internalServerPorts;
64
65     private ArrayList JavaDoc internalClientPorts;
66
67     private ArrayList JavaDoc internalControllerPorts;
68
69     /** Parameters that serve for attribute a number for each internal ports */
70     private int numberInternalServer;
71
72     private int numberInternalClient;
73
74     private int numberInternalController;
75
76     /**
77      * Create a new Component Vertex
78      *
79      * @param component the component associated with the vertex
80      * @param name the name of the component
81      * @param graph the graph that contain the vertex
82      * @param compositeSize the size of the composite
83      * @param isOpen a boolean that indicate if the component is open
84      * @param controllersDisplay a boolean that indicate if the controller ports are visible
85      * @param isStarted a boolean that indicate if the component is started
86      * @param isShared a boolean that indicate if the component is shared
87      */

88     public CompositeVertex(Object JavaDoc component, String JavaDoc name, Graph graph,
89             Dimension JavaDoc compositeSize, boolean isOpen,
90             boolean controllersDisplay, boolean isStarted, boolean isShared) {
91         super(component, name, graph, compositeSize, controllersDisplay,
92                 isStarted, isShared);
93         verteces = new ArrayList JavaDoc();
94         binding = new ArrayList JavaDoc();
95         setSize(compositeSize);
96         this.isOpen = isOpen;
97         this.type = VertexType.COMPOSITE_VERTEX;
98         if (isOpen) {
99             verteces.add(this);
100             AttributeMap vAttributes = graph.getModel().createAttributes();
101             internalPorts = new ArrayList JavaDoc();
102             internalServerPorts = new ArrayList JavaDoc();
103             internalClientPorts = new ArrayList JavaDoc();
104             internalControllerPorts = new ArrayList JavaDoc();
105             origin = new Point JavaDoc();
106         }
107     }
108
109     /**
110      *
111      * @return the vertex edges list
112      */

113     public ArrayList JavaDoc getBinding() {
114         return binding;
115     }
116
117     /**
118      *
119      * @param model
120      * @return An edge that have been created and inserted into the edges list
121      * of the vertex
122      */

123     public Edge addBinding(GraphModel model) {
124         Edge e = new DefaultEdge();
125         binding.add(e);
126         AttributeMap eAttributes = model.createAttributes();
127         attributes.put(e, eAttributes);
128         int style = GraphConstants.STYLE_ORTHOGONAL;
129         GraphConstants.setLineStyle(eAttributes, style);
130         return e;
131     }
132
133     /**
134      * Research an internal port of a composite vertex with his name
135      *
136      * @param name the name of the researched port
137      * @return the researched port
138      */

139     public MyPort getInternalPort(String JavaDoc name) {
140         for (int i = 0; i < internalPorts.size(); i++) {
141             if ((((MyPort) internalPorts.get(i)).getName().equals(name))
142                     && (!((MyPort) internalPorts.get(i)).isGenerated()))
143                 return (MyPort) internalPorts.get(i);
144         }
145         return null;
146     }
147
148     /**
149      * Research a port generated (a port is generated automaticly when an
150      * internal port is created) with his name
151      *
152      * @param name the name of the researched port
153      * @return the researched port
154      */

155     public MyPort getGeneratedPort(String JavaDoc name) {
156         ArrayList JavaDoc internalPorts = getListInternalPorts();
157         for (int i = 0; i < internalPorts.size(); i++) {
158             if ((((MyPort) internalPorts.get(i)).getName().equals(name))
159                     && (((MyPort) internalPorts.get(i)).isGenerated()))
160                 return (MyPort) internalPorts.get(i);
161         }
162         return null;
163     }
164
165     /**
166      *
167      * @return the vertex internal ports list
168      */

169     public ArrayList JavaDoc getListInternalPorts() {
170         return internalPorts;
171     }
172
173     /**
174      *
175      * @return the vertex internal server ports list
176      */

177     public ArrayList JavaDoc getListInternalServerPorts() {
178         if (internalServerPorts.size() == 0) {
179             List JavaDoc ports = getListInternalPorts();
180             for (int i = 0; i < ports.size(); i++) {
181                 if (((MyPort) ports.get(i)).getType().equals(
182                         PortType.SERVER_PORT))
183                     internalServerPorts.add(ports.get(i));
184             }
185         }
186         return internalServerPorts;
187     }
188
189     /**
190      *
191      * @return the vertex internal client ports list
192      */

193     public ArrayList JavaDoc getListInternalClientPorts() {
194         if (internalClientPorts.size() == 0) {
195             List JavaDoc ports = getListInternalPorts();
196             for (int i = 0; i < ports.size(); i++) {
197                 if (((MyPort) ports.get(i)).getType().equals(
198                         PortType.CLIENT_PORT))
199                     internalClientPorts.add(ports.get(i));
200             }
201         }
202         return internalClientPorts;
203     }
204
205     /**
206      *
207      * @return the vertex internal controller ports list
208      */

209     public ArrayList JavaDoc getListInternalControllerPorts() {
210         if (internalControllerPorts.size() == 0) {
211             List JavaDoc ports = getListInternalPorts();
212             for (int i = 0; i < ports.size(); i++) {
213                 if (((MyPort) ports.get(i)).getType().equals(
214                         PortType.CONTROLLER_PORT))
215                     internalControllerPorts.add(ports.get(i));
216             }
217         }
218         return internalControllerPorts;
219     }
220
221     /**
222      * Research a vertex inside the composite with his name
223      *
224      * @param name the name of the researched vertex
225      * @return the vertex researched
226      */

227     public PrimitiveVertex getVertex(String JavaDoc name) {
228         for (int i = 0; i < verteces.size(); i++) {
229             if (((PrimitiveVertex) verteces.get(i)).getName().equals(name))
230                 return (PrimitiveVertex) verteces.get(i);
231         }
232         return null;
233     }
234
235     /**
236      * Add a vertex inside the composite
237      *
238      * @param component the component associated with the vertex inserted
239      * @param name the name of the vertex inserted
240      * @param graph the graph that contain the verteces
241      * @param type the type of the vertex inserted
242      * @param isOpen
243      * @param controllersDisplay
244      * @param isStarted
245      * @param isShared
246      */

247     public void addVertex(Object JavaDoc component, String JavaDoc name, Graph graph,
248             String JavaDoc type, boolean isOpen, boolean controllersDisplay,
249             boolean isStarted, boolean isShared) {
250         PortGraphicsInterface pg = Graph.getPortGraphics();
251         VertexGraphicsInterface vg = Graph.getVertexGraphics();
252         DefaultGraphCell vertex;
253         if (type.equals(VertexType.PRIMITIF_VERTEX)) {
254             vertex = new PrimitiveVertex(component, name, graph, getSize(),
255                     controllersDisplay, isStarted, isShared);
256             AttributeMap vAttributes = graph.getModel().createAttributes();
257             attributes.put(vertex, vAttributes);
258         } else {
259             vertex = new CompositeVertex(component, name, graph, vg
260                     .getDefaultSize(), isOpen, controllersDisplay, isStarted,
261                     isShared);
262         }
263         verteces.add(vertex);
264     }
265
266     /**
267      * Add a port on a vertex inside the composite vertex
268      *
269      * @param vertexName the vertex inside the composite
270      * @param itf the interface associated with the port inserted
271      * @param portName the name of the port inserted
272      * @param type the type of the port inserted
273      */

274     public void addSubComponentPort(String JavaDoc vertexName, Object JavaDoc itf,
275             String JavaDoc portName, String JavaDoc type) {
276         PrimitiveVertex v = getVertex(vertexName);
277         v.addPort(itf, portName, type);
278     }
279
280     /**
281      * Add a port on the composite vertex
282      *
283      * @param itf the interface associated with the port inserted
284      * @param portName the name of the port inserted
285      * @param type the type of the port inserted
286      */

287     public void addCompositePort(Object JavaDoc itf, String JavaDoc portName, String JavaDoc type) {
288         MyPort initialPort = null;
289         MyPort generatedPort = null;
290         if ((type.equals(PortType.CLIENT_PORT))
291                 || (type.equals(PortType.COLLECTION_PORT))) {
292             numberInternalClient++;
293             initialPort = new MyPort(itf, portName, type, numberInternalClient,
294                     false);
295             generatedPort = new MyPort(itf, portName, PortType.SERVER_PORT,
296                     numberInternalClient, true);
297             internalPorts.add(generatedPort);
298             super.add(generatedPort);
299         } else if (type.equals(PortType.SERVER_PORT)) {
300             numberInternalServer++;
301             initialPort = new MyPort(itf, portName, type, numberInternalServer,
302                     false);
303             generatedPort = new MyPort(itf, portName, PortType.CLIENT_PORT,
304                     numberInternalServer, true);
305             internalPorts.add(generatedPort);
306             super.add(generatedPort);
307         } else if (type.equals(PortType.CONTROLLER_PORT)) {
308             numberInternalController++;
309             initialPort = new MyPort(itf, portName, type,
310                     numberInternalController, false);
311         }
312         internalPorts.add(initialPort);
313         super.add(initialPort);
314     }
315
316     /**
317      * @return <tt>true</tt> if the elements inside are visible.
318      */

319     public boolean isOpen() {
320         return isOpen;
321     }
322
323     /**
324      * @param isOpen <tt>true</tt> if the elements inside are visible.
325      */

326     public void setOpen(boolean isOpen) {
327         this.isOpen = isOpen;
328     }
329
330     /**
331      *
332      * @return the verteces list inside the composite vertex
333      */

334     public ArrayList JavaDoc getVerteces() {
335         return verteces;
336     }
337
338     /**
339      *
340      * @return the graphics origin of the vertex
341      */

342     public Point JavaDoc getOrigin() {
343         return origin;
344     }
345
346     /**
347      * @param p The graphics origin of the vertex
348      */

349     public void setOrigin(Point JavaDoc p) {
350         origin.x = p.x;
351         origin.y = p.y;
352     }
353
354 }
Popular Tags