-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTreeLayoutDemo.java
309 lines (278 loc) · 11 KB
/
TreeLayoutDemo.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/****************************************************************************
**
** 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.treelayout;
import com.yworks.yfiles.graph.GraphItemTypes;
import com.yworks.yfiles.graph.IEdge;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.IModelItem;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.layout.MinimumNodeSizeStage;
import com.yworks.yfiles.layout.tree.AspectRatioNodePlacer;
import com.yworks.yfiles.layout.tree.AssistantNodePlacer;
import com.yworks.yfiles.layout.tree.ChildPlacement;
import com.yworks.yfiles.layout.tree.CompactNodePlacer;
import com.yworks.yfiles.layout.tree.DefaultNodePlacer;
import com.yworks.yfiles.layout.tree.FillStyle;
import com.yworks.yfiles.layout.tree.LeftRightNodePlacer;
import com.yworks.yfiles.layout.tree.RootAlignment;
import com.yworks.yfiles.layout.tree.RoutingStyle;
import com.yworks.yfiles.layout.tree.SimpleNodePlacer;
import com.yworks.yfiles.layout.tree.TreeLayout;
import com.yworks.yfiles.layout.tree.TreeLayoutData;
import com.yworks.yfiles.utils.IListEnumerable;
import com.yworks.yfiles.view.GraphControl;
import com.yworks.yfiles.view.ISelectionModel;
import com.yworks.yfiles.view.input.GraphEditorInputMode;
import toolkit.DemoApplication;
import toolkit.WebViewUtils;
import javafx.scene.web.WebView;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
/**
* Demonstrates the tree layout style and the different ways in which this
* algorithm can arrange a node and its children.
*/
public class TreeLayoutDemo extends DemoApplication {
public WebView helpView;
public GraphControl graphControl;
/**
* Specifies the node placement policy used for arranging the displayed graph.
* <dl>
* <dt>Mixed</dt>
* <dd>
* arranges child nodes either with {@link DefaultNodePlacer} or
* {@link LeftRightNodePlacer}. Which of the two placers is used for a
* given subtree depends on the tree depth of the subtree's root.
* </dd>
* <dt>The {@link DefaultNodePlacer}</dt>
* <dd>
* arranges child nodes horizontally aligned below their root node.
* It offers options to change the orientation of the subtree, the edge
* routing style, and the alignment of the root node.
* </dd>
* <dt>The {@link CompactNodePlacer}</dt>
* <dd>
* uses a dynamic optimization approach that chooses a placement strategy
* for children of a local root such that the overall result is compact
* with respect to the specified aspect ratio.
* </dd>
* <dt>The {@link AssistantNodePlacer}</dt>
* <dd>
* delegates to two different node placers to arrange the child nodes:
* Nodes that are marked as <em>Assistants</em> are placed using
* {@link LeftRightNodePlacer}.
* Other children are arranged below the assistant nodes using
* {@link SimpleNodePlacer}.
* </dd>
* <dt>The {@link AspectRatioNodePlacer}</dt>
* <dd>
* arranges child nodes such that a given aspect ratio is obeyed.
* </dd>
* </dl>
*/
private final static Policy NODE_PLACEMENT = Policy.Mixed;
/**
* Initializes the demo.
*/
public void initialize() {
WebViewUtils.initHelp(helpView, this);
// configure user interaction
initializeInputMode();
// load a sample graph
loadGraph();
// run the layout algorithm.
runLayout();
}
/**
* Configures user interaction.
*/
private void initializeInputMode() {
GraphEditorInputMode geim = new GraphEditorInputMode();
geim.setEditLabelAllowed(false);
geim.setClipboardOperationsAllowed(false);
geim.setUndoOperationsAllowed(false);
// do not allow node or edge creation
// only deleting nodes is allowed to ensure that the displayed graph is
// always a tree
geim.setCreateNodeAllowed(false);
geim.setCreateEdgeAllowed(false);
geim.setSelectableItems(GraphItemTypes.NODE);
geim.setDeletableItems(GraphItemTypes.NODE);
geim.setFocusableItems(GraphItemTypes.NODE);
// when deleting a node, always delete its whole subtree
geim.addDeletingSelectionListener(( sender, args ) -> {
ISelectionModel<IModelItem> selectedNodes = args.getSelection();
ArrayList<INode> nodesToDelete = new ArrayList<>();
// collect all selected nodes and their child nodes in the array
for (IModelItem item : selectedNodes) {
INode selectedNode = ((INode) item);
collectSubtreeNodes(selectedNode, nodesToDelete);
}
// select child nodes to be deleted
for (INode node : nodesToDelete) {
args.getSelection().setSelected(node, true);
}
});
// run the layout algorithm again if something got deleted
geim.addDeletedSelectionListener(( sender, args ) -> {
runLayout();
});
// assign the input mode to the graph control
graphControl.setInputMode(geim);
}
/**
* Runs {@link TreeLayout} with the chosen node placement policy for the
* displayed graph.
*/
private void runLayout() {
// create the layout algorithm ...
TreeLayout layout = new TreeLayout();
// ... and set a properly configured node placer
TreeLayoutData layoutData = new TreeLayoutData();
configure(layoutData, NODE_PLACEMENT);
graphControl.morphLayout(new MinimumNodeSizeStage(layout), Duration.ofMillis(500), layoutData);
}
/**
* Collects the node in the subtree with the given root node.
* @param root the root node of the subtree.
* @param children the list to collect the subtree nodes.
*/
private void collectSubtreeNodes( INode root, List<INode> children ) {
children.add(root);
for (IEdge edge : graphControl.getGraph().outEdgesAt(root)) {
collectSubtreeNodes(edge.getTargetNode(), children);
}
}
/**
* Loads a sample tree graph.
*/
private void loadGraph() {
try {
graphControl.importFromGraphML(getClass().getResource("resources/sample.graphml"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Configures the node placer of the given layout data.
*/
private void configure( TreeLayoutData layoutData, Policy policy ) {
switch (policy) {
case AspectRatio:
AspectRatioNodePlacer aspectRatioNodePlacer = new AspectRatioNodePlacer();
aspectRatioNodePlacer.setAspectRatio(1);
aspectRatioNodePlacer.setFillStyle(FillStyle.LEADING);
aspectRatioNodePlacer.setHorizontalDistance(40);
aspectRatioNodePlacer.setVerticalDistance(40);
aspectRatioNodePlacer.setHorizontal(true);
layoutData.setNodePlacers(aspectRatioNodePlacer);
break;
case Assistant:
// specifying assistant nodes has only an effect for CompactNodePlacer
// and AssistantNodePlacer
layoutData.setAssistantNodes(node -> Boolean.TRUE.equals(node.getTag()));
SimpleNodePlacer childNodePlacer = new SimpleNodePlacer();
childNodePlacer.setRootAlignment(SimpleNodePlacer.RootAlignment.CENTER);
AssistantNodePlacer assistantNodePlacer = new AssistantNodePlacer();
assistantNodePlacer.setChildNodePlacer(childNodePlacer);
assistantNodePlacer.setSpacing(20);
layoutData.setNodePlacers(assistantNodePlacer);
break;
case Compact:
// specifying assistant nodes has only an effect for CompactNodePlacer
// and AssistantNodePlacer
layoutData.setAssistantNodes(node -> Boolean.TRUE.equals(node.getTag()));
CompactNodePlacer compactNodePlacer = new CompactNodePlacer();
compactNodePlacer.setPreferredAspectRatio(1);
compactNodePlacer.setVerticalDistance(40);
compactNodePlacer.setHorizontalDistance(40);
compactNodePlacer.setMinimumFirstSegmentLength(10);
compactNodePlacer.setMinimumLastSegmentLength(10);
layoutData.setNodePlacers(compactNodePlacer);
break;
case Mixed:
layoutData.setNodePlacers(node -> {
int depth = getDepth(graphControl.getGraph(), node);
if (depth == 3) {
return new LeftRightNodePlacer();
} else {
return new DefaultNodePlacer();
}
});
break;
default:
// if none of the above is selected, the DefaultNodePlacer is used
DefaultNodePlacer defaultNodePlacer = new DefaultNodePlacer();
defaultNodePlacer.setChildPlacement(ChildPlacement.HORIZONTAL_DOWNWARD);
defaultNodePlacer.setRoutingStyle(RoutingStyle.FORK);
defaultNodePlacer.setHorizontalDistance(40);
defaultNodePlacer.setVerticalDistance(40);
defaultNodePlacer.setRootAlignment(RootAlignment.CENTER);
defaultNodePlacer.setMinimumChannelSegmentDistance(0);
layoutData.setNodePlacers(defaultNodePlacer);
break;
}
}
private int getDepth(IGraph graph, INode node) {
int depth = 0;
INode walker = node;
while (walker != null) {
IListEnumerable<IEdge> inEdges = graph.inEdgesAt(walker);
int size = inEdges.size();
if (size == 0) {
return depth;
} else if (size > 1) {
throw new IllegalStateException("Graph is not a tree.");
} else {
walker = inEdges.first().getSourceNode();
depth++;
}
if (depth > graph.getNodes().size()) {
throw new IllegalStateException("Graph is not a tree.");
}
}
return depth;
}
public static void main(String[] args) {
launch(args);
}
/**
* Lists the node placement policies supported in this demo.
*/
private enum Policy {
AspectRatio,
Assistant,
Compact,
Default,
Mixed
}
}