~ubuntu-branches/ubuntu/trusty/libjgraph-java/trusty

« back to all changes in this revision

Viewing changes to src/org/jgraph/graph/PortView.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2007-02-26 17:25:47 UTC
  • Revision ID: james.westby@ubuntu.com-20070226172547-k03sz4dbg197zr0p
Tags: upstream-5.10.0.1.dfsg
ImportĀ upstreamĀ versionĀ 5.10.0.1.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)PortView.java    1.0 03-JUL-04
 
3
 * 
 
4
 * Copyright (c) 2001-2004 Gaudenz Alder
 
5
 *  
 
6
 */
 
7
package org.jgraph.graph;
 
8
 
 
9
import java.awt.geom.Point2D;
 
10
import java.awt.geom.Rectangle2D;
 
11
 
 
12
/**
 
13
 * The default implementation of a port view.
 
14
 * 
 
15
 * @version 1.0 1/1/02
 
16
 * @author Gaudenz Alder
 
17
 */
 
18
 
 
19
public class PortView extends AbstractCellView {
 
20
 
 
21
        /** Default size for all ports is 6. */
 
22
        public static transient int SIZE = 6;
 
23
 
 
24
        /** Renderer for the class. */
 
25
        public static transient PortRenderer renderer = new PortRenderer();
 
26
 
 
27
        /**
 
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.
 
30
         */
 
31
        public static boolean allowPortMagic = true;
 
32
 
 
33
        /** Cache of the last valid parent. //FIX: Better solution? */
 
34
        protected transient CellView lastParent;
 
35
 
 
36
        /**
 
37
         * Constructs an empty portview.
 
38
         */
 
39
        public PortView() {
 
40
                super();
 
41
        }
 
42
 
 
43
        /**
 
44
         * Constructs a view that holds a reference to the specified cell, anchor
 
45
         * and parent vertex.
 
46
         * 
 
47
         * @param cell
 
48
         *            reference to the cell in the model
 
49
         */
 
50
        public PortView(Object cell) {
 
51
                super(cell);
 
52
        }
 
53
 
 
54
        //
 
55
        // CellView interface
 
56
        //
 
57
 
 
58
        /**
 
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.
 
63
         */
 
64
        public CellView getParentView() {
 
65
                CellView parent = super.getParentView();
 
66
                if (parent == null)
 
67
                        parent = lastParent;
 
68
                else
 
69
                        lastParent = parent;
 
70
                return parent;
 
71
        }
 
72
 
 
73
        /**
 
74
         * Returns the bounds for the port view.
 
75
         */
 
76
        public Rectangle2D getBounds() {
 
77
                Point2D loc = getLocation();
 
78
                double x = 0;
 
79
                double y = 0;
 
80
                if (loc != null) {
 
81
                        x = loc.getX();
 
82
                        y = loc.getY();
 
83
                }
 
84
                Rectangle2D bounds = new Rectangle2D.Double(x, y, 0, 0);
 
85
                bounds.setFrame(bounds.getX() - SIZE / 2, bounds.getY() - SIZE / 2,
 
86
                                SIZE, SIZE);
 
87
                return bounds;
 
88
        }
 
89
 
 
90
        /**
 
91
         * Returns a renderer for the class.
 
92
         */
 
93
        public CellViewRenderer getRenderer() {
 
94
                return renderer;
 
95
        }
 
96
 
 
97
        /**
 
98
         * Returns <code>null</code>.
 
99
         */
 
100
        public CellHandle getHandle(GraphContext context) {
 
101
                return null;
 
102
        }
 
103
 
 
104
        //
 
105
        // Special Methods
 
106
        //
 
107
 
 
108
        /**
 
109
         * Shortcut method to getLocation(null, null)
 
110
         */
 
111
        public Point2D getLocation() {
 
112
                return getLocation(null, null);
 
113
        }
 
114
 
 
115
        /**
 
116
         * For backwards compatibility.
 
117
         */
 
118
        public Point2D getLocation(EdgeView edge) {
 
119
                return getLocation(edge, null);
 
120
        }
 
121
 
 
122
        /**
 
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>.
 
127
         */
 
128
        public Point2D getLocation(EdgeView edge, Point2D nearest) {
 
129
                CellView vertex = getParentView();
 
130
                Point2D pos = null;
 
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);
 
139
                        // Apply Offset
 
140
                        if (offset != null) {
 
141
                                double x = offset.getX();
 
142
                                double y = offset.getY();
 
143
                                Rectangle2D r = vertex.getBounds();
 
144
                                // Absolute Offset
 
145
                                boolean isAbsoluteX = GraphConstants.isAbsoluteX(allAttributes);
 
146
                                boolean isAbsoluteY = GraphConstants.isAbsoluteY(allAttributes);
 
147
                                if (!isAbsoluteX) {
 
148
                                        x = x * (r.getWidth() - 1) / GraphConstants.PERMILLE;
 
149
                                }
 
150
                                if (!isAbsoluteY) {
 
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) {
 
157
                                // Floating Port
 
158
                                if (nearest == null) {
 
159
                                        // If "Dangling" Port Return Center
 
160
                                        return getCenterPoint(vertex);
 
161
                                }
 
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());
 
172
                                                }
 
173
                                                if (nearest.getX() < r.getX())
 
174
                                                        pos.setLocation(r.getX(), pos.getY());
 
175
                                                else if (nearest.getX() > r.getX() + r.getWidth())
 
176
                                                        pos
 
177
                                                                        .setLocation(r.getX() + r.getWidth(), pos
 
178
                                                                                        .getY());
 
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()
 
183
                                                                        + r.getHeight());
 
184
                                        }
 
185
                                }
 
186
                        }
 
187
                }
 
188
                return pos;
 
189
        }
 
190
 
 
191
        /**
 
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.)
 
196
         */
 
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;
 
202
        }
 
203
 
 
204
}
 
 
b'\\ No newline at end of file'