KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.jgraph.graph.DefaultGraphCell;
33
34 /**
35  *
36  * @version 0.2
37  */

38 public class PrimitiveVertex extends DefaultGraphCell {
39
40     /** The component relative to the vertex */
41     private Object JavaDoc component;
42
43     /** The vertex ports list */
44     private ArrayList JavaDoc ports;
45
46     /** The vertex client ports list */
47     private ArrayList JavaDoc clientPorts;
48
49     /** The vertex server ports list */
50     private ArrayList JavaDoc serverPorts;
51
52     /** The vertex controller ports list */
53     private ArrayList JavaDoc controllerPorts;
54
55     /** The vertex name */
56     private String JavaDoc name;
57
58     /** The graph that contain the vertex */
59     private Graph graph;
60
61     /** Boolean tht indicates if the component is started or not */
62     private boolean isStarted;
63
64     /** Boolean that indicates if the component is shared or not */
65     private boolean isShared;
66
67     /** Number of the specific port */
68     private int numberServer, numberClient, numberControler,
69             numberInternalServer, numberInternalClient;
70
71     /** The type of the vertex : primitive/composite */
72     transient String JavaDoc type;
73
74     /** The size of the vertex */
75     private Dimension JavaDoc size = new Dimension JavaDoc();
76
77     /** The size of the superComposite */
78     private Dimension JavaDoc compositeSize = new Dimension JavaDoc();
79
80     /** Boolean that indicate if the controllers must be displayed or not */
81     private boolean controllersDisplay;
82
83     private boolean isFirstPaint;
84
85     public PrimitiveVertex(Object JavaDoc component, String JavaDoc name, Graph graph,
86             Dimension JavaDoc compositeSize, boolean controllersDisplay,
87             boolean isStarted, boolean isShared) {
88         super(name);
89         this.name = name;
90         this.graph = graph;
91         this.controllersDisplay = controllersDisplay;
92         this.component = component;
93         this.type = VertexType.PRIMITIF_VERTEX;
94         this.isStarted = isStarted;
95         this.isShared = isShared;
96         ports = new ArrayList JavaDoc();
97         clientPorts = new ArrayList JavaDoc();
98         serverPorts = new ArrayList JavaDoc();
99         controllerPorts = new ArrayList JavaDoc();
100         numberServer = 0;
101         numberClient = 0;
102         numberControler = 0;
103         numberInternalServer = 0;
104         numberInternalClient = 0;
105         setCompositeSize(compositeSize);
106         isFirstPaint = true;
107     }
108
109     /**
110      * Add a port on the vertex
111      * @param itf, the interface relative to the port to add
112      * @param name, the name of the port
113      * @param type, the type of the port
114      */

115     public void addPort(Object JavaDoc itf, String JavaDoc name, String JavaDoc type) {
116         MyPort p = null;
117         if ((type.equals(PortType.CLIENT_PORT))
118                 || (type.equals(PortType.COLLECTION_PORT))) {
119             numberClient++;
120             p = new MyPort(itf, name, type, numberClient, false);
121         } else if (type.equals(PortType.SERVER_PORT)) {
122             numberServer++;
123             p = new MyPort(itf, name, type, numberServer, false);
124         } else if (type.equals(PortType.CONTROLLER_PORT)) {
125             numberControler++;
126             p = new MyPort(itf, name, type, numberControler, false);
127         }
128         super.add(p);
129         ports.add(p);
130     }
131
132     public boolean isFirstPaint() {
133         return isFirstPaint;
134     }
135
136     public void setFirstPaint(boolean isFirstPaint) {
137         this.isFirstPaint = isFirstPaint;
138     }
139
140     public boolean isShared() {
141         return isShared;
142     }
143
144     public void setShared(boolean isShared) {
145         this.isShared = isShared;
146     }
147
148     public boolean isStarted() {
149         return isStarted;
150     }
151
152     public void setStarted(boolean isStarted) {
153         this.isStarted = isStarted;
154     }
155
156     public Object JavaDoc getComponent() {
157         return component;
158     }
159
160     public boolean getControllerDisplay() {
161         return controllersDisplay;
162     }
163
164     public void setControllerDisplay(boolean controllersDisplay) {
165         this.controllersDisplay = controllersDisplay;
166     }
167
168     public ArrayList JavaDoc getListPorts() {
169         return ports;
170     }
171
172     /**
173      * Research a port with his name
174      * @param name, the name of the port researched
175      * @return the port researched
176      */

177     public MyPort getPort(String JavaDoc name) {
178         for (int i = 0; i < ports.size(); i++) {
179             if (((MyPort) ports.get(i)).getName().equals(name))
180                 return (MyPort) ports.get(i);
181         }
182         return null;
183     }
184
185     /**
186      * Research a port with his name, his type and his number.
187      * @param name, the name of the port
188      * @param type, the type of the port
189      * @param number, the number of the port
190      * @return the port researched
191      */

192     public MyPort getPort(String JavaDoc name, String JavaDoc type, int number) {
193         List JavaDoc ports;
194         if (type.equals(PortType.CLIENT_PORT)) {
195             ports = getListClientPorts();
196         } else if (type.equals(PortType.SERVER_PORT)) {
197             ports = getListServerPorts();
198         } else
199             ports = getListControllerPorts();
200
201         for (int i = 0; i < ports.size(); i++) {
202             if ((((MyPort) ports.get(i)).getName().equals(name))
203                     && (((MyPort) ports.get(i)).getNumber() == number))
204                 return (MyPort) ports.get(i);
205         }
206         return null;
207     }
208
209     public String JavaDoc getName() {
210         return name;
211     }
212
213     public String JavaDoc getType() {
214         return type;
215     }
216
217     /**
218      * @param port
219      * @return the width of the label of the port
220      */

221     public int getPortLabelWidth(MyPort port) {
222         return port.getName().length() * PortGraphicsInterface.PORT_NAME_WIDTH;
223     }
224
225     /**
226      * Return the width of a line of a vertex.
227      * The lines of a vertex are determined by the number of client/server ports.
228      * The width is determinate with the width of the ports label and the width of the vertex label
229      * @param line, the line in the vertex
230      * @return the width of the line
231      */

232     private int getMinWidht(int line) {
233         int minWidth = 0;
234         List JavaDoc servers = getListServerPorts();
235         List JavaDoc clients = getListClientPorts();
236         int serversLength = servers.size();
237         int clientsLength = clients.size();
238         if ((serversLength > line) && (clientsLength > line)) {
239             MyPort server = (MyPort) servers.get(line);
240             MyPort client = (MyPort) clients.get(line);
241             if (getPortLabelWidth(server) > getPortLabelWidth(client))
242                 minWidth = getPortLabelWidth(server) * 2;
243             else
244                 minWidth = getPortLabelWidth(client) * 2;
245         } else if (clientsLength > line) {
246             MyPort client = (MyPort) clients.get(line);
247             minWidth = getPortLabelWidth(client) * 2;
248         } else if (serversLength > line){
249             MyPort server = (MyPort) servers.get(line);
250             minWidth = getPortLabelWidth(server) * 2;
251         }
252         return minWidth + getName().length()*VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH;
253     }
254
255     /**
256      * Returns the minimum width that the vertex must have to draw correctly his
257      * label and his ports label
258      * @return the minimum width that the vertex must have
259      */

260     public int getMinWidth() {
261         int minWidth = 0;
262         List JavaDoc servers = getListServerPorts();
263         List JavaDoc clients = getListClientPorts();
264         int serversLength = servers.size();
265         int clientsLength = clients.size();
266         if(serversLength>clientsLength){
267             for(int i=0;i<serversLength;i++){
268                 if(minWidth<getMinWidht(i))
269                     minWidth=getMinWidht(i);
270             }
271         }
272         else{
273             for(int i=0;i<clientsLength;i++){
274                 if(minWidth<getMinWidht(i))
275                     minWidth=getMinWidht(i);
276             }
277         }
278        
279         return minWidth;
280     }
281
282     public ArrayList JavaDoc getListServerPorts() {
283         if (serverPorts.size() == 0) {
284             List JavaDoc ports = getListPorts();
285             for (int i = 0; i < ports.size(); i++) {
286                 if (((MyPort) ports.get(i)).getType().equals(
287                         PortType.SERVER_PORT))
288                     serverPorts.add(ports.get(i));
289             }
290         }
291         return serverPorts;
292     }
293
294     public ArrayList JavaDoc getListClientPorts() {
295         if (clientPorts.size() == 0) {
296             List JavaDoc ports = getListPorts();
297             for (int i = 0; i < ports.size(); i++) {
298                 if ((((MyPort) ports.get(i)).getType()
299                         .equals(PortType.CLIENT_PORT))
300                         || (((MyPort) ports.get(i)).getType()
301                                 .equals(PortType.COLLECTION_PORT)))
302                     clientPorts.add(ports.get(i));
303             }
304         }
305         return clientPorts;
306     }
307
308     public ArrayList JavaDoc getListControllerPorts() {
309         if (controllerPorts.size() == 0) {
310             List JavaDoc ports = getListPorts();
311             for (int i = 0; i < ports.size(); i++) {
312                 if (((MyPort) ports.get(i)).getType().equals(
313                         PortType.CONTROLLER_PORT))
314                     controllerPorts.add(ports.get(i));
315             }
316         }
317         return controllerPorts;
318     }
319
320     /**
321      * Return the size of the vertex calculated in relation with the number of
322      * ports
323      */

324     public Dimension JavaDoc getSize() {
325         VertexGraphicsInterface vg = Graph.getVertexGraphics();
326             PortGraphicsInterface pg = Graph.getPortGraphics();
327             int portsServer = getListServerPorts().size();
328             int portsClients = getListClientPorts().size();
329             int portsControl = getListControllerPorts().size();
330             if (size.height < portsServer
331                     * PortGraphicsInterface.PORT_NAME_LENGHT) {
332                 size.height = portsServer
333                         * PortGraphicsInterface.PORT_NAME_LENGHT
334                         + PortGraphicsInterface.FIRST_PORT_Y;
335             }
336             if (size.height < portsClients
337                     * PortGraphicsInterface.PORT_NAME_LENGHT) {
338                 size.height = portsClients
339                         * PortGraphicsInterface.PORT_NAME_LENGHT
340                         + PortGraphicsInterface.FIRST_PORT_Y;
341             }
342             if (size.width < portsControl
343                     * PortGraphicsInterface.CONTROLLER_PORT_WIDTH) {
344                 size.width = portsControl
345                         * PortGraphicsInterface.CONTROLLER_PORT_WIDTH;
346             }
347             int minWidth=getMinWidth();
348             if(minWidth>VertexGraphicsInterface.VERTEX_MAX_WIDTH)
349                 minWidth=VertexGraphicsInterface.VERTEX_MAX_WIDTH;
350             if (size.width < minWidth)
351                 size.width = minWidth;
352             if ((size.height == 0)
353                     || (size.height < vg.getDefaultSize().height))
354                 size.height = vg.getDefaultSize().height;
355             if ((size.width == 0) || (size.width < vg.getDefaultSize().width))
356                 size.width = vg.getDefaultSize().width;
357         return size;
358     }
359
360     public void setSize(Dimension JavaDoc d) {
361         size.width = d.width;
362         size.height = d.height;
363     }
364
365     public Dimension JavaDoc getCompositeSize() {
366         return compositeSize;
367     }
368
369     public void setCompositeSize(Dimension JavaDoc d) {
370         compositeSize.width = d.width;
371         compositeSize.height = d.height;
372     }
373
374 }
Popular Tags