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