1 25 package org.objectweb.util.explorer.swing.graph; 26 27 import java.awt.Color ; 28 import java.awt.Dimension ; 29 import java.awt.Font ; 30 import java.awt.Graphics ; 31 import java.awt.Point ; 32 import java.awt.Rectangle ; 33 import java.awt.geom.Rectangle2D ; 36 import java.util.Map ; 38 39 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 56 private static boolean firstPaint = true; 57 58 59 protected static MyVertexRenderer renderer = new MyVertexRenderer(); 60 61 66 public CompositeVertexView(JGraph graph, org.jgraph.graph.CellMapper cm, 67 Object cell) { 68 super(cell, graph, cm); 69 } 70 71 public CellViewRenderer getRenderer() { 72 return renderer; 73 } 74 75 78 public Rectangle2D getBounds() { 79 CompositeVertex vertex = (CompositeVertex) getCell(); 80 Rectangle bounds = super.getBounds().getBounds(); 81 if (vertex.isOpen()) { 82 Point 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 (bounds.x, bounds.y)); 86 } 87 return bounds; 88 } 89 90 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 104 public static class MyVertexRenderer extends VertexRenderer { 105 106 107 private PortGraphicsInterface pg = Graph.getPortGraphics(); 108 109 110 private VertexGraphicsInterface vg = Graph.getVertexGraphics(); 111 112 private boolean isFirstPaint; 113 114 117 public MyVertexRenderer() { 118 super(); 119 isFirstPaint = true; 120 } 121 122 126 public void resizeVertex(Dimension size) { 127 CompositeVertex vertex = (CompositeVertex) view.getCell(); 128 vertex.setSize(size); 129 Map vAttributes = vertex.getAttributes(); 130 Rectangle 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 143 public void drawClosedCompositePortLabel(Graphics g, MyPort port, 144 Dimension 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 pos; 150 String portType = port.getType(); 151 int portLabelWidth = vertex.getPortLabelWidth(port); 152 153 if (portType.equals(PortType.SERVER_PORT)) { 155 int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y) 156 / portsServer; 157 pos = new Point ( 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 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 (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 { 210 int interval = size.width / portsControl; 211 pos = new Point (2 + interval * (port.getNumber() - 1), 212 PortGraphicsInterface.CONTROLLER_PORT_Y); 213 g.drawString(port.getName(), pos.x, pos.y); 214 } 215 216 } 217 218 225 public void drawOpenCompositePortLabel(Graphics g, MyPort port, 226 Dimension 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 if (!port.isGenerated()) { 236 Point pos; 237 String portType = port.getType(); 238 int portLabelWidth = vertex.getPortLabelWidth(port); 239 240 if (portType.equals(PortType.SERVER_PORT)) { 242 int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y) 244 / portsInternalServer; 245 pos = new Point ( 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 else if ((portType.equals(PortType.CLIENT_PORT)) 255 || ((portType.equals(PortType.COLLECTION_PORT)))) { 256 int interval = (size.height - PortGraphicsInterface.FIRST_PORT_Y) 258 / portsInternalClient; 259 pos = new Point (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 274 public void paintOpenComposite(Graphics g) { 275 CompositeVertex vertex = (CompositeVertex) view.getCell(); 276 Rectangle gBounds = g.getClipBounds(); 277 Dimension compositeSize = vg.getSuperCompositeSize(); 278 Dimension size = new Dimension (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 g.setColor(Color.black); 291 Font defaultFont = g.getFont(); 292 g.setFont(new Font ("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 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 338 } 348 349 } 350 351 355 public void paintClosedComposite(Graphics g) { 356 CompositeVertex vertex = (CompositeVertex) view.getCell(); 357 Dimension defaultSize = vg.getDefaultSize(); 359 Dimension size; 360 if (isFirstPaint) { 361 size = vertex.getSize(); 362 isFirstPaint = false; 363 } else 364 size = new Dimension (view.getBounds().getBounds().width, view 365 .getBounds().getBounds().height); 366 if (size.width < defaultSize.width) 368 size.width = defaultSize.width; 369 if (size.height < defaultSize.height) 370 size.height = defaultSize.height; 371 Map vAttributes = vertex.getAttributes(); 372 Rectangle gBounds = g.getClipBounds(); 373 gBounds.height = size.height; 374 gBounds.width = size.width; 375 g.setClip(gBounds.x, gBounds.y, gBounds.width, gBounds.height); 377 resizeVertex(size); 378 vg.drawVertex(g, vertex.getType(), size, vertex); 379 380 g.setColor(Color.black); 382 if (!vertex.isStarted()) 383 g.setColor(new Color (149, 0, 0)); 384 int vertexLabelLocation = size.width / 2 385 - (vertex.getName().length() 386 * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH +5)/2; 387 Font defaultFont = g.getFont(); 388 g.setFont(new Font ("TimesRoman", Font.PLAIN, 18)); 389 g.drawString(vertex.getName(), vertexLabelLocation, size.height / 2 390 + VertexGraphicsInterface.PRIMITIVE_NAME_HEIGHT); 391 g.setFont(defaultFont); 392 393 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 404 public void paint(Graphics g) { 405 CompositeVertex vertex = (CompositeVertex) view.getCell(); 406 if (vertex.isOpen()) { 407 paintOpenComposite(g); 408 } else { 409 paintClosedComposite(g); 410 } 411 } 412 } 413 414 417 public static class MySizeHandle extends SizeHandle { 418 419 424 public MySizeHandle(VertexView vertexview, GraphContext ctx) { 425 super(vertexview, ctx); 426 } 427 428 432 478 } 479 } | Popular Tags |