KickJava   Java API By Example, From Geeks To Geeks.

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


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

25 package org.objectweb.util.explorer.swing.graph;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.awt.Graphics JavaDoc;
31 import java.awt.Point JavaDoc;
32 import java.awt.Rectangle JavaDoc;
33 //import java.awt.event.ActionEvent;
34
//import java.awt.event.MouseEvent;
35
import java.awt.geom.Rectangle2D JavaDoc;
36 //import java.util.HashMap;
37
import java.util.Map JavaDoc;
38
39 //import javax.swing.AbstractAction;
40
//import javax.swing.Action;
41
//import javax.swing.JMenuItem;
42
//import javax.swing.JPopupMenu;
43
//import javax.swing.SwingUtilities;
44

45 import org.jgraph.JGraph;
46 import org.jgraph.graph.CellHandle;
47 import org.jgraph.graph.CellViewRenderer;
48 import org.jgraph.graph.GraphConstants;
49 import org.jgraph.graph.GraphContext;
50 import org.jgraph.graph.VertexRenderer;
51 import org.jgraph.graph.VertexView;
52
53 public class CompositeVertexView extends VertexView {
54
55     /** Boolean that indicates if it's the first passage into the view */
56     private static boolean firstPaint = true;
57
58     /** The renderer of the view */
59     protected static MyVertexRenderer renderer = new MyVertexRenderer();
60
61     /**
62      * @param graph
63      * @param cm
64      * @param cell
65      */

66     public CompositeVertexView(JGraph graph, org.jgraph.graph.CellMapper cm,
67             Object JavaDoc cell) {
68         super(cell, graph, cm);
69     }
70
71     public CellViewRenderer getRenderer() {
72         return renderer;
73     }
74
75     /**
76      * Returns the bounds of the compositeVertexView.
77      */

78     public Rectangle2D JavaDoc getBounds() {
79         CompositeVertex vertex = (CompositeVertex) getCell();
80         Rectangle JavaDoc bounds = super.getBounds().getBounds();
81         if (vertex.isOpen()) {
82             Point JavaDoc origin = VertexGraphicsInterface.SUPER_COMPOSITE_ORIGIN;
83             bounds.x = bounds.x + origin.x;
84             bounds.y = bounds.y + origin.y;
85             ((CompositeVertex) vertex).setOrigin(new Point JavaDoc(bounds.x, bounds.y));
86         }
87         return bounds;
88     }
89
90     /**
91      * Returns the handle of the CompositeVertexView. In the handle, we can
92      * change the mouse actions
93      */

94     public CellHandle getHandle(GraphContext context) {
95         if (GraphConstants.isSizeable(getAllAttributes())
96                 && context.getGraph().isSizeable())
97             return new MySizeHandle(this, context);
98         return null;
99     }
100
101     /**
102      * Renderer of a vertex.
103      */

104     public static class MyVertexRenderer extends VertexRenderer {
105
106         /** The interface that determinate the graphics methods of a port */
107         private PortGraphicsInterface pg = Graph.getPortGraphics();
108
109         /** The interface that determinate the graphics methods of a vertex */
110         private VertexGraphicsInterface vg = Graph.getVertexGraphics();
111
112         private boolean isFirstPaint;
113
114         /**
115          * Default constructor
116          */

117         public MyVertexRenderer() {
118             super();
119             isFirstPaint = true;
120         }
121
122         /**
123          * The method serves to resize a vertex
124          * @param size the new size of the vertex
125          */

126         public void resizeVertex(Dimension JavaDoc size) {
127             CompositeVertex vertex = (CompositeVertex) view.getCell();
128             vertex.setSize(size);
129             Map JavaDoc vAttributes = vertex.getAttributes();
130             Rectangle JavaDoc r = GraphConstants.getBounds(vAttributes).getBounds();
131             r.height = size.height;
132             r.width = size.width;
133             GraphConstants.setBounds(vAttributes, r);
134             graph.getGraphLayoutCache().update(view);
135         }
136
137         /**
138          * Draw a port on an closed composite vertex
139          * @param g the graphics
140          * @param port the port to paint
141          * @param size the size of the closed composite vertex
142          */

143         public void drawClosedCompositePortLabel(Graphics JavaDoc g, MyPort port,
144                 Dimension JavaDoc size) {
145             CompositeVertex vertex = (CompositeVertex) view.getCell();
146             int portsServer = vertex.getListServerPorts().size();
147             int portsClients = vertex.getListClientPorts().size();
148             int portsControl = vertex.getListControllerPorts().size();
149             Point JavaDoc pos;
150             String JavaDoc portType = port.getType();
151             int portLabelWidth = vertex.getPortLabelWidth(port);
152
153             //if the port is a server port
154
if (portType.equals(PortType.SERVER_PORT)) {
155                 int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y)
156                         / portsServer;
157                 pos = new Point JavaDoc(
158                         VertexGraphicsInterface.PRIMITIVE_MEMBRANE_SIZE + 2,
159                         interval * (port.getNumber() - 1)
160                                 + PortGraphicsInterface.FIRST_PORT_Y
161                                 + pg.getPortSize(portType, false).height);
162                 if (portLabelWidth > vertex.getSize().width / 2) {
163                     int newPortLabelSize = (this.getBounds().width / 2 - ((vertex
164                             .getName().length() * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH)/2))
165                             / PortGraphicsInterface.PORT_NAME_WIDTH;
166                     if ((port.getName().length() > newPortLabelSize)
167                             && (newPortLabelSize > 3))
168                         g.drawString(port.getName().substring(0,
169                                 newPortLabelSize - 3)
170                                 + "...", pos.x, pos.y);
171                     else {
172                         g.drawString(port.getName(), pos.x, pos.y);
173                     }
174                 } else
175                     g.drawString(port.getName(), pos.x, pos.y);
176             }
177
178             //if the port is a client port
179
else if ((portType.equals(PortType.CLIENT_PORT))
180                     || (portType.equals(PortType.COLLECTION_PORT))) {
181                 int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y)
182                         / portsClients;
183                 pos = new Point JavaDoc(size.width - portLabelWidth
184                         - VertexGraphicsInterface.PRIMITIVE_MEMBRANE_SIZE - 3,
185                         interval * (port.getNumber() - 1)
186                                 + PortGraphicsInterface.FIRST_PORT_Y
187                                 + pg.getPortSize(portType, false).height);
188                 if (portLabelWidth > vertex.getSize().width / 2) {
189                     int vertexLabelSize = vertex.getName().length()
190                             * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH;
191                     int newPortLabelSize = (this.getBounds().width / 2 - ((vertex
192                             .getName().length() * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH)/2))
193                             / PortGraphicsInterface.PORT_NAME_WIDTH;
194                     pos.x = this.getBounds().width
195                             - (newPortLabelSize * PortGraphicsInterface.PORT_NAME_WIDTH);
196                     if ((port.getName().length() > newPortLabelSize)
197                             && (newPortLabelSize > 3))
198                         g.drawString(port.getName().substring(0,
199                                 newPortLabelSize - 3)
200                                 + "...", pos.x, pos.y);
201                     else {
202                         g.drawString(port.getName(), pos.x, pos.y);
203                     }
204                 } else
205                     g.drawString(port.getName(), pos.x, pos.y);
206             }
207
208             //else, if the prot is a controller
209
else {
210                 int interval = size.width / portsControl;
211                 pos = new Point JavaDoc(2 + interval * (port.getNumber() - 1),
212                         PortGraphicsInterface.CONTROLLER_PORT_Y);
213                 g.drawString(port.getName(), pos.x, pos.y);
214             }
215
216         }
217
218         /**
219          * Draw a port on an open composite vertex
220          *
221          * @param g the graphics
222          * @param port the port to paint
223          * @param size the size of the open composite vertex
224          */

