comparison dwtx/draw2d/graph/DirectedGraph.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, 2005 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.DirectedGraph;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.PositionConstants;
18 import dwtx.draw2d.geometry.Dimension;
19 import dwtx.draw2d.geometry.Insets;
20 import dwtx.draw2d.graph.EdgeList;
21 import dwtx.draw2d.graph.NodeList;
22 import dwtx.draw2d.graph.Node;
23 import dwtx.draw2d.graph.RankList;
24 import dwtx.draw2d.graph.Edge;
25 import dwtx.draw2d.graph.Rank;
26
27 /**
28 * A graph consisting of nodes and directed edges. A DirectedGraph serves as the input to
29 * a graph layout algorithm. The algorithm will place the graph's nodes and edges
30 * according to certain goals, such as short, non-crossing edges, and readability.
31 *
32 * @author hudsonr
33 * @since 2.1.2
34 */
35 public class DirectedGraph {
36
37 private int direction = PositionConstants.SOUTH;
38
39 /**
40 * The default padding to be used for nodes which don't specify any padding. Padding is
41 * the amount of empty space to be left around a node. The default value is undefined.
42 */
43 private Insets defaultPadding;
44
45 /**
46 * All of the edges in the graph.
47 */
48 public EdgeList edges;
49
50 /**
51 * All of the nodes in the graph.
52 */
53 public NodeList nodes;
54
55 /**
56 * For internal use only. The list of rows which makeup the final graph layout.
57 * @deprecated
58 */
59 public RankList ranks;
60
61 Node forestRoot;
62 Insets margin;
63 int[] rankLocations;
64 int[][] cellLocations;
65 int tensorStrength;
66 int tensorSize;
67 Dimension size;
68
69 public this(){
70 defaultPadding = new Insets(16);
71 edges = new EdgeList();
72 nodes = new NodeList();
73 ranks = new RankList();
74 margin = new Insets();
75 size = new Dimension();
76 }
77
78 /**
79 * Returns the default padding for nodes.
80 * @return the default padding
81 * @since 3.2
82 */
83 public Insets getDefaultPadding() {
84 return defaultPadding;
85 }
86
87 /**
88 * Returns the direction in which the graph will be layed out.
89 * @return the layout direction
90 * @since 3.2
91 */
92 public int getDirection() {
93 return direction;
94 }
95
96 /**
97 * Sets the outer margin for the entire graph. The margin is the space in which nodes
98 * should not be placed.
99 * @return the graph's margin
100 * @since 3.2
101 */
102 public Insets getMargin() {
103 return margin;
104 }
105
106 /**
107 * Returns the effective padding for the given node. If the node has a specified padding,
108 * it will be used, otherwise, the graph's defaultPadding is returned. The
109 * returned value must not be modified.
110 * @param node the node
111 * @return the effective padding for that node
112 */
113 public Insets getPadding(Node node) {
114 Insets pad = node.getPadding();
115 if (pad is null)
116 return defaultPadding;
117 return pad;
118 }
119
120 int[] getCellLocations(int rank) {
121 return cellLocations[rank];
122 }
123
124 int[] getRankLocations() {
125 return rankLocations;
126 }
127 //
128 //public Cell getCell(Point pt) {
129 // int rank = 0;
130 // while (rank < rankLocations.length - 1 && rankLocations[rank] < pt.y)
131 // rank++;
132 // int cells[] = cellLocations[rank];
133 // int cell = 0;
134 // while (cell < cells.length - 1 && cells[cell] < pt.x)
135 // cell++;
136 // return new Cell(rank, cell, ranks.getRank(rank).getNode(index));
137 //}
138
139 public Node getNode(int rank, int index) {
140 if (ranks.size() <= rank)
141 return null;
142 Rank r = ranks.getRank(rank);
143 if (r.size() <= index)
144 return null;
145 return r.getNode(index);
146 }
147
148 /**
149 * Removes the given edge from the graph.
150 * @param edge the edge to be removed
151 */
152 public void removeEdge(Edge edge) {
153 edges.remove(edge);
154 edge.source.outgoing.remove(edge);
155 edge.target.incoming.remove(edge);
156 if (edge.vNodes !is null)
157 for (int j = 0; j < edge.vNodes.size(); j++)
158 removeNode(edge.vNodes.getNode(j));
159 }
160
161 /**
162 * Removes the given node from the graph. Does not remove the node's edges.
163 * @param node the node to remove
164 */
165 public void removeNode(Node node) {
166 nodes.remove(node);
167 if (ranks !is null)
168 ranks.getRank(node.rank).remove(node);
169 }
170
171 /**
172 * Sets the default padding for all nodes in the graph. Padding is the empty space left
173 * around the <em>outside</em> of each node. The default padding is used for all nodes
174 * which do not specify a specific amount of padding (i.e., their padding is
175 * <code>null</code>).
176 * @param insets the padding
177 */
178 public void setDefaultPadding(Insets insets) {
179 defaultPadding = insets;
180 }
181
182 /**
183 * Sets the layout direction for the graph. Edges will be layed out in the specified
184 * direction (unless the graph contains cycles). Supported values are:
185 * <UL>
186 * <LI>{@link dwtx.draw2d.PositionConstants#EAST}
187 * <LI>{@link dwtx.draw2d.PositionConstants#SOUTH}
188 * </UL>
189 * <P>The default direction is south.
190 * @param direction the layout direction
191 * @since 3.2
192 */
193 public void setDirection(int direction) {
194 this.direction = direction;
195 }
196
197 //public void setGraphTensor(int length, int strength) {
198 // tensorStrength = strength;
199 // tensorSize = length;
200 //}
201
202 /**
203 * Sets the graphs margin.
204 * @param insets the graph's margin
205 * @since 3.2
206 */
207 public void setMargin(Insets insets) {
208 this.margin = insets;
209 }
210
211 public Dimension getLayoutSize() {
212 return size;
213 }
214
215 }