KickJava   Java API By Example, From Geeks To Geeks.

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


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
33 import java.awt.Rectangle JavaDoc;
34 //import java.awt.event.ActionEvent;
35
import java.awt.event.MouseEvent 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 PrimitiveVertexView extends VertexView {
54
55     /** The vertex renderer */
56     protected static MyVertexRenderer renderer = new MyVertexRenderer();
57
58     public PrimitiveVertexView(JGraph graph, org.jgraph.graph.CellMapper cm,
59             Object JavaDoc cell) {
60         super(cell, graph, cm);
61     }
62
63     public CellViewRenderer getRenderer() {
64         return renderer;
65     }
66
67     /**
68      * Returns the handle of the CompositeVertexView. In the handle, we can
69      * change the mouse actions
70      */

71     public CellHandle getHandle(GraphContext context) {
72         if (GraphConstants.isSizeable(getAllAttributes())
73                 && context.getGraph().isSizeable())
74             return new MySizeHandle(this, context);
75         return null;
76     }
77
78     public static class MyVertexRenderer extends VertexRenderer {
79
80         private PortGraphicsInterface pg = Graph.getPortGraphics();
81
82         private VertexGraphicsInterface vg = Graph.getVertexGraphics();
83
84         private boolean isFirstPaint;
85
86         public MyVertexRenderer() {
87             super();
88             isFirstPaint = true;
89         }
90
91         /**
92          * The method serves to resize a vertex
93          *
94          * @param size,
95          * the new size of the vertex
96          */

97         public void resizeVertex(Dimension JavaDoc size) {
98             PrimitiveVertex vertex = (PrimitiveVertex) view.getCell();
99             vertex.setSize(size);
100             Map JavaDoc vAttributes = vertex.getAttributes();
101             Rectangle JavaDoc r = GraphConstants.getBounds(vAttributes).getBounds();
102             r.height = size.height;
103             r.width = size.width;
104             GraphConstants.setBounds(vAttributes, r);
105             graph.getGraphLayoutCache().update(view);
106         }
107
108         /**
109          * The method is used to paint the vertex
110          */

111         public void paint(Graphics JavaDoc g) {
112             PrimitiveVertex vertex = (PrimitiveVertex) view.getCell();
113             int portsServer = vertex.getListServerPorts().size();
114             int portsClients = vertex.getListClientPorts().size();
115             int portsControl = vertex.getListControllerPorts().size();
116             //draw vertex
117
Dimension JavaDoc defaultSize = vg.getDefaultSize();
118             Dimension JavaDoc size;
119             Dimension JavaDoc initialSize = null;
120             if (isFirstPaint) {
121                 initialSize = vertex.getSize();
122                 size = initialSize;
123                 isFirstPaint = false;
124             } else
125                 size = new Dimension JavaDoc(view.getBounds().getBounds().width, view
126                         .getBounds().getBounds().height);
127             if (initialSize != null) {
128                 if (size.width < initialSize.width)
129                     size.width = initialSize.width;
130                 if (size.height < initialSize.height)
131                     size.height = initialSize.height;
132             }
133             if (size.width < defaultSize.width)
134                 size.width = defaultSize.width;
135             if (size.height < defaultSize.height)
136                 size.height = defaultSize.height;
137
138             Map JavaDoc vAttributes = vertex.getAttributes();
139             Rectangle JavaDoc gBounds = g.getClipBounds();
140
141             gBounds.height = size.height;
142             gBounds.width = size.width;
143             g.setClip(gBounds.x, gBounds.y, gBounds.width, gBounds.height);
144
145             resizeVertex(size);
146
147             vg.drawVertex(g, vertex.getType(), size, vertex);
148
149             //draw vertex label
150
g.setColor(Color.black);
151             if (!vertex.isStarted())
152                 g.setColor(new Color JavaDoc(149, 0, 0));
153             int vertexLabelLocation = size.width
154                     / 2
155                     - (vertex.getName().length()
156                             * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH + 5)
157                     / 2;
158             Font JavaDoc defaultFont = g.getFont();
159             g.setFont(new Font JavaDoc("TimesRoman", Font.PLAIN, 18));
160             g.drawString(vertex.getName(), vertexLabelLocation, size.height / 2
161                     + VertexGraphicsInterface.PRIMITIVE_NAME_HEIGHT);
162             g.setFont(defaultFont);
163
164             //draw ports labels
165
for (int i = 0; i < vertex.getChildCount(); i++) {
166                 MyPort port = (MyPort) vertex.getChildAt(i);
167                 Point JavaDoc pos;
168                 String JavaDoc portType = port.getType();
169                 int portLabelWidth = vertex.getPortLabelWidth(port);
170                 if (portType.equals(PortType.SERVER_PORT)) {
171                     int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y)
172                             / portsServer;
173                     pos = new Point JavaDoc(
174                             VertexGraphicsInterface.PRIMITIVE_MEMBRANE_SIZE + 2,
175                             interval * (port.getNumber() - 1)
176                                     + PortGraphicsInterface.FIRST_PORT_Y
177                                     + pg.getPortSize(portType, false).height);
178                     if (portLabelWidth > vertex.getSize().width / 2) {
179                         int newPortLabelSize = (getBounds().width / 2 - ((vertex
180                                 .getName().length() * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH) / 2))
181                                 / PortGraphicsInterface.PORT_NAME_WIDTH;
182                         if ((port.getName().length() > newPortLabelSize)
183                                 && (newPortLabelSize > 3))
184                             g.drawString(port.getName().substring(0,
185                                     newPortLabelSize - 3)
186                                     + "...", pos.x, pos.y);
187                         else {
188                             g.drawString(port.getName(), pos.x, pos.y);
189                         }
190
191                     } else
192                         g.drawString(port.getName(), pos.x, pos.y);
193                 } else if ((portType.equals(PortType.CLIENT_PORT))
194                         || (portType.equals(PortType.COLLECTION_PORT))) {
195                     int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y)
196                             / portsClients;
197                     pos = new Point JavaDoc(size.width - portLabelWidth
198                             - VertexGraphicsInterface.PRIMITIVE_MEMBRANE_SIZE
199                             - 3, interval * (port.getNumber() - 1)
200                             + PortGraphicsInterface.FIRST_PORT_Y
201                             + pg.getPortSize(portType, false).height);
202                     if (portLabelWidth > vertex.getSize().width / 2) {
203                         int vertexLabelSize = vertex.getName().length()
204                                 * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH;
205                         int newPortLabelSize = (getBounds().width / 2 - ((vertex
206                                 .getName().length() * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH) / 2))
207                                 / PortGraphicsInterface.PORT_NAME_WIDTH;
208                         pos.x = getBounds().width
209                                 - (newPortLabelSize * PortGraphicsInterface.PORT_NAME_WIDTH);
210                         if ((port.getName().length() > newPortLabelSize)
211                                 && (newPortLabelSize > 3))
212                             g.drawString(port.getName().substring(0,
213                                     newPortLabelSize - 3)
214                                     + "...", pos.x, pos.y);
215                         else {
216                             g.drawString(port.getName(), pos.x, pos.y);
217                         }
218                     } else
219                         g.drawString(port.getName(), pos.x, pos.y);
220                 } else {
221                     int interval = size.width / portsControl;
222                     pos = new Point JavaDoc(2 + interval * (port.getNumber() - 1),
223                             PortGraphicsInterface.CONTROLLER_PORT_Y);
224                     g.drawString(port.getName(), pos.x, pos.y);
225                 }
226             }
227         }
228     }
229
230     public static class MySizeHandle extends SizeHandle {
231
232         public MySizeHandle(VertexView vertexview, GraphContext ctx) {
233             super(vertexview, ctx);
234         }
235
236         /**
237          * In this method, we have to add the same popup that there is in the
238          * tree
239          */

240         public void mousePressed(MouseEvent JavaDoc event) {
241             super.mousePressed(event);
242             /*
243             if (SwingUtilities.isRightMouseButton(event)) {
244                 int x = event.getX();
245                 int y = event.getY();
246                 final PrimitiveVertex v = (PrimitiveVertex) vertex.getCell();
247                 System.out.println("click droit sur : " + v);
248                 //create a jpopup
249                 final JPopupMenu popup = new JPopupMenu();
250
251                 //create actions
252                 Action demarrerAction = new AbstractAction("DEMARRER") {
253                     public void actionPerformed(ActionEvent e) {
254                         v.setStarted(true);
255                         HashMap nested = new HashMap();
256                         Map vAttributes = vertex.getAttributes();
257                         nested.put(vertex, vAttributes);
258                         graph.getGraphLayoutCache().edit(nested, null, null,
259                                 null);
260                         graph.getGraphLayoutCache().update(vertex);
261                     }
262                 };
263
264                 Action arreterAction = new AbstractAction("ARRETER") {
265                     public void actionPerformed(ActionEvent e) {
266                         v.setStarted(false);
267                         HashMap nested = new HashMap();
268                         Map vAttributes = vertex.getAttributes();
269                         nested.put(vertex, vAttributes);
270                         graph.getGraphLayoutCache().edit(nested, null, null,
271                                 null);
272                         graph.getGraphLayoutCache().update(vertex);
273                     }
274                 };
275
276                 //create jmenus
277                 JMenuItem demarrer = new JMenuItem(demarrerAction);
278                 JMenuItem arreter = new JMenuItem(arreterAction);
279
280                 //insert the menus inside the popup
281                 popup.add(demarrer);
282                 popup.addSeparator();
283                 popup.add(arreter);
284                 popup.show(graph, x, y);
285
286             }*/

287         }
288     }
289 }
Popular Tags