-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCssEdgeStyle.java
235 lines (205 loc) · 7.72 KB
/
CssEdgeStyle.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
/****************************************************************************
**
** 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 viewer.css;
import com.yworks.yfiles.geometry.GeneralPath;
import com.yworks.yfiles.geometry.Matrix2D;
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.Arrow;
import com.yworks.yfiles.graph.styles.ArrowType;
import com.yworks.yfiles.view.IRenderContext;
import com.yworks.yfiles.view.Pen;
import com.yworks.yfiles.view.VisualGroup;
import javafx.scene.Node;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Path;
import java.util.Objects;
/**
* An edge style that can be styled with CSS.
* <p>
* You can change the visualization of the edge path by defining properties for the style class {@code edge-style}.
* As the edge path is essentially a JavaFX
* <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#path">Path</a>,
* the {@code edge-style} style class supports path properties.
* </p>
* <p>
* Changing the stroke or stroke width of the edge path will also be applied to the arrows of the edge.
* </p>
*/
public class CssEdgeStyle extends AbstractEdgeStyle {
// identity transformation
private static final Matrix2D IDENTITY = new Matrix2D();
private String[] styleClasses;
private Arrow sourceArrow;
private Arrow targetArrow;
/**
* Initializes a new {@code CssEdgeStyle} instance using no arrow at both ends of the edge.
*/
public CssEdgeStyle() {
}
/**
* Initializes a new {@code CssEdgeStyle} instance using the given style classes.
*/
public CssEdgeStyle(String... styleClasses) {
this(new Arrow(ArrowType.NONE), new Arrow(ArrowType.NONE), styleClasses);
}
/**
* Initializes a new {@code CssEdgeStyle} instance using the given arrows and the given style classes.
*/
public CssEdgeStyle(Arrow sourceArrow, Arrow targetArrow, String... styleClasses) {
setSourceArrow(sourceArrow);
setTargetArrow(targetArrow);
this.styleClasses = styleClasses;
}
/**
* Returns the style classes.
*/
public String[] getStyleClasses() {
return styleClasses;
}
/**
* Sets the style classes.
*/
public void setStyleClasses(String[] styleClasses) {
this.styleClasses = styleClasses;
}
/**
* Returns the arrow at the beginning of the edge.
*/
public Arrow getSourceArrow() {
return sourceArrow;
}
/**
* Specifies the arrow at the beginning of the edge.
*/
public void setSourceArrow(Arrow arrow) {
this.sourceArrow = arrow;
}
/**
* Returns the arrow at the end of the edge.
*/
public Arrow getTargetArrow() {
return targetArrow;
}
/**
* Specifies the arrow at the end of the edge.
*/
public void setTargetArrow(Arrow arrow) {
this.targetArrow = arrow;
}
/**
* 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
CssEdgeVisual group = new CssEdgeVisual();
Path path = new Path();
if (styleClasses != null) {
path.getStyleClass().addAll(styleClasses);
}
group.add(path);
// update the arrow if stroke or width has been changed
path.strokeProperty().addListener((observable, oldPaint, newPaint) ->
updateArrows(context, newPaint, path.getStrokeWidth()));
path.strokeWidthProperty().addListener((observable, oldWidth, newWidth) ->
updateArrows(context, path.getStroke(), newWidth));
GeneralPath generalPath = getPath(edge);
update(group, generalPath);
// add visuals that paint the arrows
addArrows(context, group, edge, generalPath, getSourceArrow(), getTargetArrow());
return group;
}
/**
* Re-renders the edge using the old visual instead of creating a new one for each call.
*/
@Override
protected Node updateVisual(IRenderContext context, Node oldVisual, IEdge edge) {
if (!(oldVisual instanceof CssEdgeVisual)) {
return createVisual(context, edge);
}
CssEdgeVisual group = (CssEdgeVisual) oldVisual;
GeneralPath generalPath = getPath(edge);
if (!Objects.equals(generalPath, group.generalPath)) {
update(group, generalPath);
}
// 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, generalPath, getSourceArrow(), getTargetArrow());
return group;
}
/**
* Updates the given visual's {@link Path} instance to reflect the geometry
* of the given {@link GeneralPath} instance.
*/
private void update(CssEdgeVisual group, GeneralPath generalPath) {
Path path = (Path) group.getChildren().get(0);
generalPath.updatePath(path, IDENTITY);
group.generalPath = generalPath;
}
/**
* Updates the arrows if the color or the width of edge has been changed.
*/
private void updateArrows(IRenderContext context, Paint stroke, Number width) {
if (sourceArrow != null) {
sourceArrow.setPaint(stroke);
sourceArrow.setPen(new Pen(stroke, width.doubleValue()));
}
if (targetArrow != null) {
targetArrow.setPaint(stroke);
targetArrow.setPen(new Pen(stroke, width.doubleValue()));
}
context.getCanvasControl().invalidate();
}
/**
* 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);
}
/**
* Stores the geometry of the visualization to speed-up visualization updates in
* {@link CssEdgeStyle#updateVisual(IRenderContext, Node, IEdge)} if the edge geometry has not changed.
*/
private static class CssEdgeVisual extends VisualGroup {
GeneralPath generalPath;
}
}