-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMySimpleEdgeStyle.java
355 lines (313 loc) · 12.8 KB
/
MySimpleEdgeStyle.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
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
/****************************************************************************
**
** 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 style.simplecustomstyle;
import com.yworks.yfiles.geometry.GeneralPath;
import com.yworks.yfiles.geometry.Matrix2D;
import com.yworks.yfiles.geometry.PointD;
import com.yworks.yfiles.graph.IBend;
import com.yworks.yfiles.graph.IEdge;
import com.yworks.yfiles.graph.styles.AbstractEdgeStyle;
import com.yworks.yfiles.graph.styles.IArrow;
import com.yworks.yfiles.view.CanvasControl;
import com.yworks.yfiles.view.EdgeSelectionIndicatorInstaller;
import com.yworks.yfiles.view.IGraphSelection;
import com.yworks.yfiles.view.IRenderContext;
import com.yworks.yfiles.view.ISelectionIndicatorInstaller;
import com.yworks.yfiles.view.Pen;
import com.yworks.yfiles.view.VisualGroup;
import com.yworks.yfiles.view.input.IInputModeContext;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Path;
import javafx.util.Duration;
import java.util.Objects;
/**
* A simple implementation of an {@link com.yworks.yfiles.graph.styles.IEdgeStyle} that uses the convenience class {@link
* com.yworks.yfiles.graph.styles.AbstractEdgeStyle} as its base class.
*/
public class MySimpleEdgeStyle extends AbstractEdgeStyle {
// default gradient to paint the edge with
private static final Paint DEFAULT_PAINT = new LinearGradient(0, 0, 1, 1, true, CycleMethod.REPEAT,
new Stop(0, Color.rgb(200, 255, 255, 0.6)),
new Stop(0.5, Color.rgb(0, 130, 180, 0.8)),
new Stop(1, Color.rgb(150, 255, 255, 0.6)));
private double pathThickness;
private IArrow sourceArrow;
private IArrow targetArrow;
/**
* Returns the arrow at the beginning of the edge.
*/
public IArrow getSourceArrow() {
return sourceArrow;
}
/**
* Specifies the arrow at the beginning of the edge.
*/
public void setSourceArrow(IArrow arrow) {
this.sourceArrow = arrow;
}
/**
* Returns the arrow at the end of the edge.
*/
public IArrow getTargetArrow() {
return targetArrow;
}
/**
* Specifies the arrow at the end of the edge.
*/
public void setTargetArrow(IArrow arrow) {
this.targetArrow = arrow;
}
/**
* Initializes a new <code>MySimpleEdgeStyle</code>instance using {@link MySimpleArrow} at both ends of the edge and
* an edge thickness of 3.
*/
public MySimpleEdgeStyle() {
this(new MySimpleArrow(), 3);
}
/**
* Initializes a new <code>MySimpleEdgeStyle</code>instance using the given arrow at both ends of the edge and the
* given edge thickness.
*/
public MySimpleEdgeStyle(IArrow arrow, double pathThickness) {
this(arrow, arrow, pathThickness);
}
/**
* Initializes a new <code>MySimpleEdgeStyle</code>instance using the given arrows and the given edge thickness.
*/
public MySimpleEdgeStyle(IArrow sourceArrow, IArrow targetArrow, double pathThickness) {
setSourceArrow(sourceArrow);
setTargetArrow(targetArrow);
setPathThickness(pathThickness);
}
/**
* Gets the thickness of the edge. The default is 3.0.
*/
public double getPathThickness() {
return pathThickness;
}
/**
* Sets the thickness of the edge. The default is 3.0.
*/
public void setPathThickness(double pathThickness) {
this.pathThickness = pathThickness;
}
/**
* Creates the visual for an edge.
*/
@Override
protected Node createVisual(IRenderContext context, IEdge edge) {
// create a group that holds all visuals needed to paint the edge
VisualGroup group = new VisualGroup();
GeneralPath path = getPath(edge);
// create the visual that paints the edge path
EdgeVisual edgeVisual = new EdgeVisual();
edgeVisual.update(path, getPathThickness(), isSelected(context, edge));
group.add(edgeVisual);
// add visuals that paint the arrows
addArrows(context, group, edge, path, getSourceArrow(), getTargetArrow());
return group;
}
/**
* Re-renders the edge using the old visual instead of creating a new one for each call. It is strongly recommended to
* do it for performance reasons. Otherwise, {@link #createVisual(IRenderContext, IEdge)} is called instead.
*/
@Override
protected Node updateVisual(IRenderContext context, Node oldVisual, IEdge edge) {
if (!(oldVisual instanceof VisualGroup)) {
return createVisual(context, edge);
}
VisualGroup group = (VisualGroup) oldVisual;
GeneralPath path = getPath(edge);
// update the visual that paints the edge path
EdgeVisual edgeVisual = (EdgeVisual) group.getChildren().get(0);
edgeVisual.update(path, getPathThickness(), isSelected(context, edge));
// since the edge's ends might have changed their positions or orientations, we also have to update the visuals
// painting the arrows
updateArrows(context, group, edge, path, getSourceArrow(), getTargetArrow());
return group;
}
/**
* Creates a {@link com.yworks.yfiles.geometry.GeneralPath} from the segments of the given edge.
*/
@Override
protected GeneralPath getPath(IEdge edge) {
// create a general path from the source port over the bends to the target port of the edge
GeneralPath path = new GeneralPath();
path.moveTo(edge.getSourcePort().getLocation());
for (IBend bend : edge.getBends()) {
path.lineTo(bend.getLocation());
}
path.lineTo(edge.getTargetPort().getLocation());
// crop the edge's path at its nodes
// take the arrows into account when cropping the path
return cropPath(edge, getSourceArrow(), getTargetArrow(), path);
}
/**
* Determines if the visual representation of the edge has been hit at the given location.
* This implementation includes {@link #getPathThickness()} and the given
* context's <code>HitTestRadius</code> in the calculation.
*/
@Override
protected boolean isHit(IInputModeContext context, PointD location, IEdge edge) {
// use the convenience method in GeneralPath
return getPath(edge).pathContains(location, context.getHitTestRadius() + getPathThickness() * 0.5);
}
/**
* Returns whether the edge is selected or not.
*/
private boolean isSelected(IRenderContext context, IEdge edge) {
// we acquire the CanvasComponent instance from the render context and
// fetch an IGraphSelection instance using its lookup. We can be sure
// that those instances actually exist in this demo because we know that
// our canvas is a GraphControl.
// This is equivalent to casting the canvas to GraphControl and calling
// the method GraphControl#getSelection() from it.
IGraphSelection selection = context.getCanvasControl().lookup(IGraphSelection.class);
return selection.isSelected(edge);
}
/**
* Provides a custom implementation of the {@link com.yworks.yfiles.view.ISelectionIndicatorInstaller} interface
* that is better suited to this style.
*/
@Override
protected Object lookup(IEdge edge, Class type) {
if (type == ISelectionIndicatorInstaller.class) {
return new MySelectionInstaller();
} else {
return super.lookup(edge, type);
}
}
/**
* This customized {@link com.yworks.yfiles.view.ISelectionIndicatorInstaller} overrides the pen property to be
* transparent, so that no edge path is rendered if the edge is selected.
*/
private static class MySelectionInstaller extends EdgeSelectionIndicatorInstaller {
@Override
protected Pen getPen(CanvasControl canvas, IEdge edge) {
return Pen.getTransparent();
}
}
/**
* A {@link VisualGroup} that paints the line of an edge. We store the path and the thickness of
* the edge as instance variables.
* The {@link #update(GeneralPath, double, boolean)} method checks whether these values have been changed.
* If so, the instance variables are updated.
*/
private static class EdgeVisual extends VisualGroup {
// identity transformation
private static final Matrix2D IDENTITY = new Matrix2D();
// the shape to paint the line of the edge
private Path path;
// stores the segments of the edge; used to create and update the path
private GeneralPath generalPath;
// the thickness of the edge
private double pathThickness;
// whether or not the edge is currently selected; used to determine the color of the edge
private boolean selected;
// paint used for selected edges
private AnimatedPaint animatedPaint;
private ChangeListener<Paint> paintChangeListener;
EdgeVisual() {
this.path = new Path();
animatedPaint = new AnimatedPaint();
paintChangeListener = (observable, oldPaint, newPaint) -> updatePathPaint(newPaint, pathThickness);
this.add(path);
}
/**
* Checks if the path or the thickness of the edge has been changed. If so, updates all items needed to paint the
* edge.
* @param generalPath the path of the edge
* @param pathThickness the thickness of the edge
* @param selected whether or not the edge is selected
*/
void update(GeneralPath generalPath, double pathThickness, boolean selected) {
// update path
if (!Objects.equals(generalPath, this.generalPath)) {
generalPath.updatePath(path, IDENTITY);
this.generalPath = generalPath;
}
// update fill
if (pathThickness != this.pathThickness || selected != this.selected) {
if (!selected) {
// fill for non-selected state
animatedPaint.removeListener(paintChangeListener);
updatePathPaint(DEFAULT_PAINT, pathThickness);
} else if (!this.selected) {
// fill for selected state
animatedPaint.addListener(paintChangeListener);
}
this.pathThickness = pathThickness;
this.selected = selected;
}
}
private void updatePathPaint(Paint paint, double thickness) {
Pen pen = new Pen(paint, thickness);
pen.styleShape(path);
}
/**
* Binding used to animate the stroke of a selected edge.
*/
private static class AnimatedPaint extends ObjectBinding<Paint> {
private static final double SIZE = 30.0;
private static final int DURATION_MILLIS = 500;
private DoubleProperty start = new SimpleDoubleProperty(this, "start", 0);
AnimatedPaint() {
super.bind(start);
Timeline timeline = new Timeline();
timeline.setAutoReverse(false);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.getKeyFrames().add(new KeyFrame(Duration.ZERO, new KeyValue(start, 0.0)));
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(DURATION_MILLIS), new KeyValue(start, SIZE)));
timeline.play();
}
@Override
protected Paint computeValue() {
double startValue = this.start.get();
return new LinearGradient(startValue, startValue, startValue + SIZE, startValue + SIZE, false, CycleMethod.REPEAT,
new Stop(0, Color.rgb(255, 215, 0, 1.0)),
new Stop(0.5, Color.rgb(255, 245, 30, 1.0)),
new Stop(1, Color.rgb(255, 215, 0, 1.0)));
}
}
}
}