comparison dwtx/draw2d/graph/Node.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2003, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.graph.Node;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17 import tango.text.convert.Format;
18
19 import dwtx.draw2d.geometry.Dimension;
20 import dwtx.draw2d.geometry.Insets;
21 import dwtx.draw2d.graph.EdgeList;
22 import dwtx.draw2d.graph.Subgraph;
23 import dwtx.draw2d.graph.Edge;
24
25 /**
26 * A node in a DirectedGraph. A node has 0 or more incoming and outgoing {@link Edge}s. A
27 * node is given a width and height by the client. When a layout places the node in the
28 * graph, it will determine the node's x and y location. It may also modify the node's
29 * height.
30 *
31 * A node represents both the <EM>input</EM> and the <EM>output</EM> for a layout
32 * algorithm. The following fields are used as input to a graph layout:
33 * <UL>
34 * <LI>{@link #width} - the node's width.
35 * <LI>{@link #height} - the node's height.
36 * <LI>{@link #outgoing} - the node's outgoing edges.
37 * <LI>{@link #incoming} - the node's incoming edges.
38 * <LI>padding - the amount of space to be left around the outside of the node.
39 * <LI>{@link #incomingOffset} - the default attachment point for incoming edges.
40 * <LI>{@link #outgoingOffset} - the default attachment point for outgoing edges.
41 * <LI>parent - the parent subgraph containing this node.
42 * </UL>
43 * <P>
44 * The following fields are calculated by a graph layout and comprise the <EM>output</EM>:
45 * <UL>
46 * <LI>{@link #x} - the node's x location
47 * <LI>{@link #y} - the node's y location
48 * <LI>{@link #height} - the node's height may be stretched to match the height of other
49 * nodes
50 * </UL>
51 *
52 * @author Randy Hudson
53 * @since 2.1.2
54 */
55 public class Node {
56
57 Node left, right;
58
59 Object[] workingData;
60 int[] workingInts;
61
62 /**
63 * Clients may use this field to mark the Node with an arbitrary data object.
64 */
65 public Object data;
66
67 //used by various graph visitors
68 bool flag;
69
70 /**
71 * The height of this node. This value should be set prior to laying out the directed
72 * graph. Depending on the layout rules, a node's height may be expanded to match the
73 * height of other nodes around it.
74 */
75 public int height = 40;
76
77 /**
78 * @deprecated use {@link #setRowConstraint(int)} and {@link #getRowConstraint()}
79 */
80 public int rowOrder = -1;
81
82 /**
83 * The edges for which this node is the target.
84 */
85 public EdgeList incoming;
86
87 /**
88 * The default attachment point for incoming edges. <code>-1</code> indicates that the
89 * node's horizontal center should be used.
90 */
91 public int incomingOffset = -1;
92
93 // A non-decreasing number given to consecutive nodes in a Rank.
94 int index;
95
96 //Used in Compound graphs to quickly determine whether a node is inside a subgraph.
97 int nestingIndex = -1;
98
99 /**
100 * The edges for which this node is the source.
101 */
102 public EdgeList outgoing;
103
104 Insets padding;
105 private Subgraph parent;
106 int rank;
107
108 /**
109 * @deprecated for internal use only
110 */
111 public double sortValue;
112
113 /**
114 * The node's outgoing offset attachment point.
115 */
116 public int outgoingOffset = -1;
117
118 /**
119 * The node's width. The default value is 50.
120 */
121 public int width = 50;
122
123 /**
124 * The node's x coordinate.
125 */
126 public int x;
127 /**
128 * The node's y coordinate.
129 */
130 public int y;
131
132 /**
133 * Constructs a new node.
134 */
135 public this() { }
136
137 /**
138 * Constructs a node with the given data object
139 * @param data an arbitrary data object
140 */
141 public this(Object data) {
142 this(data, null);
143 }
144
145 /**
146 * Constructs a node inside the given subgraph.
147 * @param parent the parent subgraph
148 */
149 public this(Subgraph parent) {
150 this(null, parent);
151 }
152
153 /**
154 * Constructs a node with the given data object and parent subgraph. This node is added to
155 * the set of members for the parent subgraph
156 * @param data an arbitrary data object
157 * @param parent the parent subgraph or <code>null</code>
158 */
159 public this(Object data, Subgraph parent) {
160 this.data = data;
161 this.parent = parent;
162 incoming = new EdgeList();
163 outgoing = new EdgeList();
164 workingData = new Object[3];
165 workingInts = new int[4];
166 if (parent !is null)
167 parent.addMember(this);
168 }
169
170 /**
171 * Returns the incoming attachment point. This is the distance from the left edge to the
172 * default incoming attachment point for edges. Each incoming edge may have it's own
173 * attachment setting which takes priority over this default one.
174 * @return the incoming offset
175 */
176 public int getOffsetIncoming() {
177 if (incomingOffset is -1)
178 return width / 2;
179 return incomingOffset;
180 }
181
182 /**
183 * Returns the outgoing attachment point. This is the distance from the left edge to the
184 * default outgoing attachment point for edges. Each outgoing edge may have it's own
185 * attachment setting which takes priority over this default one.
186 * @return the outgoing offset
187 */
188 public int getOffsetOutgoing() {
189 if (outgoingOffset is -1)
190 return width / 2;
191 return outgoingOffset;
192 }
193
194 /**
195 * Returns the padding for this node or <code>null</code> if the default padding for the
196 * graph should be used.
197 * @return the padding or <code>null</code>
198 */
199 public Insets getPadding() {
200 return padding;
201 }
202
203 /**
204 * Returns the parent Subgraph or <code>null</code> if there is no parent. Subgraphs are
205 * only for use in {@link CompoundDirectedGraphLayout}.
206 * @return the parent or <code>null</code>
207 */
208 public Subgraph getParent() {
209 return parent;
210 }
211
212 /**
213 * For internal use only. Returns <code>true</code> if the given node is equal to this
214 * node. This method is implemented for consitency with Subgraph.
215 * @param node the node in question
216 * @return <code>true</code> if nested
217 */
218 bool isNested(Node node) {
219 return node is this;
220 }
221
222 /**
223 * Sets the padding. <code>null</code> indicates that the default padding should be used.
224 * @param padding an insets or <code>null</code>
225 */
226 public void setPadding(Insets padding) {
227 this.padding = padding;
228 }
229
230 /**
231 * Sets the parent subgraph. This method should not be called directly. The constructor
232 * will set the parent accordingly.
233 * @param parent the parent
234 */
235 public void setParent(Subgraph parent) {
236 this.parent = parent;
237 }
238
239 /**
240 * Sets the row sorting constraint for this node. By default, a node's constraint is
241 * <code>-1</code>. If two nodes have different values both >= 0, the node with the
242 * smaller constraint will be placed to the left of the other node. In all other cases no
243 * relative placement is guaranteed.
244 * @param value the row constraint
245 * @since 3.2
246 */
247 public void setRowConstraint(int value) {
248 this.rowOrder = value;
249 }
250
251 /**
252 * Returns the row constraint for this node.
253 * @return the row constraint
254 * @since 3.2
255 */
256 public int getRowConstraint() {
257 return rowOrder;
258 }
259
260 /**
261 * Sets the size of this node to the given dimension.
262 * @param size the new size
263 * @since 3.2
264 */
265 public void setSize(Dimension size) {
266 width = size.width;
267 height = size.height;
268 }
269
270 /**
271 * @see Object#toString()
272 */
273 public String toString() {
274 return Format("N({})", data ); //$NON-NLS-1$ //$NON-NLS-2$
275 }
276
277 Iterator iteratorNeighbors() {
278 return new class(outgoing) Iterator {
279 int offset;
280 EdgeList list;
281 this(EdgeList a){
282 list = a;
283 }
284 public Object next() {
285 Edge edge = list.getEdge(offset++);
286 if (offset < list.size())
287 return edge.opposite(this.outer);
288 if (list is outgoing) {
289 list = incoming;
290 offset = 0;
291 } else
292 list = null;
293 return edge.opposite(this.outer);
294 }
295
296 public bool hasNext() {
297 if (list is null)
298 return false;
299 if (offset < list.size())
300 return true;
301 if (list is outgoing) {
302 list = incoming;
303 offset = 0;
304 }
305 return offset < list.size();
306 }
307
308 public void remove() {
309 throw new RuntimeException("Remove not supported"); //$NON-NLS-1$
310 }
311 };
312 }
313
314 /**
315 * Returns a reference to a node located left from this one
316 * @return <code>Node</code> on the left from this one
317 * @since 3.4
318 */
319 public Node getLeft() {
320 return left;
321 }
322
323 /**
324 * Returns a reference to a node located right from this one
325 * @return <code>Node</code> on the right from this one
326 * @since 3.4
327 */
328 public Node getRight() {
329 return right;
330 }
331
332 }