1 26 27 package org.objectweb.util.explorer.swing.graph; 28 29 import java.awt.Dimension ; 30 import java.awt.Graphics ; 31 import java.awt.Point ; 32 import java.awt.Rectangle ; 33 import java.awt.geom.Point2D ; 34 import java.awt.geom.Rectangle2D ; 35 36 import org.jgraph.JGraph; 37 import org.jgraph.graph.CellViewRenderer; 38 import org.jgraph.graph.EdgeView; 39 import org.jgraph.graph.GraphConstants; 40 import org.jgraph.graph.Port; 41 import org.jgraph.graph.PortRenderer; 42 import org.jgraph.graph.PortView; 43 import org.jgraph.graph.VertexView; 44 45 public class MyPortView extends PortView { 46 47 48 private PortGraphicsInterface pg; 49 50 51 private VertexGraphicsInterface vg; 52 53 54 protected static MyPortRenderer renderer = new MyPortRenderer(); 55 56 public MyPortView(JGraph graph, org.jgraph.graph.CellMapper cm, Object cell) { 57 super(cell, graph, cm); 58 pg = Graph.getPortGraphics(); 59 vg = Graph.getVertexGraphics(); 60 } 61 62 65 public Point2D getLocation(EdgeView edge) { 66 Object modelAnchor = null; 67 if (cell instanceof Port) 68 modelAnchor = ((Port) cell).getAnchor(); 69 MyPort port = (MyPort) cell; 70 PrimitiveVertex vertex = (PrimitiveVertex) ((MyPort) cell).getParent(); 71 PortView anchor = (PortView) mapper.getMapping(modelAnchor, false); 72 Point2D pos = null; 73 boolean isAbsolute = GraphConstants.isAbsolute(allAttributes); 74 Point2D offset = GraphConstants.getOffset(allAttributes); 75 VertexView vertexView = (VertexView) getParentView(); 76 if (vertexView != null) { 77 if (edge != null) { 78 MyPort p = (MyPort) getCell(); 79 String portType = p.getType(); 80 if (portType.equals(PortType.SERVER_PORT)) { 81 pos = new Point ((int) getBounds().getX(), (int) getBounds() 82 .getCenterY()); 83 } else if ((portType.equals(PortType.CLIENT_PORT)) 84 || ((portType.equals(PortType.COLLECTION_PORT)))) { 85 pos = new Point ((int) getBounds().getX() 86 + (int) getBounds().getWidth(), (int) getBounds() 87 .getCenterY()); 88 } else if (portType.equals(PortType.CONTROLLER_PORT)) { 89 pos = new Point ((int) getBounds().getCenterX(), 90 (int) getBounds().getY()); 91 } 92 } 93 } 94 return pos; 95 } 96 97 98 101 public Rectangle2D getBounds() { 102 103 Rectangle parentBounds = super.getParentView().getBounds().getBounds(); 104 String portType = ((MyPort) getCell()).getType(); 105 boolean isGenerated = ((MyPort) getCell()).isGenerated(); 106 PrimitiveVertex vertex = (PrimitiveVertex) ((MyPort) getCell()) 107 .getParent(); 108 Dimension vertexSize = new Dimension (parentBounds.width, parentBounds.height); 109 Rectangle bounds = new Rectangle (); 110 bounds.height = pg.getPortSize(portType, false).height; 111 bounds.width = pg.getPortSize(portType, false).width; 112 int nbServerPorts, nbClientPorts, nbControllerPorts; 113 if ((vertex instanceof CompositeVertex) 115 && ((CompositeVertex) vertex).isOpen()) { 116 bounds.height = pg.getPortSize(portType, true).height; 117 bounds.width = pg.getPortSize(portType, true).width; 118 nbServerPorts = ((CompositeVertex) vertex) 119 .getListInternalServerPorts().size(); 120 nbClientPorts = ((CompositeVertex) vertex) 121 .getListInternalClientPorts().size(); 122 nbControllerPorts = ((CompositeVertex) vertex) 123 .getListInternalControllerPorts().size(); 124 } 125 else { 127 nbServerPorts = vertex.getListServerPorts().size(); 128 nbClientPorts = vertex.getListClientPorts().size(); 129 nbControllerPorts = vertex.getListControllerPorts().size(); 130 } 131 if (portType.equals(PortType.SERVER_PORT)) { 134 int interval = (vertexSize.height - PortGraphicsInterface.FIRST_PORT_Y) 135 / nbServerPorts; 136 if (!isGenerated) { 137 bounds.x = parentBounds.x - bounds.width; 138 } else { 139 bounds.x = parentBounds.x + vertexSize.width - bounds.width 140 - VertexGraphicsInterface.COMPOSITE_MEMBRANE_SIZE; 141 } 142 bounds.y = PortGraphicsInterface.FIRST_PORT_Y + parentBounds.y 143 + interval * (((MyPort) getCell()).getNumber() - 1); 144 } 145 else if ((portType.equals(PortType.CLIENT_PORT)) 148 || (portType.equals(PortType.COLLECTION_PORT))) { 149 int interval = (vertexSize.height - PortGraphicsInterface.FIRST_PORT_Y) 150 / nbClientPorts; 151 if (!isGenerated) { 152 bounds.x = parentBounds.x + vertexSize.width; 153 } else { 154 bounds.x = parentBounds.x 155 + VertexGraphicsInterface.COMPOSITE_MEMBRANE_SIZE; 156 } 157 bounds.y = PortGraphicsInterface.FIRST_PORT_Y + parentBounds.y 158 + interval * (((MyPort) getCell()).getNumber() - 1); 159 } 160 else if (portType.equals(PortType.CONTROLLER_PORT)) { 163 int interval = vertexSize.width / nbControllerPorts; 164 bounds.x = parentBounds.x + interval 165 * (((MyPort) getCell()).getNumber() - 1) 166 + pg.getPortSize(portType, false).width; 167 bounds.y = parentBounds.y - bounds.height; 168 } 169 170 return bounds; 171 } 172 173 public CellViewRenderer getRenderer() { 174 return renderer; 175 } 176 177 public static class MyPortRenderer extends PortRenderer { 178 179 182 public void paint(Graphics g) { 183 PortGraphicsInterface pg = Graph.getPortGraphics(); 184 MyPort port = (MyPort) view.getCell(); 185 PrimitiveVertex vertex = (PrimitiveVertex) port.getParent(); 186 String portType = port.getType(); 187 Rectangle bounds = g.getClipBounds(); 188 boolean isInternal = false; 189 if ((vertex instanceof CompositeVertex) 190 && (((CompositeVertex) vertex).isOpen())) { 191 isInternal = true; 192 } else { 193 bounds.height = pg.getPortSize(portType, isInternal).height; 194 bounds.width = pg.getPortSize(portType, isInternal).width; 195 } 196 g.setClip(bounds.x, bounds.y, bounds.width, bounds.height); 197 if (!((portType.equals(PortType.CONTROLLER_PORT)) && (!(vertex 198 .getControllerDisplay())))) 199 pg.drawPort(g, port.getName(), portType, isInternal); 200 } 201 202 } 203 204 } | Popular Tags |