-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathZoomInvariantLabelStyleDemo.java
More file actions
376 lines (326 loc) · 12.6 KB
/
Copy pathZoomInvariantLabelStyleDemo.java
File metadata and controls
376 lines (326 loc) · 12.6 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/****************************************************************************
**
** This demo file is part of yFiles for JavaFX 3.6.
**
** Copyright (c) 2000-2026 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 style.zoominvariantlabelstyle;
import com.yworks.yfiles.graph.GraphItemTypes;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.ILabel;
import com.yworks.yfiles.graph.IModelItem;
import com.yworks.yfiles.graph.styles.DefaultLabelStyle;
import com.yworks.yfiles.graph.styles.ILabelStyle;
import com.yworks.yfiles.graphml.GraphMLIOHandler;
import com.yworks.yfiles.view.GraphControl;
import com.yworks.yfiles.view.IGraphSelection;
import com.yworks.yfiles.view.SelectionIndicatorManager;
import com.yworks.yfiles.view.input.GraphViewerInputMode;
import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.web.WebView;
import toolkit.DemoApplication;
import toolkit.DemoStyles;
import toolkit.WebViewUtils;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.function.Consumer;
/**
* Demonstrates zoom-invariant label rendering.
*/
public class ZoomInvariantLabelStyleDemo extends DemoApplication {
public GraphControl graphControl;
public WebView help;
public ComboBox modes;
public Label maxZoomLbl;
public Slider maxZoomSlider;
public Label maxZoomValue;
public Label minZoomLbl;
public Slider minZoomSlider;
public Label minZoomValue;
public Label zoomValue;
/**
* Configures user interaction and creates a sample diagram.
*/
public void initialize() {
initializeControls();
// prevent adding, removing, or editing elements but support selecting
// elements and panning the view
GraphViewerInputMode gvim = new GraphViewerInputMode();
gvim.setSelectableItems(GraphItemTypes.NODE.or(GraphItemTypes.EDGE).or(GraphItemTypes.LABEL));
graphControl.setInputMode(gvim);
// load a sample graph
loadGraph();
}
private void initializeControls() {
// setup the help text on the right side.
WebViewUtils.initHelp(help, this);
// display the current zoom factor of the demo's graph control
graphControl.zoomProperty().addListener(new SetLabelText(zoomValue, 2));
// initialize slider for changing the minimum zoom value for styles
// ZoomInvariantBelowThresholdLabelStyle and ZoomInvariantOutsideRangeLabelStyle
minZoomSlider.valueProperty().addListener(new SetLabelText(minZoomValue, 1));
minZoomSlider.valueProperty().addListener(new SliderValueHandler(this::onMinZoomChanged));
minZoomSlider.valueChangingProperty().addListener(new SliderValueChangingHandler(this::onMinZoomChanged));
Consumer<Boolean> minZoomControlsState = enabled -> {
boolean state = enabled == Boolean.FALSE;
minZoomLbl.setDisable(state);
minZoomSlider.setDisable(state);
minZoomValue.setDisable(state);
};
minZoomControlsState.accept(Boolean.TRUE);
// initialize slider for changing the maximum zoom value for styles
// ZoomInvariantAboveThresholdLabelStyle and ZoomInvariantOutsideRangeLabelStyle
maxZoomSlider.valueProperty().addListener(new SetLabelText(maxZoomValue, 1));
maxZoomSlider.valueProperty().addListener(new SliderValueHandler(this::onMaxZoomChanged));
maxZoomSlider.valueChangingProperty().addListener(new SliderValueChangingHandler(this::onMaxZoomChanged));
Consumer<Boolean> maxZoomControlsState = enabled -> {
boolean state = enabled == Boolean.FALSE;
maxZoomLbl.setDisable(state);
maxZoomSlider.setDisable(state);
maxZoomValue.setDisable(state);
};
maxZoomControlsState.accept(Boolean.FALSE);
// initialize drop-down list for choosing label styles with different
// rendering behavior
modes.getItems().addAll(Mode.values());
modes.setValue(Mode.FIXED_BELOW_THRESHOLD);
modes.valueProperty().addListener((property, oldValue, newValue) -> {
if (newValue == null) {
return;
}
Mode mode = (Mode) newValue;
setLabelStyles(mode, minZoomSlider.getValue(), maxZoomSlider.getValue());
switch (mode) {
case FIXED_ABOVE_THRESHOLD:
minZoomControlsState.accept(Boolean.FALSE);
maxZoomControlsState.accept(Boolean.TRUE);
break;
case FIXED_BELOW_THRESHOLD:
minZoomControlsState.accept(Boolean.TRUE);
maxZoomControlsState.accept(Boolean.FALSE);
break;
case INVARIANT_OUTSIDE_RANGE:
minZoomControlsState.accept(Boolean.TRUE);
maxZoomControlsState.accept(Boolean.TRUE);
break;
default:
minZoomControlsState.accept(Boolean.FALSE);
maxZoomControlsState.accept(Boolean.FALSE);
break;
}
});
}
/**
* Centers the sample diagram in the visible area.
*/
@Override
protected void onLoaded() {
graphControl.fitGraphBounds();
}
/**
* Set the label styles appropriate for the given label rendering policy.
*/
private void setLabelStyles( Mode mode, double minZoom, double maxZoom ) {
IGraph graph = graphControl.getGraph();
IGraphSelection selection = graphControl.getSelection();
SelectionIndicatorManager<IModelItem> manager = graphControl.getSelectionIndicatorManager();
for (ILabel label : graph.getLabels()) {
boolean updateSelectionHighlight = selection.isSelected(label);
if (updateSelectionHighlight) {
manager.removeSelection(label);
}
graph.setStyle(label, newLabelStyle(mode, minZoom, maxZoom));
if (updateSelectionHighlight) {
manager.addSelection(label);
}
}
}
/**
* Updates the maximum zoom value of the used label styles.
*/
private void onMaxZoomChanged( Number value ) {
if (value == null) {
return;
}
double newValue = value.doubleValue();
for (ILabel label : graphControl.getGraph().getLabels()) {
ILabelStyle style = label.getStyle();
if (style instanceof ZoomInvariantOutsideRangeLabelStyle) {
((ZoomInvariantOutsideRangeLabelStyle) style).setMaxZoom(newValue);
} else if (style instanceof ZoomInvariantAboveThresholdLabelStyle) {
((ZoomInvariantAboveThresholdLabelStyle) style).setMaxZoom(newValue);
}
}
graphControl.updateImmediately();
}
/**
* Updates the minimum zoom value of the used label styles.
*/
private void onMinZoomChanged( Number value ) {
if (value == null) {
return;
}
double newValue = value.doubleValue();
for (ILabel label : graphControl.getGraph().getLabels()) {
ILabelStyle style = label.getStyle();
if (style instanceof ZoomInvariantOutsideRangeLabelStyle) {
((ZoomInvariantOutsideRangeLabelStyle) style).setMinZoom(newValue);
} else if (style instanceof ZoomInvariantBelowThresholdLabelStyle) {
((ZoomInvariantBelowThresholdLabelStyle) style).setMinZoom(newValue);
}
}
graphControl.updateImmediately();
}
/**
* Loads a sample graph.
*/
private void loadGraph() {
DemoStyles.initDemoStyles(graphControl.getGraph());
GraphMLIOHandler graphMLIOHandler = graphControl.getGraphMLIOHandler();
graphMLIOHandler.addXamlNamespaceMapping(
"http://www.yworks.com/yfiles-for-javafx/demos/zoominvariantlabelstyle/1.0",
AbstractZoomInvariantLabelStyle.class);
try {
graphControl.importFromGraphML(getClass().getResource("resources/sample.graphml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main( String[] args) {
launch(args);
}
/**
* Create a new label style instance that demonstrates the given policy
* for zoom-invariant label rendering.
*/
private static ILabelStyle newLabelStyle( Mode mode, double minZoom, double maxZoom ) {
switch (mode) {
case DEFAULT:
return new DefaultLabelStyle();
case FIXED_ABOVE_THRESHOLD:
ZoomInvariantAboveThresholdLabelStyle invariantAboveStyle =
new ZoomInvariantAboveThresholdLabelStyle();
invariantAboveStyle.setMaxZoom(maxZoom);
return invariantAboveStyle;
case FIXED_BELOW_THRESHOLD:
ZoomInvariantBelowThresholdLabelStyle invariantBelowStyle =
new ZoomInvariantBelowThresholdLabelStyle();
invariantBelowStyle.setMinZoom(minZoom);
return invariantBelowStyle;
case INVARIANT_OUTSIDE_RANGE:
ZoomInvariantOutsideRangeLabelStyle invariantRangeStyle =
new ZoomInvariantOutsideRangeLabelStyle();
invariantRangeStyle.setMaxZoom(maxZoom);
invariantRangeStyle.setMinZoom(minZoom);
return invariantRangeStyle;
case FIT_OWNER:
return new FitOwnerLabelStyle();
default:
throw new IllegalArgumentException();
}
}
private static class SliderValueHandler implements ChangeListener<Number> {
private final Consumer<Number> onValueChanged;
SliderValueHandler( Consumer<Number> onValueChanged ) {
this.onValueChanged = onValueChanged;
}
@Override
public void changed(
ObservableValue<? extends Number> property, Number oldValue, Number newValue
) {
if (newValue == null) {
return;
}
Slider slider = (Slider) ((ReadOnlyProperty) property).getBean();
if (!slider.isValueChanging()) {
onValueChanged.accept(newValue);
}
}
}
private static class SliderValueChangingHandler implements ChangeListener<Boolean> {
private final Consumer<Number> onValueChanged;
SliderValueChangingHandler( Consumer<Number> onValueChanged ) {
this.onValueChanged = onValueChanged;
}
@Override
public void changed(
ObservableValue<? extends Boolean> property, Boolean oldValue, Boolean newValue
) {
if (Boolean.FALSE.equals(newValue)) {
Slider slider = (Slider) ((ReadOnlyProperty) property).getBean();
onValueChanged.accept(slider.getValue());
}
}
}
/**
* Displays decimal values as label text.
*/
private static class SetLabelText implements ChangeListener<Number> {
private final Label label;
private final NumberFormat format;
SetLabelText( Label label, int fractionDigits ) {
this.label = label;
this.format = newFormat(fractionDigits);
}
@Override
public void changed(
ObservableValue<? extends Number> observable, Number oldValue, Number newValue
) {
if (newValue != null) {
label.setText(format.format(newValue.doubleValue()));
}
}
private static NumberFormat newFormat( int fractionDigits ) {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(fractionDigits);
nf.setMinimumFractionDigits(fractionDigits);
return nf;
}
}
/**
* Enumerates the supported zoom-invariant label rendering policies.
*/
enum Mode {
DEFAULT("Default Label Style"),
FIXED_ABOVE_THRESHOLD("Fixed above maximum zoom"),
FIXED_BELOW_THRESHOLD("Fixed below minimum zoom"),
INVARIANT_OUTSIDE_RANGE("Fixed when outside specified range"),
FIT_OWNER("Fit into the label's owner");
private final String description;
Mode( String description ) {
this.description = description;
}
@Override
public String toString() {
return description;
}
}
}