2
* @(#)PortView.java 1.0 03-JUL-04
4
* Copyright (c) 2001-2004 Gaudenz Alder
7
package org.jgraph.graph;
9
import java.awt.geom.Point2D;
10
import java.awt.geom.Rectangle2D;
13
* The default implementation of a port view.
16
* @author Gaudenz Alder
19
public class PortView extends AbstractCellView {
21
/** Default size for all ports is 6. */
22
public static transient int SIZE = 6;
24
/** Renderer for the class. */
25
public static transient PortRenderer renderer = new PortRenderer();
28
* Controls if port magic should be allowed. Default is true. This is an
29
* easy switch to disable port magic for all instances of graphs.
31
public static boolean allowPortMagic = true;
33
/** Cache of the last valid parent. //FIX: Better solution? */
34
protected transient CellView lastParent;
37
* Constructs an empty portview.
44
* Constructs a view that holds a reference to the specified cell, anchor
48
* reference to the cell in the model
50
public PortView(Object cell) {
59
* This method ensures a non-null value. If the super method returns null
60
* then the last valid parent is returned. Note: If a vertex is removed, all
61
* ports will be replaced in connected edges. The ports are replaced by the
62
* center point of the <i>last </i> valid vertex view.
64
public CellView getParentView() {
65
CellView parent = super.getParentView();
74
* Returns the bounds for the port view.
76
public Rectangle2D getBounds() {
77
Point2D loc = getLocation();
84
Rectangle2D bounds = new Rectangle2D.Double(x, y, 0, 0);
85
bounds.setFrame(bounds.getX() - SIZE / 2, bounds.getY() - SIZE / 2,
91
* Returns a renderer for the class.
93
public CellViewRenderer getRenderer() {
98
* Returns <code>null</code>.
100
public CellHandle getHandle(GraphContext context) {
109
* Shortcut method to getLocation(null, null)
111
public Point2D getLocation() {
112
return getLocation(null, null);
116
* For backwards compatibility.
118
public Point2D getLocation(EdgeView edge) {
119
return getLocation(edge, null);
123
* Returns the point that the port represents with respect to
124
* <code>edge</code> and <code>point</code>, which is the nearest point
125
* to this port view on the edge. <code>edge</code> and <code>point</code>
126
* may be <code>null</code>.
128
public Point2D getLocation(EdgeView edge, Point2D nearest) {
129
CellView vertex = getParentView();
131
if (vertex != null) {
132
PortView anchor = null; // FIXME: (PortView)
133
// mapper.getMapping(modelAnchor, false); //
134
// Use refresh to get anchor view
135
Point2D offset = GraphConstants.getOffset(allAttributes);
136
// If No Edge Return Center
137
if (edge == null && offset == null)
138
pos = getCenterPoint(vertex);
140
if (offset != null) {
141
double x = offset.getX();
142
double y = offset.getY();
143
Rectangle2D r = vertex.getBounds();
145
boolean isAbsoluteX = GraphConstants.isAbsoluteX(allAttributes);
146
boolean isAbsoluteY = GraphConstants.isAbsoluteY(allAttributes);
148
x = x * (r.getWidth() - 1) / GraphConstants.PERMILLE;
151
y = y * (r.getHeight() - 1) / GraphConstants.PERMILLE;
152
} // Offset from Anchor
153
pos = (anchor != null) ? anchor.getLocation(edge, nearest)
154
: new Point2D.Double(r.getX(), r.getY());
155
pos = new Point2D.Double(pos.getX() + x, pos.getY() + y);
156
} else if (edge != null) {
158
if (nearest == null) {
159
// If "Dangling" Port Return Center
160
return getCenterPoint(vertex);
162
pos = vertex.getPerimeterPoint(edge, pos, nearest);
163
if (shouldInvokePortMagic(edge)) {
164
if (nearest != null) {
165
Rectangle2D r = vertex.getBounds();
166
if (nearest.getX() >= r.getX()
167
&& nearest.getX() <= r.getX() + r.getWidth()) {
168
pos.setLocation(nearest.getX(), pos.getY());
169
} else if (nearest.getY() >= r.getY()
170
&& nearest.getY() <= r.getY() + r.getHeight()) { // vertical
171
pos.setLocation(pos.getX(), nearest.getY());
173
if (nearest.getX() < r.getX())
174
pos.setLocation(r.getX(), pos.getY());
175
else if (nearest.getX() > r.getX() + r.getWidth())
177
.setLocation(r.getX() + r.getWidth(), pos
179
if (nearest.getY() < r.getY())
180
pos.setLocation(pos.getX(), r.getY());
181
else if (nearest.getY() > r.getY() + r.getHeight())
182
pos.setLocation(pos.getX(), r.getY()
192
* Subclassers can override this to decide whether or not "port magic"
193
* should appear on a given edge. (Port magic means the port tries to make
194
* the edge horizontal or vertical if the closest control point lies within
195
* the bounds of the parent vertex.)
197
protected boolean shouldInvokePortMagic(EdgeView edge) {
198
return allowPortMagic
199
&& !(getParentView() instanceof EdgeView)
200
&& edge.getPointCount() > 2
201
&& GraphConstants.getLineStyle(edge.getAllAttributes()) == GraphConstants.STYLE_ORTHOGONAL;
b'\\ No newline at end of file'