-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLayerVisualCreator.java
282 lines (259 loc) · 10.5 KB
/
LayerVisualCreator.java
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/****************************************************************************
**
** This demo file is part of yFiles for JavaFX 3.6.
**
** Copyright (c) 2000-2023 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for JavaFX functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for JavaFX version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for JavaFX powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for JavaFX
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
package layout.hierarchiclayout;
import com.yworks.yfiles.geometry.PointD;
import com.yworks.yfiles.geometry.RectD;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.IMapper;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.view.IRenderContext;
import com.yworks.yfiles.view.IVisualCreator;
import com.yworks.yfiles.view.VisualGroup;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* Visualizes the layers that have been calculated by the {@link com.yworks.yfiles.layout.hierarchic.HierarchicLayout}.
* Each layer is visualized by a rectangle.
*/
class LayerVisualCreator implements IVisualCreator {
private static final int LAYER_INSETS = 10;
// the dark brush used for drawing the layers
private static final Paint DARK_PAINT = Color.rgb(0xFF, 0xC9, 0x14, 0.5);
// the light brush used for drawing the layers
private static final Paint LIGHT_PAINT = Color.rgb(0xFF, 0xE8, 0xA0, 0.5);
// the bounds of the complete drawing
private RectD bounds;
// the list of the dividers (one less than the number of layers)
// a divider is the y coordinate between two adjacent layers
private List<Float> dividers = new ArrayList<>();
/**
* Updates the bounds of the layers based on the bounds of their containing nodes.
*/
public void updateLayerBounds(IGraph graph, IMapper<INode, Integer> layerMapper) {
// count the layers
Collection<INode> nodes;
// collect nodes except for empty group nodes
nodes = graph.getNodes().stream().filter(node -> graph.getChildren(node).size() == 0).collect(Collectors.toList());
int layerCount = 0;
for (INode node : nodes) {
layerCount = Math.max(layerCount, layerMapper.getValue(node));
}
layerCount++;
// calculate min and max values
int[] mins = new int[layerCount];
int[] maxs = new int[layerCount];
for (int i = 0; i < maxs.length; i++) {
maxs[i] = Integer.MIN_VALUE;
}
for (int i = 0; i < maxs.length; i++) {
mins[i] = Integer.MAX_VALUE;
}
double minX = Double.POSITIVE_INFINITY;
double maxX = Double.NEGATIVE_INFINITY;
for (INode node : nodes) {
double minY = node.getLayout().getY();
double maxY = minY + node.getLayout().getHeight();
mins[layerMapper.getValue(node)] = Math.min(mins[layerMapper.getValue(node)], (int) minY);
maxs[layerMapper.getValue(node)] = Math.max(maxs[layerMapper.getValue(node)], (int) maxY);
minX = Math.min(minX, node.getLayout().getX());
maxX = Math.max(maxX, node.getLayout().getX() + node.getLayout().getWidth());
}
// now determine divider locations
dividers.clear();
for (int i = 0; i < maxs.length - 1; i++) {
dividers.add((maxs[i] + mins[i + 1]) * 0.5f);
}
// determine the bounds of all elements
int margin = 10;
mins[0] -= margin;
minX -= margin;
maxX += margin;
maxs[maxs.length - 1] += margin;
if (nodes.iterator().hasNext()) {
mins[0] -= margin;
minX -= margin;
maxX += margin;
maxs[maxs.length - 1] += margin;
bounds = new RectD((float)minX, mins[0], (float)(maxX - minX), maxs[maxs.length - 1] - mins[0]);
} else {
bounds = new RectD();
}
}
/**
* Gets the layer to insert a node at the given location.
* @param location the location
* @return a positive value if a specific layer is hit, a negative one to indicate that a new layer should be inserted
* before layer -(value + 1) - int.MaxValue if no layer has been hit.
*/
public int getLayer(PointD location) {
// global bounds
RectD nbounds = new RectD(
bounds.getX(),
bounds.getY() - LAYER_INSETS,
bounds.getWidth(),
bounds.getHeight() + LAYER_INSETS * 2);
if (location.getY() < bounds.getMinY()) {
// before the first layer
return -1;
}
if (location.getY() > bounds.getMaxY()) {
// after the last layer
return -((dividers.size() + 2) + 1);
}
// nothing found,
if (!nbounds.contains(location)) {
return Integer.MAX_VALUE;
}
// now search the layer
double top = bounds.getMinY();
int layerCount = 0;
for (float divider : dividers) {
RectD layerBounds = new RectD(bounds.getX(), top, bounds.getWidth(), divider - top);
if (layerBounds.contains(location)) {
return getLayerIndex(location, layerBounds, layerCount);
}
layerCount++;
top = divider;
}
{
RectD layerBounds = new RectD(bounds.getX(), top, bounds.getWidth(), bounds.getMaxY() - top);
if (layerBounds.contains(location)) {
return getLayerIndex(location, layerBounds, layerCount);
}
}
// should not really happen...
return Integer.MAX_VALUE;
}
/**
* Checks if the specified layer has been clicked near the border. If the layer has been clicked near the top border
* return <code>-(layerIndex + 1)</code>, i.e a new layer should be inserted above the given one. If the layer has
* been clicked near the bottom border return <code>-(layerIndex + 2)</code>, i.e. a new layer should be inserted
* below the given one. Otherwise return the specified layer index.
*/
private int getLayerIndex(PointD location, RectD layerBounds, int layerIndex) {
// check if close to top or bottom
if (location.getY() - layerBounds.getMinY() < LAYER_INSETS) {
// before current layer
return -(layerIndex + 1);
}
if (layerBounds.getMaxY() - location.getY() < LAYER_INSETS) {
// after current layer
return -(layerIndex + 2);
}
// in current layer
return layerIndex;
}
/**
* Gets the bounds of a layer by index as specified by {@link #getLayer(com.yworks.yfiles.geometry.PointD)}.
*/
RectD getLayerBounds(int layerIndex) {
if (layerIndex == Integer.MAX_VALUE) {
return RectD.INFINITE;
}
if (layerIndex < 0) {
// new layer
int beforeLayer = -(layerIndex + 1);
if (beforeLayer <= dividers.size()) {
RectD layerBounds = getLayerBounds(beforeLayer);
return new RectD(layerBounds.getX(), layerBounds.getMinY() - LAYER_INSETS, layerBounds.getWidth(), LAYER_INSETS * 2);
} else {
// after last layer
RectD layerBounds = getLayerBounds(dividers.size());
return new RectD(layerBounds.getX(), layerBounds.getMaxY() - LAYER_INSETS, layerBounds.getWidth(), LAYER_INSETS * 2);
}
} else {
double top = layerIndex > 0 ? dividers.get(layerIndex - 1) : bounds.getMinY();
double bottom = layerIndex < dividers.size() ? dividers.get(layerIndex) : bounds.getMaxY();
return new RectD(bounds.getX(), top, bounds.getWidth(), bottom - top);
}
}
@Override
public Node createVisual(IRenderContext renderContext) {
// create a new VisualGroup and update it with the current state
VisualGroup container = new VisualGroup();
updateLayerVisuals(container);
return container;
}
@Override
public Node updateVisual(IRenderContext renderContext, Node oldVisual) {
if (oldVisual instanceof VisualGroup) {
VisualGroup visual = (VisualGroup) oldVisual;
updateLayerVisuals(visual);
return visual;
} else {
return createVisual(renderContext);
}
}
/**
* Iterates over the dividers of the layers from top to bottom and updates or creates a rectangle for each layer for
* its visualization.
* @param container contains the visuals for the layers
*/
private void updateLayerVisuals(VisualGroup container) {
// create a rectangle for each layer for its visualization
double y = bounds.getMinY();
int count = 0;
for (float divider : dividers) {
createOrUpdateLayerVisual(container, count, y, divider);
count++;
y = divider;
}
// we have one less dividers than the number of layers,
// so we have to update or create here one more rectangle
createOrUpdateLayerVisual(container, count, y, bounds.getMaxY());
count++;
// the number of layers has decreased - remove all remaining rectangles
while (container.getChildren().size() > count) {
container.getChildren().remove(container.getChildren().size() - 1);
}
}
private void createOrUpdateLayerVisual(VisualGroup container, int index, double y, double maxY) {
Rectangle rectangle;
if (container.getChildren().size() <= index) {
// the number of layers has increased - we need to create additional rectangles
container.getChildren().add(rectangle = new Rectangle());
} else {
// update an existing rectangle
rectangle = (Rectangle) container.getChildren().get(index);
}
// set the bounds of the rectangle
rectangle.setX(bounds.getMinX());
rectangle.setY(y);
rectangle.setWidth(bounds.getWidth());
rectangle.setHeight(maxY - y);
rectangle.setFill(index %2 == 1 ? LIGHT_PAINT : DARK_PAINT);
}
}