-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPortConstraintBendHandle.java
220 lines (199 loc) · 8.29 KB
/
PortConstraintBendHandle.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
/****************************************************************************
**
** 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 layout.hierarchiclayout;
import com.yworks.yfiles.geometry.GeneralPath;
import com.yworks.yfiles.geometry.Matrix2D;
import com.yworks.yfiles.geometry.PointD;
import com.yworks.yfiles.geometry.RectD;
import com.yworks.yfiles.graph.IBend;
import com.yworks.yfiles.graph.IEdge;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.graph.IPort;
import com.yworks.yfiles.graph.Mapper;
import com.yworks.yfiles.layout.PortConstraint;
import com.yworks.yfiles.layout.PortSide;
import com.yworks.yfiles.view.ICanvasObject;
import com.yworks.yfiles.view.ICanvasObjectDescriptor;
import com.yworks.yfiles.view.IRenderContext;
import com.yworks.yfiles.view.IVisualCreator;
import com.yworks.yfiles.view.Pen;
import com.yworks.yfiles.view.input.ConstrainedHandle;
import com.yworks.yfiles.view.input.IHandle;
import com.yworks.yfiles.view.input.IInputModeContext;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
/**
* Helper class that provides a handle for the first and last bend of an edge that interactively determines the port
* constraint.
*/
class PortConstraintBendHandle extends ConstrainedHandle implements IVisualCreator {
// the minimum distance to require for a port constraint
private static final int MIN_DISTANCE = 12;
private boolean sourceEnd;
private IBend bend;
private Mapper<IEdge, PortConstraint> portConstraints;
private ICanvasObject canvasObject;
PortConstraintBendHandle(boolean sourceEnd, IBend bend, IHandle originalImplementation,
Mapper<IEdge, PortConstraint> portConstraints) {
super(originalImplementation);
this.sourceEnd = sourceEnd;
this.bend = bend;
this.portConstraints = portConstraints;
}
@Override
protected void onInitialized(IInputModeContext inputModeContext, PointD originalLocation) {
super.onInitialized(inputModeContext, originalLocation);
// render the indicator
canvasObject = inputModeContext.getCanvasControl().getRootGroup().addChild(this, ICanvasObjectDescriptor.ALWAYS_DIRTY_INSTANCE);
}
@Override
protected void onCanceled(IInputModeContext inputModeContext, PointD originalLocation) {
super.onCanceled(inputModeContext, originalLocation);
// remove the indicator
canvasObject.remove();
}
@Override
protected void onFinished(IInputModeContext inputModeContext, PointD originalLocation, PointD newLocation) {
super.onFinished(inputModeContext, originalLocation, newLocation);
// remove the indicator
canvasObject.remove();
// calculate the direction
IPort port = sourceEnd ? bend.getOwner().getSourcePort() : bend.getOwner().getTargetPort();
RectD nodeLayout = ((INode) port.getOwner()).getLayout().toRectD();
PointD portLocation = nodeLayout.getCenter();
PointD bendLocation = bend.getLocation().toPointD();
PointD delta = PointD.subtract(bendLocation, portLocation);
PortConstraint pc = null;
// create a port constraint for the side to which the bend has been moved. The bend should have a minimum distance
// to the node and should not lay inside the node.
if (delta.getVectorLength() > MIN_DISTANCE && !nodeLayout.contains(bendLocation)) {
PointD direction = delta.getNormalized();
if (direction.isHorizontalVector()) {
if (direction.getX() > 0) {
pc = PortConstraint.create(PortSide.EAST);
} else {
pc = PortConstraint.create(PortSide.WEST);
}
} else {
if (direction.getY() > 0) {
pc = PortConstraint.create(PortSide.SOUTH);
} else {
pc = PortConstraint.create(PortSide.NORTH);
}
}
}
// and set the port constraint
if (pc == null) {
portConstraints.removeValue(bend.getOwner());
} else {
portConstraints.setValue(bend.getOwner(), pc);
}
}
@Override
protected PointD constrainNewLocation(IInputModeContext context, PointD originalLocation, PointD newLocation) {
// do not constrain...
return newLocation;
}
/**
* Creates a visual representation of the constraint indicator.
*/
private GeneralPath createConstraintIndicator() {
IPort port = sourceEnd ? bend.getOwner().getSourcePort() : bend.getOwner().getTargetPort();
RectD nodeLayout = ((INode) port.getOwner()).getLayout().toRectD();
PointD portLocation = nodeLayout.getCenter();
PointD bendLocation = bend.getLocation().toPointD();
PointD delta = PointD.subtract(bendLocation, portLocation);
// the indicator must have a minimum length and should not lay inside the node bounds
if (delta.getVectorLength() > MIN_DISTANCE && !nodeLayout.contains(bendLocation)) {
PointD direction = delta.getNormalized();
// create a path visualizing the constraint indicator
GeneralPath path = new GeneralPath(20);
path.moveTo(-15, 0);
path.lineTo(-5, 10);
path.lineTo(-2, 7);
path.lineTo(-5, 4);
path.lineTo(8, 4);
path.lineTo(8, -4);
path.lineTo(-5, -4);
path.lineTo(-2, -7);
path.lineTo(-5, -10);
path.close();
// mirror at target end
if (!sourceEnd) {
path.transform(new Matrix2D(-1, 0, 0, 1, 0, 0));
}
// rotate and translate arrow for the side to which the bend has been moved
int ArrowOffset = 11;
if (direction.isHorizontalVector()) {
if (direction.getX() > 0) {
path.transform(new Matrix2D(-1, 0, 0, 1, nodeLayout.getMaxX() + ArrowOffset, nodeLayout.getCenterY()));
} else {
path.transform(new Matrix2D(1, 0, 0, 1, nodeLayout.getX() - ArrowOffset, nodeLayout.getCenterY()));
}
} else {
if (direction.getY() < 0) {
path.transform(new Matrix2D(0, 1, 1, 0, nodeLayout.getCenterX(), nodeLayout.getY() - ArrowOffset));
} else {
path.transform(new Matrix2D(0, 1, -1, 0, nodeLayout.getCenterX(), nodeLayout.getMaxY() + ArrowOffset));
}
}
// and render
return path;
}
return null;
}
@Override
public Node createVisual(IRenderContext ctx) {
GeneralPath indicator = createConstraintIndicator();
if (indicator != null) {
final Path path = indicator.createPath(new Matrix2D());
path.setFill(Color.GREEN);
Pen.getBlack().styleShape(path);
return path;
} else {
return null;
}
}
@Override
public Node updateVisual(IRenderContext ctx, Node oldVisual) {
GeneralPath indicator = createConstraintIndicator();
// update an already created constraint indicator
if (indicator != null && oldVisual instanceof Path) {
final Path oldPath = (Path) oldVisual;
indicator.updatePath(oldPath, new Matrix2D());
oldPath.setFill(Color.GREEN);
Pen.getBlack().styleShape(oldPath);
return oldVisual;
} else {
return createVisual(ctx);
}
}
}