225         public void drawOpenCompositePortLabel(Graphics JavaDoc g, MyPort port,
226                 Dimension JavaDoc size) {
227             CompositeVertex vertex = (CompositeVertex) view.getCell();
228             int portsInternalServer = ((CompositeVertex) vertex)
229                     .getListInternalServerPorts().size();
230             int portsInternalClient = ((CompositeVertex) vertex)
231                     .getListInternalClientPorts().size();
232
233             //The name of the port generated isn't draw
234
//(the port generated has the same name that the internal port)
235
if (!port.isGenerated()) {
236                 Point JavaDoc pos;
237                 String JavaDoc portType = port.getType();
238                 int portLabelWidth = vertex.getPortLabelWidth(port);
239
240                 //if the port is a server port
241
if (portType.equals(PortType.SERVER_PORT)) {
242                     //interval that separate two server ports
243
int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y)
244                             / portsInternalServer;
245                     pos = new Point JavaDoc(
246                             VertexGraphicsInterface.COMPOSITE_MEMBRANE_SIZE + 3,
247                             interval * (port.getNumber() - 1)
248                                     + PortGraphicsInterface.FIRST_PORT_Y);
249                     g.drawString(port.getName(), pos.x, pos.y
250                             + PortGraphicsInterface.FIRST_PORT_Y);
251                 }
252
253                 //if the port is a client port
254
else if ((portType.equals(PortType.CLIENT_PORT))
255                         || ((portType.equals(PortType.COLLECTION_PORT)))) {
256                     //interval that separate two client ports
257
int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y)
258                             / portsInternalClient;
259                     pos = new Point JavaDoc(size.width - portLabelWidth
260                             - VertexGraphicsInterface.COMPOSITE_MEMBRANE_SIZE
261                             - 4, interval * (port.getNumber() - 1)
262                             + PortGraphicsInterface.FIRST_PORT_Y);
263                     g.drawString(port.getName(), pos.x, pos.y
264                             + PortGraphicsInterface.FIRST_PORT_Y);
265                 }
266             }
267
268         }
269
270         /**
271          * Paint an open composite vertex
272          * @param g the graphics
273          */

