KickJava   Java API By Example, From Geeks To Geeks.

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


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

26
27 package org.objectweb.util.explorer.swing.graph;
28
29 import java.awt.Dimension JavaDoc;
30 import java.awt.Graphics JavaDoc;
31 import java.awt.Point JavaDoc;
32 import java.awt.Rectangle JavaDoc;
33 import java.awt.geom.Point2D JavaDoc;
34 import java.awt.geom.Rectangle2D JavaDoc;
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     /** The interface where are defined the graphics method to draw a port */
48     private PortGraphicsInterface pg;
49
50     /** The interface where are defined the graphics method to draw a vertex */
51     private VertexGraphicsInterface vg;
52
53     /** The port renderer*/
54     protected static MyPortRenderer renderer = new MyPortRenderer();
55
56     public MyPortView(JGraph graph, org.jgraph.graph.CellMapper cm, Object JavaDoc cell) {
57         super(cell, graph, cm);
58         pg = Graph.getPortGraphics();
59         vg = Graph.getVertexGraphics();
60     }
61
62     /**
63      * Returns the port lacation
64      */

65     public Point2D JavaDoc getLocation(EdgeView edge) {
66         Object JavaDoc 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 JavaDoc pos = null;
73         boolean isAbsolute = GraphConstants.isAbsolute(allAttributes);
74         Point2D JavaDoc offset = GraphConstants.getOffset(allAttributes);
75         VertexView vertexView = (VertexView) getParentView();
76         if (vertexView != null) {
77             if (edge != null) {
78                 MyPort p = (MyPort) getCell();
79                 String JavaDoc portType = p.getType();
80                 if (portType.equals(PortType.SERVER_PORT)) {
81                     pos = new Point JavaDoc((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 JavaDoc((int) getBounds().getX()
86                             + (int) getBounds().getWidth(), (int) getBounds()
87                             .getCenterY());
88                 } else if (portType.equals(PortType.CONTROLLER_PORT)) {
89                     pos = new Point JavaDoc((int) getBounds().getCenterX(),
90                             (int) getBounds().getY());
91                 }
92             }
93         }
94         return pos;
95     }
96
97     
98     /**
99      * Returns the bounds for the port view.
100      */

101     public Rectangle2D JavaDoc getBounds() {
102
103         Rectangle JavaDoc parentBounds = super.getParentView().getBounds().getBounds();
104         String JavaDoc portType = ((MyPort) getCell()).getType();
105         boolean isGenerated = ((MyPort) getCell()).isGenerated();
106         PrimitiveVertex vertex = (PrimitiveVertex) ((MyPort) getCell())
107                 .getParent();
108         Dimension JavaDoc vertexSize = new Dimension JavaDoc(parentBounds.width, parentBounds.height);
109         Rectangle JavaDoc bounds = new Rectangle JavaDoc();
110         bounds.height = pg.getPortSize(portType, false).height;
111         bounds.width = pg.getPortSize(portType, false).width;
112         int nbServerPorts, nbClientPorts, nbControllerPorts;
113         //if it's an internal port
114
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
126
else {
127             nbServerPorts = vertex.getListServerPorts().size();
128             nbClientPorts = vertex.getListClientPorts().size();
129             nbControllerPorts = vertex.getListControllerPorts().size();
130         }
131         //if the port is a server port
132
//the bounds must be at the left of the parent vertex
133
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         //if the port is a client port
146
//the bounds must be at the right of the parent vertex
147
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         //if the port is a client port
161
//the bounds must be at the up of the parent vertex
162
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         /**
180          * The method is called to paint the port
181          */

182         public void paint(Graphics JavaDoc g) {
183             PortGraphicsInterface pg = Graph.getPortGraphics();
184             MyPort port = (MyPort) view.getCell();
185             PrimitiveVertex vertex = (PrimitiveVertex) port.getParent();
186             String JavaDoc portType = port.getType();
187             Rectangle JavaDoc 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