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 33 import java.awt.Rectangle ; 34 import java.awt.event.MouseEvent ; 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 PrimitiveVertexView extends VertexView { 54 55 56 protected static MyVertexRenderer renderer = new MyVertexRenderer(); 57 58 public PrimitiveVertexView(JGraph graph, org.jgraph.graph.CellMapper cm, 59 Object cell) { 60 super(cell, graph, cm); 61 } 62 63 public CellViewRenderer getRenderer() { 64 return renderer; 65 } 66 67 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 97 public void resizeVertex(Dimension size) { 98 PrimitiveVertex vertex = (PrimitiveVertex) view.getCell(); 99 vertex.setSize(size); 100 Map vAttributes = vertex.getAttributes(); 101 Rectangle 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 111 public void paint(Graphics 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 Dimension defaultSize = vg.getDefaultSize(); 118 Dimension size; 119 Dimension initialSize = null; 120 if (isFirstPaint) { 121 initialSize = vertex.getSize(); 122 size = initialSize; 123 isFirstPaint = false; 124 } else 125 size = new Dimension (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 vAttributes = vertex.getAttributes(); 139 Rectangle 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 g.setColor(Color.black); 151 if (!vertex.isStarted()) 152 g.setColor(new Color (149, 0, 0)); 153 int vertexLabelLocation = size.width 154 / 2 155 - (vertex.getName().length() 156 * VertexGraphicsInterface.PRIMITIVE_NAME_WIDTH + 5) 157 / 2; 158 Font defaultFont = g.getFont(); 159 g.setFont(new Font ("TimesRoman", Font.PLAIN, 18)); 160 g.drawString(vertex.getName(), vertexLabelLocation, size.height / 2 161 + VertexGraphicsInterface.PRIMITIVE_NAME_HEIGHT); 162 g.setFont(defaultFont); 163 164 for (int i = 0; i < vertex.getChildCount(); i++) { 166 MyPort port = (MyPort) vertex.getChildAt(i); 167 Point pos; 168 String 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 ( 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 (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 (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 240 public void mousePressed(MouseEvent event) { 241 super.mousePressed(event); 242 287 } 288 } 289 } | Popular Tags |