274         public void paintOpenComposite(Graphics JavaDoc g) {
275             CompositeVertex vertex = (CompositeVertex) view.getCell();
276             Rectangle JavaDoc gBounds = g.getClipBounds();
277             Dimension JavaDoc compositeSize = vg.getSuperCompositeSize();
278             Dimension JavaDoc size = new Dimension JavaDoc(view.getBounds().getBounds().width,
279                     view.getBounds().getBounds().height);
280             gBounds.height = size.height;
281             gBounds.width = size.width;
282             if (size.width < compositeSize.width)
283                 size.width = compositeSize.width;
284             if (size.height < compositeSize.height)
285                 size.height = compositeSize.height;
286             g.setClip(gBounds.x, gBounds.y, size.width, size.height);
287             resizeVertex(size);
288             vg.drawVertex(g, vertex.getType(), size, vertex);
289             //draw label
290
g.setColor(Color.black);
291             Font JavaDoc defaultFont = g.getFont();
292             g.setFont(new Font JavaDoc("TimesRoman", Font.PLAIN, 18));
293             g.drawString(vertex.getName(),
294                     VertexGraphicsInterface.SUPER_COMPOSITE_NAME_LOCATION.x,
295                     VertexGraphicsInterface.SUPER_COMPOSITE_NAME_LOCATION.y);
296             g.setFont(defaultFont);
297             //draw ports label
298
for (int i = 0; i < vertex.getChildCount(); i++) {
299                 MyPort port = (MyPort) vertex.getChildAt(i);
300                 drawOpenCompositePortLabel(g, port, size);
301             }
302             if (firstPaint)
303                 firstPaint = false;
304             else {
305                 /** *********************************************************************** */
306                 // A ce niveau, il faudrait pouvoir appliquer l'algorithm de
307
// placement au composant composite (composite vertex) afin
308
// ques les
309
// sous-composants se replacent correctement
310
// J'ai essayé :
311
// -> Appliquer l'algorithm à partir du deuxième passage
312
// dans cette méthode.
313
// Problème rencontré :
314
// -> Une boucle dans l'algoritm dans la méthode
315
// refreshGraph au niveau de la méthode
316
// graph.getGraphLayoutCache().edit(...)
317
// Cette méthode rafraichit tout le model, c'est pourquoi la
318
// méthode paint était rappellée
319
// et relançait l'algo.
320
// Solution apportée :
321
// 1 -> Utiliser une méthode différente de la méthode "edit"
322
// :
323
// refresh de toutes les cellules, refresh de la cellule
324
// concernée, update, validate, revalidate,...
325
// 2 -> Ajout d'un booléen dans cette classe :
326
// "isRefreshed", et appliquer l'algo seulement lorsque ce
327
// booléen est à "true". Avant d'appliquer l'algo, on met ce
328
// booléen
329
// à false afin d'éviter la boucle.
330
// Au final :
331
// 1 -> Aucune de ces méthodes utilisées seules ou à la
332
// suite apportent un résultat aussi satisfaisant que celui
333
// qu'apporte la méthode edit
334
// (mauvais rafraichissement des ports ou des vertex,
335
// impossibilité de bouger les vertex, ...)
336
// 2 -> la boucle presiste toujours ...
337
/** *********************************************************************** */
338                 // if(isRefreshed){
339
// LayoutAlgorithm algo = ((MyCompositeVertex) vertex)
340
// .getAlgorithm();
341
// MyCompositeVertex cv = (MyCompositeVertex) vertex;
342
// //isRefreshed = false;
343
// algo.applyLayout(graph, new Point(getBounds().x,
344
// getBounds().y), cv.getSize());
345
// isRefreshed = false;
346
// }
347
}
348
349         }
350
351         /**
352          * Paint an closed composite vertex
353          * @param g the graphics
354          */

