-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSingleSelectionDemo.java
244 lines (216 loc) · 9.71 KB
/
SingleSelectionDemo.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
/****************************************************************************
**
** This demo file is part of yFiles for Java (Swing) 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 Java (Swing) 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 Java (Swing) version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for Java (Swing) powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for Java (Swing)
** 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.singleselection;
import com.yworks.yfiles.geometry.RectD;
import com.yworks.yfiles.graph.GraphItemTypes;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.IModelItem;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.view.input.GraphEditorInputMode;
import com.yworks.yfiles.view.input.ICommand;
import com.yworks.yfiles.view.input.IEventRecognizer;
import com.yworks.yfiles.view.input.KeyboardInputModeBinding;
import toolkit.AbstractDemo;
import toolkit.DemoStyles;
import javax.swing.AbstractAction;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Collection;
/**
* Configure {@link com.yworks.yfiles.view.input.GraphEditorInputMode} to enable single selection
* mode for interaction.
* <p>
* All default gestures that result in more than one item selected at a time are either switched
* off or changed so only one item gets selected. This requires some configuration that is done in
* {@link #enableSingleSelection(boolean)}. This method also restores the default selection behavior if
* single selection is disabled.
* </p>
*/
public class SingleSelectionDemo extends AbstractDemo {
// node locations
private final double[] nodeLocationsX = new double[]{317, 291, 220, 246, 221, 150, 142, 213, 232, 71, 0};
private final double[] nodeLocationsY = new double[]{87, 2, 0, 73, 144, 180, 251, 286, 215, 285, 320};
// the previously set multi-selection recognizer
private IEventRecognizer oldMultiSelectionRecognizer;
// the status of the single-selection
private boolean singleSelectionEnabled;
private GraphItemTypes oldPasteItems;
// custom command binding for 'toggle item selection'
private KeyboardInputModeBinding customToggleSelectionBinding;
/**
* Initializes this demo by configuring the graph defaults and the input mode, enabling single selection
* and loading the sample graph.
*/
public void initialize() {
DemoStyles.initDemoStyles(graphComponent.getGraph());
GraphEditorInputMode mode = new GraphEditorInputMode();
graphComponent.setInputMode(mode);
oldPasteItems = mode.getPasteSelectableItems();
// initially enable single selection
enableSingleSelection(true);
// create a sample graph
createSampleGraph();
}
/**
* Centers the graph in the graph component.
*/
public void onVisible() {
graphComponent.fitGraphBounds();
}
@Override
protected void configureToolBar(JToolBar toolBar) {
super.configureToolBar(toolBar);
// add the single selection toggle button to the tool bar
JToggleButton singleSelectionButton = new JToggleButton(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JToggleButton) {
enableSingleSelection(((JToggleButton) e.getSource()).isSelected());
}
}
});
singleSelectionButton.setText("Single Selection Mode");
// initially enable the button
singleSelectionButton.setSelected(true);
toolBar.add(singleSelectionButton);
}
private void createSampleGraph() {
IGraph graph = graphComponent.getGraph();
INode[] nodes = new INode[nodeLocationsX.length];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = graph.createNode(new RectD(nodeLocationsX[i], nodeLocationsY[i], 30, 30));
}
graph.createEdge(nodes[2], nodes[1]);
graph.createEdge(nodes[1], nodes[0]);
graph.createEdge(nodes[0], nodes[3]);
graph.createEdge(nodes[3], nodes[2]);
graph.createEdge(nodes[3], nodes[1]);
graph.createEdge(nodes[4], nodes[3]);
graph.createEdge(nodes[4], nodes[5]);
graph.createEdge(nodes[8], nodes[4]);
graph.createEdge(nodes[7], nodes[8]);
graph.createEdge(nodes[7], nodes[6]);
graph.createEdge(nodes[6], nodes[5]);
graph.createEdge(nodes[6], nodes[9]);
graph.createEdge(nodes[9], nodes[10]);
}
/**
* Enables or disables the single selection feature.
*/
private void enableSingleSelection(boolean enable) {
if (enable == this.singleSelectionEnabled) {
return;
}
this.singleSelectionEnabled = enable;
GraphEditorInputMode mode = (GraphEditorInputMode) graphComponent.getInputMode();
if (enable) {
// remember old recognizer so we can restore it later
oldMultiSelectionRecognizer = mode.getMultiSelectionRecognizer();
// disable marquee selection
mode.getMarqueeSelectionInputMode().setEnabled(false);
// disable multi selection with Ctrl-Click
mode.setMultiSelectionRecognizer(IEventRecognizer.NEVER);
// deactivate command that can lead to multi selection
mode.getAvailableCommands().remove(ICommand.SELECT_ALL);
// remove the default commands that are responsible to extend the selection via keyboard
Collection<ICommand> cmds = mode.getNavigationInputMode().getAvailableCommands();
cmds.remove(ICommand.EXTEND_SELECTION_LEFT);
cmds.remove(ICommand.EXTEND_SELECTION_RIGHT);
cmds.remove(ICommand.EXTEND_SELECTION_UP);
cmds.remove(ICommand.EXTEND_SELECTION_DOWN);
// add custom binding for toggle item selection
customToggleSelectionBinding = mode.getKeyboardInputMode().addCommandBinding(ICommand.TOGGLE_ITEM_SELECTION,
this::executedToggleItemSelection, this::canExecuteToggleItemSelection);
//Disable selection of (possibly multiple) items
oldPasteItems = mode.getPasteSelectableItems();
mode.setPasteSelectableItems(GraphItemTypes.NONE);
//Also clear the selection - even though the setup works when more than one item is selected, it looks a bit strange
graphComponent.getSelection().clear();
} else {
// restore old settings
mode.getMarqueeSelectionInputMode().setEnabled(true);
mode.setMultiSelectionRecognizer(oldMultiSelectionRecognizer);
mode.setPasteSelectableItems(oldPasteItems);
// re-activate command
mode.getAvailableCommands().add(ICommand.SELECT_ALL);
// re-insert the default commands to extend the selection
Collection<ICommand> cmds = mode.getNavigationInputMode().getAvailableCommands();
cmds.add(ICommand.EXTEND_SELECTION_LEFT);
cmds.add(ICommand.EXTEND_SELECTION_RIGHT);
cmds.add(ICommand.EXTEND_SELECTION_UP);
cmds.add(ICommand.EXTEND_SELECTION_DOWN);
// remove custom binding for toggle item selection
customToggleSelectionBinding.remove();
}
}
/**
* Checks whether an <code>IModelItem</code> can be determined whose selection state can be toggled.
*/
private boolean canExecuteToggleItemSelection(ICommand command, Object parameter, Object source) {
// if we have an item, the command can be executed
IModelItem modelItem = parameter instanceof IModelItem ? (IModelItem) parameter : graphComponent.getCurrentItem();
return modelItem != null;
}
/**
* Custom command handler that allows toggling the selection state of an item respecting the single selection policy.
*/
private boolean executedToggleItemSelection(ICommand command, Object parameter, Object source) {
// get the item
IModelItem modelItem = parameter instanceof IModelItem ? (IModelItem) parameter : graphComponent.getCurrentItem();
GraphEditorInputMode inputMode = (GraphEditorInputMode) graphComponent.getInputMode();
// check if it is allowed to be selected
if (modelItem != null &&
graphComponent.getGraph().contains(modelItem) &&
inputMode.getSelectableItems().is(modelItem)) {
boolean isSelected = inputMode.getGraphSelection().isSelected(modelItem);
if (isSelected) {
// the item is selected and needs to be unselected - just clear the selection
inputMode.getGraphSelection().clear();
} else {
// the item is not selected - deselect all other items and select the currentItem
inputMode.getGraphSelection().clear();
inputMode.setSelected(modelItem, true);
}
return true;
}
return false;
}
/**
* Builds the user interface and initializes the demo.
*/
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
initLnF();
new SingleSelectionDemo().start();
});
}
}