-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReparentHandlerDemo.java
213 lines (187 loc) · 9.14 KB
/
ReparentHandlerDemo.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
/****************************************************************************
**
** 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 input.reparenthandler;
import com.yworks.yfiles.geometry.RectD;
import com.yworks.yfiles.graph.GraphItemTypes;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.graph.styles.DefaultLabelStyle;
import com.yworks.yfiles.graph.styles.GroupNodeStyle;
import com.yworks.yfiles.graph.styles.ILabelStyle;
import com.yworks.yfiles.graph.styles.RectangleNodeStyle;
import com.yworks.yfiles.utils.IEnumerable;
import com.yworks.yfiles.view.GraphControl;
import com.yworks.yfiles.view.input.GraphEditorInputMode;
import com.yworks.yfiles.view.input.IInputModeContext;
import com.yworks.yfiles.view.input.ReparentNodeHandler;
import javafx.scene.web.WebView;
import toolkit.DemoApplication;
import toolkit.DemoStyles;
import toolkit.Palette;
import toolkit.Themes;
import toolkit.WebViewUtils;
import java.util.Arrays;
/**
* Demo code that shows how to customize the reparent gesture in a {@link com.yworks.yfiles.graph.IGraph} by
* implementing a custom {@link com.yworks.yfiles.view.input.IReparentNodeHandler}.
*/
public class ReparentHandlerDemo extends DemoApplication {
public GraphControl graphControl;
public WebView help;
/**
* Initializes the controller. This is called when the FXMLLoader instantiates the scene graph.
* At the time this method is called, all nodes in the scene graph are available. Most importantly,
* the GraphControl instance is initialized.
*/
public void initialize() {
// setup the help text on the right side.
WebViewUtils.initHelp(help, this);
IGraph graph = graphControl.getGraph();
// Enable the undo feature
graph.setUndoEngineEnabled(true);
// initialize the input mode
initializeInputMode();
// create a sample graph using different colored nodes to show different reparenting behaviors
createSampleGraph(graph);
}
private void initializeInputMode() {
GraphEditorInputMode mode = new GraphEditorInputMode();
// disable element creation and deletion
mode.setCreateEdgeAllowed(false);
mode.setCreateNodeAllowed(false);
mode.setDeletableItems(GraphItemTypes.NONE);
mode.setGroupSelectionAllowed(false);
// enable grouping operations such as grouping selected nodes moving nodes
// into group nodes
mode.setGroupingOperationsAllowed(true);
// assign the custom reparent handler
mode.setReparentNodeHandler(new DemoReparentNodeHandler());
graphControl.setInputMode(mode);
}
/**
* Creates the sample graph with colored nodes and groups. The color indicates which node can be reparented to which
* group node.
*/
private void createSampleGraph(IGraph graph) {
DemoStyles.initDemoStyles(graph);
// Create some group nodes
INode group1 = createGroupNode(graph, 100, 100, Themes.PALETTE_LIGHTBLUE, "Only Blue Children");
INode group2 = createGroupNode(graph, 160, 130, Themes.PALETTE_LIGHTBLUE, "Only Blue Children");
INode greenGroup = createGroupNode(graph, 100, 350, Themes.PALETTE_GREEN, "Only Green Children");
createGroupNode(graph, 400, 350, Themes.PALETTE_GREEN, "Only Green Children");
// And some regular nodes
RectangleNodeStyle blueStyle = DemoStyles.createDemoNodeStyle(Themes.PALETTE_LIGHTBLUE);
INode blueNode = graph.createNode(new RectD(110, 130, 30, 30), blueStyle, Themes.PALETTE_LIGHTBLUE);
RectangleNodeStyle greenStyle = DemoStyles.createDemoNodeStyle(Themes.PALETTE_GREEN);
INode greenNode = graph.createNode(new RectD(130, 380, 30, 30), greenStyle, Themes.PALETTE_GREEN);
RectangleNodeStyle redStyle = DemoStyles.createDemoNodeStyle(Themes.PALETTE_RED);
graph.createNode(new RectD(400, 100, 30, 30), redStyle, Themes.PALETTE_RED);
graph.createNode(new RectD(500, 100, 30, 30), greenStyle, Themes.PALETTE_GREEN);
graph.createNode(new RectD(400, 200, 30, 30), blueStyle, Themes.PALETTE_LIGHTBLUE);
graph.createNode(new RectD(500, 200, 30, 30), redStyle, Themes.PALETTE_RED);
// Add some initial children to the groups
graph.groupNodes(group1, Arrays.asList(blueNode, group2));
graph.groupNodes(greenGroup, IEnumerable.create(greenNode));
// Ensure that the outer blue group completely contains its inner group
graph.setNodeLayout(group1, new RectD(100, 100, 200, 150));
// Uncomment the following line to adjust the bounds of the outer blue group automatically
// graph.adjustGroupNodeLayout(group1);
}
/**
* Creates a group node for the sample graph with a specific styling.
*/
private static INode createGroupNode(IGraph graph, double x, double y, Palette palette, String labelText) {
GroupNodeStyle groupNodeStyle = DemoStyles.createDemoGroupStyle(palette);
INode groupNode = graph.createGroupNode(null, new RectD(x, y, 130, 100), groupNodeStyle, palette);
// The label style and placement
ILabelStyle labelStyle = DemoStyles.createDemoGroupLabelStyle(palette);
graph.addLabel(groupNode, labelText, null, labelStyle);
return groupNode;
}
public static void main(String[] args) {
launch(args);
}
/**
* Customized variant of the default {@link ReparentNodeHandler} that
* determines the possible reparenting operations based on the node's tag.
*/
private static class DemoReparentNodeHandler extends ReparentNodeHandler {
/**
* In general, this method determines whether the current gesture that
* can be determined through the context is a reparent gesture. In this
* case, it returns true if the base implementation returns true or if the
* current node is green.
* @param context the context that provides information about the user input
* @param node the node that will possibly be reparented
* @return whether this is a reparenting gesture
*/
@Override
public boolean isReparentGesture(IInputModeContext context, INode node) {
return super.isReparentGesture(context, node) || Themes.PALETTE_GREEN.equals(node.getTag());
}
/**
* In general, this method determines whether the user may detach the
* given node from its current parent in order to reparent it. In this case,
* it returns false for red nodes.
* @param context the context that provides information about the user input
* @param node the node that is about to be detached from its current parent
* @return whether the node may be detached and reparented
*/
@Override
public boolean shouldReparent(IInputModeContext context, INode node) {
return !Themes.PALETTE_RED.equals(node.getTag());
}
/**
* In general, this method determines whether the provided node
* may be reparented to the given <code>newParent</code>.
* @param context the context that provides information about the user input
* @param node the node that will be reparented
* @param newParent the potential new parent
* @return whether <code>newParent</code> is a valid new parent for <code>node</code>
*/
@Override
public boolean isValidParent(IInputModeContext context, INode node, INode newParent) {
// Obtain the tag from the designated child
Object nodeTag = node.getTag();
// and from the designated parent.
Object parentTag = newParent == null ? null : newParent.getTag();
if (nodeTag == null) {
// Newly created nodes or nodes without a tag in general can be reparented freely
return true;
}
// Otherwise, allow nodes to be moved only if their tags are the same color
if (nodeTag instanceof Palette && parentTag instanceof Palette) {
return nodeTag.equals(parentTag);
}
// Finally, if there is no new parent, this is ok, too
return newParent == null;
}
}
}