355         public void paintClosedComposite(Graphics JavaDoc g) {
356             CompositeVertex vertex = (CompositeVertex) view.getCell();
357             //draw vertex
358
Dimension JavaDoc defaultSize = vg.getDefaultSize();
359             Dimension JavaDoc size;
360             if (isFirstPaint) {
361                 size = vertex.getSize();
362                 isFirstPaint = false;
363             } else
364                 size = new Dimension JavaDoc(view.getBounds().getBounds().width, view
365                         .getBounds().getBounds().height);
366             //the size of the vertex must be larger than the default size
367
if (size.width < defaultSize.width)
368                 size.width = defaultSize.width;
369             if (size.height < defaultSize.height)
370                 size.height = defaultSize.height;
371             Map JavaDoc vAttributes = vertex.getAttributes();
372             Rectangle JavaDoc gBounds = g.getClipBounds();
373             gBounds.height = size.height;
374             gBounds.width = size.width;
375             //resize the graphics
376
g.setClip(gBounds.x, gBounds.y, gBounds.width, gBounds.height);
377             resizeVertex(size);
378             vg.drawVertex(g, vertex.getType(), size, vertex);
379
380             //draw vertex label
381
g.setColor(Color.black);
382             if (!vertex.isStarted())
383                 g.setColor(new Color JavaDoc(149, 0, 0));
384             int vertexLabelLocation = size.width / 2
385             - (vertex.getName().length()
386             * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH +5)/2;
387             Font JavaDoc defaultFont = g.getFont();
388             g.setFont(new Font JavaDoc("TimesRoman", Font.PLAIN, 18));
389             g.drawString(vertex.getName(), vertexLabelLocation, size.height / 2
390                     + VertexGraphicsInterface.PRIMITIVE_NAME_HEIGHT);
391             g.setFont(defaultFont);
392
393             //draw ports labels
394
for (int i = 0; i < vertex.getChildCount(); i++) {
395                 MyPort port = (MyPort) vertex.getChildAt(i);
396                 drawClosedCompositePortLabel(g, port, size);
397             }
398
399         }
400
401         /**
402          * The method is used to paint the vertex
403          */

404         public void paint(Graphics JavaDoc g) {
405             CompositeVertex vertex = (CompositeVertex) view.getCell();
406             if (vertex.isOpen()) {
407                 paintOpenComposite(g);
408             } else {
409                 paintClosedComposite(g);
410             }
411         }
412     }
413
414     /**
415      * The size of the Handle
416      */

417     public static class MySizeHandle extends SizeHandle {
418
419         /**
420          * Default constructor
421          * @param vertexview
422          * @param ctx
423          */

424         public MySizeHandle(VertexView vertexview, GraphContext ctx) {
425             super(vertexview, ctx);
426         }
427
428         /**
429          * In this method, we have to add the same popup that there is in the
430          * tree
431          */

432         /*
433         public void mousePressed(MouseEvent event) {
434             super.mousePressed(event);
435             if (SwingUtilities.isRightMouseButton(event)) {
436                 int x = event.getX();
437                 int y = event.getY();
438                 final PrimitiveVertex v = (PrimitiveVertex) vertex.getCell();
439                 //create a jpopup
440                 final JPopupMenu popup = new JPopupMenu();
441
442                 //create actions
443                 Action demarrerAction = new AbstractAction("DEMARRER") {
444                     public void actionPerformed(ActionEvent e) {
445                         v.setStarted(true);
446                         HashMap nested = new HashMap();
447                         Map vAttributes = vertex.getAttributes();
448                         nested.put(vertex, vAttributes);
449                         graph.getGraphLayoutCache().edit(nested, null, null,
450                                 null);
451                         graph.getGraphLayoutCache().update(vertex);
452                     }
453                 };
454
455                 Action arreterAction = new AbstractAction("ARRETER") {
456                     public void actionPerformed(ActionEvent e) {
457                         v.setStarted(false);
458                         HashMap nested = new HashMap();
459                         Map vAttributes = vertex.getAttributes();
460                         nested.put(vertex, vAttributes);
461                         graph.getGraphLayoutCache().edit(nested, null, null,
462                                 null);
463                         graph.getGraphLayoutCache().update(vertex);
464                     }
465                 };
466
467                 //create jmenus
468                 JMenuItem demarrer = new JMenuItem(demarrerAction);
469                 JMenuItem arreter = new JMenuItem(arreterAction);
470
471                 //insert the menus inside the popup
472                 popup.add(demarrer);
473                 popup.addSeparator();
474                 popup.add(arreter);
475                 popup.show(graph, x, y);
476             }
477         }*/

478     }
479 }
Popular Tags