forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxPartitionLayout.java
More file actions
executable file
·235 lines (198 loc) · 5.26 KB
/
Copy pathmxPartitionLayout.java
File metadata and controls
executable file
·235 lines (198 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.mxgraph.layout;
import java.util.ArrayList;
import java.util.List;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxICell;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
public class mxPartitionLayout extends mxGraphLayout
{
/**
* Boolean indicating the direction in which the space is partitioned.
* Default is true.
*/
protected boolean horizontal;
/**
* Integer that specifies the absolute spacing in pixels between the
* children. Default is 0.
*/
protected int spacing;
/**
* Integer that specifies the absolute inset in pixels for the parent that
* contains the children. Default is 0.
*/
protected int border;
/**
* Boolean that specifies if vertices should be resized. Default is true.
*/
protected boolean resizeVertices = true;
/**
* Constructs a new stack layout layout for the specified graph,
* spacing, orientation and offset.
*/
public mxPartitionLayout(mxGraph graph)
{
this(graph, true);
}
/**
* Constructs a new stack layout layout for the specified graph,
* spacing, orientation and offset.
*/
public mxPartitionLayout(mxGraph graph, boolean horizontal)
{
this(graph, horizontal, 0);
}
/**
* Constructs a new stack layout layout for the specified graph,
* spacing, orientation and offset.
*/
public mxPartitionLayout(mxGraph graph, boolean horizontal, int spacing)
{
this(graph, horizontal, spacing, 0);
}
/**
* Constructs a new stack layout layout for the specified graph,
* spacing, orientation and offset.
*/
public mxPartitionLayout(mxGraph graph, boolean horizontal, int spacing,
int border)
{
super(graph);
this.horizontal = horizontal;
this.spacing = spacing;
this.border = border;
}
/*
* (non-Javadoc)
* @see com.mxgraph.layout.mxGraphLayout#move(java.lang.Object, double, double)
*/
public void moveCell(Object cell, double x, double y)
{
mxIGraphModel model = graph.getModel();
Object parent = model.getParent(cell);
if (cell instanceof mxICell && parent instanceof mxICell)
{
int i = 0;
double last = 0;
int childCount = model.getChildCount(parent);
// Finds index of the closest swimlane
// TODO: Take into account the orientation
for (i = 0; i < childCount; i++)
{
Object child = model.getChildAt(parent, i);
mxRectangle bounds = getVertexBounds(child);
if (bounds != null)
{
double tmp = bounds.getX() + bounds.getWidth() / 2;
if (last < x && tmp > x)
{
break;
}
last = tmp;
}
}
// Changes child order in parent
int idx = ((mxICell) parent).getIndex((mxICell) cell);
idx = Math.max(0, i - ((i > idx) ? 1 : 0));
model.add(parent, cell, idx);
}
}
/**
* Hook for subclassers to return the container size.
*/
public mxRectangle getContainerSize()
{
return new mxRectangle();
}
/*
* (non-Javadoc)
* @see com.mxgraph.layout.mxIGraphLayout#execute(java.lang.Object)
*/
public void execute(Object parent)
{
mxIGraphModel model = graph.getModel();
mxGeometry pgeo = model.getGeometry(parent);
// Handles special case where the parent is either a layer with no
// geometry or the current root of the view in which case the size
// of the graph's container will be used.
if (pgeo == null && model.getParent(parent) == model.getRoot()
|| parent == graph.getView().getCurrentRoot())
{
mxRectangle tmp = getContainerSize();
pgeo = new mxGeometry(0, 0, tmp.getWidth(), tmp.getHeight());
}
if (pgeo != null)
{
int childCount = model.getChildCount(parent);
List<Object> children = new ArrayList<Object>(childCount);
for (int i = 0; i < childCount; i++)
{
Object child = model.getChildAt(parent, i);
if (!isVertexIgnored(child) && isVertexMovable(child))
{
children.add(child);
}
}
int n = children.size();
if (n > 0)
{
double x0 = border;
double y0 = border;
double other = (horizontal) ? pgeo.getHeight() : pgeo
.getWidth();
other -= 2 * border;
mxRectangle size = graph.getStartSize(parent);
other -= (horizontal) ? size.getHeight() : size.getWidth();
x0 = x0 + size.getWidth();
y0 = y0 + size.getHeight();
double tmp = border + (n - 1) * spacing;
double value = (horizontal) ? ((pgeo.getWidth() - x0 - tmp) / n)
: ((pgeo.getHeight() - y0 - tmp) / n);
// Avoids negative values, that is values where the sum of the
// spacing plus the border is larger then the available space
if (value > 0)
{
model.beginUpdate();
try
{
for (int i = 0; i < n; i++)
{
Object child = children.get(i);
mxGeometry geo = model.getGeometry(child);
if (geo != null)
{
geo = (mxGeometry) geo.clone();
geo.setX(x0);
geo.setY(y0);
if (horizontal)
{
if (resizeVertices)
{
geo.setWidth(value);
geo.setHeight(other);
}
x0 += value + spacing;
}
else
{
if (resizeVertices)
{
geo.setHeight(value);
geo.setWidth(other);
}
y0 += value + spacing;
}
model.setGeometry(child, geo);
}
}
}
finally
{
model.endUpdate();
}
}
}
}
}
}