KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > explorer > graph > FractalGraph


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): Alexandre Vandekerkhove.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.fractal.explorer.graph;
27
28 import java.awt.Dimension JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.jgraph.graph.DefaultGraphModel;
32 import org.jgraph.graph.GraphModel;
33 import org.objectweb.fractal.api.Component;
34 import org.objectweb.fractal.api.Interface;
35 import org.objectweb.fractal.api.type.InterfaceType;
36 import org.objectweb.fractal.explorer.FcExplorer;
37 import org.objectweb.util.explorer.swing.graph.CompositeVertex;
38 import org.objectweb.util.explorer.swing.graph.Graph;
39 import org.objectweb.util.explorer.swing.graph.MyPort;
40 import org.objectweb.util.explorer.swing.graph.PortType;
41 import org.objectweb.util.explorer.swing.graph.PrimitiveVertex;
42
43 /**
44  * Utilities class for graphic representation
45  * @author Alexandre Vandekerkhove
46  * @version September 2004
47  */

48 public class FractalGraph
49 {
50
51     /**
52      * Empty constructor
53      */

54     public FractalGraph(){
55     }
56     
57     /**
58      * calls the graph method to represent all the ports on a composite component (around the border)
59      * @param component the composite component to represent
60      * @param composite the (CompositeVertex) associated
61      */

62     protected void addPortsComposite (Component component, CompositeVertex composite){
63         Interface[] listExtItf = GraphInformations.getSortExtItf(component);
64         for (int i=0 ; i<listExtItf.length ; i++){
65             String JavaDoc nameItf = GraphInformations.getInterfaceName(listExtItf[i]);
66             String JavaDoc typePort = GraphInformations.getPortType(listExtItf[i]);
67             //don't draw the collection ports beacause we'll draw only one port per collection
68
if (!typePort.equals(PortType.COLLECTION_PORT)) composite.addCompositePort(listExtItf[i], nameItf, typePort);
69         }
70         
71         //draws the collection ports
72
List JavaDoc listItfCollection = GraphInformations.getItfCollection(component);
73         if (listItfCollection.size()!=0){
74             InterfaceType itType;
75             for (int j=0 ; j<listItfCollection.size() ; j++){
76                 itType = (InterfaceType)listItfCollection.get(j);
77                 composite.addCompositePort(listItfCollection.get(j), itType.getFcItfName() , PortType.COLLECTION_PORT);
78             }
79         }
80     }
81     
82     /**
83      * calls the graph method to represent all the ports on a primitive component
84      * represented without its super-component
85      * @param component the primitive component to represent
86      * @param primitive the (PrimitiveVertex) associated
87      */

88     protected void addPortsPrimitive (Component component, PrimitiveVertex primitive){
89         Interface[] listExtItf = GraphInformations.getSortExtItf(component);
90         for (int i=0 ; i<listExtItf.length ; i++){
91             String JavaDoc nameItf = GraphInformations.getInterfaceName(listExtItf[i]);
92             String JavaDoc typePort = GraphInformations.getPortType(listExtItf[i]);
93             //don't add the collection ports beacause we'll draw only one port per collection
94
if (!typePort.equals(PortType.COLLECTION_PORT)) primitive.addPort(listExtItf[i], nameItf, typePort);
95         }
96         
97         //draws the collection ports
98
List JavaDoc listItfCollection = GraphInformations.getItfCollection(component);
99         if (listItfCollection.size()!=0){
100             InterfaceType itType;
101             for (int j=0 ; j<listItfCollection.size() ; j++){
102                 itType = (InterfaceType)listItfCollection.get(j);
103                 primitive.addPort(listItfCollection.get(j), itType.getFcItfName() , PortType.COLLECTION_PORT);
104             }
105         }
106     }
107     
108     /**
109      * This method draws the neigbors of a primitive component selected by the user
110      * neighbor = a component which is directly bound to the primitive component selected
111      * @param component the primitive component selected
112      * @param primitiveVertex the (PrimitiveVertex) associated
113      * @param graph the graph in which the components are represented
114      * @param controllersDisplay true : allows the display of the control interfaces
115      */

116     protected void addNeighbors (Component component, PrimitiveVertex primitiveVertex, Graph graph, boolean controllersDisplay){
117         PrimitiveVertex neighborVertex;
118         List JavaDoc serverNeighbors = GraphInformations.getServerNeighbors(component);
119         List JavaDoc clientNeighbors = GraphInformations.getClientNeighbors(component);
120         Component neighborComponent;
121         Interface[] listExtItf;
122         Interface targetItf;
123         Component targetComponent;
124         String JavaDoc namePort;
125         boolean isOpen = false; //we don't look inside the neighbor composite components
126
boolean isCompositeEdge = false; //binds between sub-components
127

128         //first we add the server neighbors
129
for (int i=0 ; i<serverNeighbors.size() ; i++){
130             neighborComponent = (Component)serverNeighbors.get(i);
131             if (GraphInformations.isPrimitiveComponent(neighborComponent)){
132                 neighborVertex = new PrimitiveVertex(neighborComponent, FcExplorer.getName(neighborComponent),
133                         graph, new Dimension JavaDoc(), controllersDisplay,
134                         GraphInformations.isStarted(neighborComponent),
135                         GraphInformations.isSharedComponent(neighborComponent));}
136             else { neighborVertex = new CompositeVertex(neighborComponent, FcExplorer.getName(neighborComponent),
137                         graph, new Dimension JavaDoc(), isOpen, controllersDisplay,
138                         GraphInformations.isStarted(neighborComponent),
139                         GraphInformations.isSharedComponent(neighborComponent));}
140             graph.addPrimitiveComponent(neighborVertex);
141             addPortsPrimitive(neighborComponent, neighborVertex);
142             listExtItf = GraphInformations.getSortExtItf(neighborComponent);
143             for(int j=0 ; j<listExtItf.length ; j++){
144                 if(GraphInformations.isClientInterface(listExtItf[j])){
145                     targetComponent = GraphInformations.getTargetComponent(neighborComponent, listExtItf[j]);
146                     if(targetComponent.equals(component)){
147                         targetItf = GraphInformations.getTargetInterface(neighborComponent, listExtItf[j]);
148                         if(GraphInformations.isCollectionInterface(listExtItf[j])){
149                             InterfaceType itType = (InterfaceType)listExtItf[j].getFcItfType();
150                             namePort = itType.getFcItfName();
151                         }
152                         else namePort = GraphInformations.getInterfaceName(listExtItf[j]);
153                         graph.primitiveConnect(neighborVertex.getPort(namePort),
154                                 primitiveVertex.getPort(GraphInformations.getInterfaceName(targetItf)), isCompositeEdge);
155                     }
156                 }
157             }
158         }
159         
160         //we can now add the client neighbors
161
for (int i=0 ; i<clientNeighbors.size() ; i++){
162             neighborComponent = (Component)clientNeighbors.get(i);
163             if (GraphInformations.isPrimitiveComponent(neighborComponent)){
164                 neighborVertex = new PrimitiveVertex(neighborComponent, FcExplorer.getName(neighborComponent),
165                         graph, new Dimension JavaDoc(), controllersDisplay,
166                         GraphInformations.isStarted(neighborComponent),
167                         GraphInformations.isSharedComponent(neighborComponent));}
168             else { neighborVertex = new CompositeVertex(neighborComponent, FcExplorer.getName(neighborComponent),
169                         graph, new Dimension JavaDoc(), isOpen, controllersDisplay,
170                         GraphInformations.isStarted(neighborComponent),
171                         GraphInformations.isSharedComponent(neighborComponent));}
172             graph.addPrimitiveComponent(neighborVertex);
173             addPortsPrimitive(neighborComponent, neighborVertex);
174         
175             listExtItf = GraphInformations.getSortExtItf(component);
176             for(int j=0 ; j<listExtItf.length ; j++){
177                 if(GraphInformations.isClientInterface(listExtItf[j])){
178                     targetComponent = GraphInformations.getTargetComponent(component, listExtItf[j]);
179                     if (targetComponent.equals(neighborComponent)){
180                         targetItf = GraphInformations.getTargetInterface(component, listExtItf[j]);
181                         if(GraphInformations.isCollectionInterface(listExtItf[j])){
182                             InterfaceType itType = (InterfaceType)listExtItf[j].getFcItfType();
183                             namePort = itType.getFcItfName();
184                         }
185                         else namePort = GraphInformations.getInterfaceName(listExtItf[j]);
186                         graph.primitiveConnect(primitiveVertex.getPort(namePort),
187                                 neighborVertex.getPort(GraphInformations.getInterfaceName(targetItf)),
188                                 isCompositeEdge);
189                     }
190                 }
191             }
192         }
193     }
194     
195     /**
196      * adds all the ports of a sub-component of a composite component
197      * @param composite the (CompositeVertex) associated to the composite component
198      * @param subComponent a sub-component
199      */

200     protected void addSubComponentPorts (CompositeVertex composite, Component subComponent){
201         Interface[] listExtItf = GraphInformations.getSortExtItf(subComponent);
202         String JavaDoc portName, typePort;
203         for (int i=0 ; i<listExtItf.length ; i++){
204             portName = GraphInformations.getInterfaceName(listExtItf[i]);
205             typePort = GraphInformations.getPortType(listExtItf[i]);
206             if (!typePort.equals(PortType.COLLECTION_PORT))
207                 composite.addSubComponentPort(FcExplorer.getName(subComponent), subComponent, portName, typePort);
208         }
209         
210         //draws the collection ports
211
List JavaDoc listItfCollection = GraphInformations.getItfCollection(subComponent);
212         if (listItfCollection.size()!=0){
213             InterfaceType itType;
214             for (int j=0 ; j<listItfCollection.size() ; j++){
215                 itType = (InterfaceType)listItfCollection.get(j);
216                 composite.addSubComponentPort(FcExplorer.getName(subComponent), subComponent, itType.getFcItfName(), PortType.COLLECTION_PORT);
217             }
218         }
219     }
220     
221     /**
222      * adds all the sub-components of a composite component with their ports
223      * @param graph the graph in which the sub-components will be represented
224      * @param superComponent the composite component to represent
225      * @param composite the (CompositeVertex) associated
226      */

227     protected void addSubComponents (Graph graph, Component superComponent, CompositeVertex composite){
228         Component[] listSubComponents = GraphInformations.getSubComponents(superComponent);
229         Component subComponent;
230         String JavaDoc nameSubComponent = "";
231         for (int i=0 ; i<listSubComponents.length ; i++){
232             subComponent = listSubComponents[i];
233             composite.addVertex(subComponent, FcExplorer.getName(subComponent), graph,
234                     GraphInformations.getComponentType(subComponent), false, true,
235                     GraphInformations.isStarted(subComponent), GraphInformations.isSharedComponent(subComponent));
236             addSubComponentPorts(composite, subComponent);
237         }
238     }
239
240     /**
241      * Method to draw a bind and make the connection between 2 interfaces
242      * @param graph the graph in which the bind will be drawn
243      * @param nameComponent the name of the client component
244      * @param namePort the name of the client interface
245      * @param nameTargetComponent the name of the server component
246      * @param nameTargetPort the name of the server interface
247      * @param composite the (CompositeVertex) which represents the super-component of the 2 components bound
248      * @param isInternalEdge true if the bind concerns an internal interface of the super-component
249      */

250     protected void drawBind(Graph graph, String JavaDoc nameComponent, String JavaDoc namePort, String JavaDoc nameTargetComponent,
251             String JavaDoc nameTargetPort, CompositeVertex composite, boolean isInternalEdge) {
252         Object JavaDoc sourcePort;
253         if(!isInternalEdge)
254             sourcePort = composite.getVertex(nameComponent).getPort(namePort);
255         else
256             sourcePort = composite.getGeneratedPort(namePort);
257         Object JavaDoc v = composite.getVertex(nameTargetComponent);
258         Object JavaDoc targetPort;
259         targetPort = composite.getVertex(nameTargetComponent).getPort(nameTargetPort);
260         graph.compositeConnect((MyPort)sourcePort, (MyPort)targetPort, isInternalEdge, composite);
261     }
262     
263     /**
264      * calls the graph method to represent all the binds between the sub-components of a composite component
265      * and the binds between the sub-components client interfaces and the server interfaces of the composite component
266      * @param graph the graph in which the binds will be represented
267      * @param superComponent the composite component to represent
268      * @param composite the (CompositeVertex) associated
269      */

270     protected void addBinds (Graph graph, Component superComponent, CompositeVertex composite){
271         
272         Interface[] listExtItf;
273         Interface itf;
274         Component subComponent;
275         Component[] listSubComponents = GraphInformations.getSubComponents(superComponent);
276         String JavaDoc nameComponent, namePort, nameTargetComponent, nameTargetPort;
277         
278         for (int i=0 ; i<listSubComponents.length ; i++){
279             subComponent = listSubComponents[i];
280             listExtItf = GraphInformations.getSortExtItf(subComponent);
281             
282             for (int j=0 ; j<listExtItf.length ; j++){
283                 itf = listExtItf[j];
284                 if ((GraphInformations.isClientInterface(itf))
285                         && (GraphInformations.getTargetComponent(subComponent, itf)!=null)
286                         && (GraphInformations.getTargetInterface(subComponent, itf)!=null)){
287                     
288                         if (GraphInformations.isCollectionInterface(itf)){
289                             InterfaceType itType = (InterfaceType)itf.getFcItfType();
290                             namePort = itType.getFcItfName();
291                         }
292                         
293                         else namePort = GraphInformations.getInterfaceName(itf);
294                         nameComponent = FcExplorer.getName(subComponent);
295                         nameTargetComponent = FcExplorer.getName(GraphInformations.getTargetComponent(subComponent, itf));
296                         nameTargetPort = GraphInformations.getInterfaceName(GraphInformations.getTargetInterface(subComponent, itf));
297                         
298                         //if the target interface is internal : it's an internal interface of the composite component
299
//so it's an internal edge
300
if ((GraphInformations.getTargetInterface(subComponent, itf).isFcInternalItf())
301                             && FcExplorer.getName(superComponent).equals(nameTargetComponent)) {
302                            drawBind(graph, FcExplorer.getName(superComponent), nameTargetPort,
303                                     nameComponent, namePort, composite, true);
304                         }
305                         else if (!GraphInformations.getTargetInterface(subComponent, itf).isFcInternalItf()){
306                             drawBind(graph, nameComponent, namePort,
307                                       nameTargetComponent, nameTargetPort, composite, false);
308                         }
309                     }
310                 }
311             }
312         }
313  
314     /**
315      * calls the graph method in order to represent the internal edges of a composite component
316      * (edges between the internal client interfaces of the composite component and the external interfaces of the sub-components)
317      * @param graph the graph in which the binds will be represented
318      * @param superComponent the composite component to draw
319      * @param composite the (CompositeVertex) associated
320      */

321     protected void addInternalBinds (Graph graph, Component superComponent, CompositeVertex composite){
322         
323         Interface internalItf;
324         Interface[] listIntItf = GraphInformations.getIntItf(superComponent);
325         String JavaDoc nameSuperComponent = FcExplorer.getName(superComponent);
326         String JavaDoc nameInternalItf, nameTargetPort, nameTargetComponent;
327         
328         for (int i=0 ; i<listIntItf.length ; i++){
329            internalItf = listIntItf[i];
330            
331            //the internal server interfaces are binded in the method "addBinds"
332
if (((GraphInformations.getPortType(internalItf))!=PortType.SERVER_PORT)
333                && (GraphInformations.getTargetComponent(superComponent, internalItf)!=null)
334                && (GraphInformations.getTargetInterface(superComponent, internalItf)!=null)){
335             
336             nameInternalItf = GraphInformations.getInterfaceName(internalItf);
337             nameTargetPort = GraphInformations.getInterfaceName(GraphInformations.getTargetInterface(superComponent, internalItf));
338             nameTargetComponent = FcExplorer.getName(GraphInformations.getTargetComponent(superComponent, internalItf));
339             
340             drawBind(graph, nameSuperComponent, nameInternalItf,
341                     nameTargetComponent, nameTargetPort, composite, true);
342             
343            }
344         }
345     }
346
347     /**
348      * method to construct a graph which represents a component selected by the user
349      * If the component is a composite component, draws its sub-components with the interfaces and the binds
350      * @param component the component selected by the user
351      * @return the (Graph) associated
352      */

353     public Graph getFractalGraph (Component component){
354         
355         String JavaDoc nameComponent = FcExplorer.getName(component);
356         
357         PortGraphics pg = new PortGraphics();
358         VertexGraphics vg = new VertexGraphics();
359         
360         //*********************Dimensions to modify************************
361
Dimension JavaDoc frameSize = new Dimension JavaDoc(100, 100); //dimension of the border
362

363         GraphModel model = new DefaultGraphModel();
364         Graph graph = new Graph(model, pg, vg);
365         graph.setPortsVisible(true);
366         
367         boolean controllersDisplay = true; //allows the display of the control interfaces
368
boolean isPrimitive = GraphInformations.isPrimitiveComponent(component);
369         
370         
371         if(!isPrimitive){
372             //we have a composite component
373

374             boolean isOpen = true; //allows the display of the sub-components of the composite component
375

376             CompositeVertex compositeVertex = new CompositeVertex(component,
377                     nameComponent, graph, frameSize, isOpen, controllersDisplay, GraphInformations.isStarted(component),
378                     GraphInformations.isSharedComponent(component));
379             graph.addSuperComponent(compositeVertex);
380             
381             //draws the ports of the composite
382
addPortsComposite(component, compositeVertex);
383             //draws the sub-components with their ports
384
addSubComponents(graph, component, compositeVertex);
385             //draws the binds
386
addBinds(graph, component, compositeVertex);
387             addInternalBinds (graph, component, compositeVertex);
388             
389             // Insert into Model
390
graph.insertIntoModel(isPrimitive);
391             graph.applyLayout(compositeVertex);
392       
393             return graph;
394         }
395         
396         else {
397             //we have a primitive component
398

399             PrimitiveVertex primitiveVertex = new PrimitiveVertex(component, nameComponent, graph, new Dimension JavaDoc(), controllersDisplay,
400                     GraphInformations.isStarted(component), GraphInformations.isSharedComponent(component));
401             graph.addPrimitiveComponent(primitiveVertex);
402          
403             //draws the ports
404
addPortsPrimitive(component, primitiveVertex);
405             //draws the neighbors and the binds between the neighbors and the primitive component
406
addNeighbors (component, primitiveVertex, graph, controllersDisplay);
407             
408              // Insert into Model
409
graph.insertIntoModel(isPrimitive);
410              graph.applyLayout(frameSize);
411              
412              return graph;
413         }
414     }
415     
416 }
Popular Tags