/
ConnectionSnippet.java
155 lines (140 loc) · 5.26 KB
/
ConnectionSnippet.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
/*******************************************************************************
* Copyright (c) 2014, 2016 itemis AG and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Matthias Wienand (itemis AG) - initial API and implementation
* Alexander Nyßen (itemis AG) - adjusted example to show relevant features
*
*******************************************************************************/
package org.eclipse.gef4.fx.examples;
import java.lang.reflect.Field;
import java.util.HashMap;
import org.eclipse.gef4.fx.anchors.DynamicAnchor;
import org.eclipse.gef4.fx.anchors.OrthogonalProjectionStrategy;
import org.eclipse.gef4.fx.nodes.Connection;
import org.eclipse.gef4.fx.nodes.GeometryNode;
import org.eclipse.gef4.fx.nodes.OrthogonalRouter;
import org.eclipse.gef4.geometry.planar.RoundedRectangle;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeLineJoin;
import javafx.scene.shape.StrokeType;
public class ConnectionSnippet extends AbstractFxExample {
public static class ArrowHead extends Polygon {
public ArrowHead() {
super(0, 0, 10, 3, 10, -3);
setFill(Color.WHITE);
setStroke(Color.BLACK);
setStrokeLineJoin(StrokeLineJoin.ROUND);
}
}
public static void main(String[] args) {
// XXX: This is a workaround for JDK-8143907, which happens in
// standalone
// applications on Mac OS X El Capitan
{
// TODO: Remove when dropping support for JavaSE-1.7
if (System.getProperty("java.version").startsWith("1.7.0")
&& System.getProperty("os.name").equals("Mac OS X")) {
try {
Class<?> macFontFinderClass = Class
.forName("com.sun.t2k.MacFontFinder");
Field psNameToPathMapField = macFontFinderClass
.getDeclaredField("psNameToPathMap");
psNameToPathMapField.setAccessible(true);
psNameToPathMapField.set(null,
new HashMap<String, String>());
} catch (Exception e) {
e.printStackTrace();
}
}
}
launch();
}
public ConnectionSnippet() {
super("Connection Snippet");
}
@Override
public Scene createScene() {
GeometryNode<RoundedRectangle> end1 = new GeometryNode<>(
new RoundedRectangle(50, 50, 30, 30, 20, 20));
end1.setFill(Color.RED);
end1.setStrokeWidth(3);
end1.setStrokeType(StrokeType.OUTSIDE);
makeDraggable(end1);
// use a shape
javafx.scene.shape.Rectangle end2 = new javafx.scene.shape.Rectangle(
200, 50, 30, 30);
end2.setArcWidth(20);
end2.setArcHeight(20);
end2.setStroke(Color.BLACK);
end2.setFill(Color.RED);
end2.setStrokeWidth(3);
end2.setStrokeType(StrokeType.OUTSIDE);
makeDraggable(end2);
// use a control as start, where layout bounds are always (0, 0, width,
// height); this demonstrates anchor positions are calculated properly
Label start = new Label("Some label");
start.setLayoutX(150);
start.setLayoutY(150);
makeDraggable(start);
// start.setBackground(new Background(new BackgroundFill(Color.GREY,
// CornerRadii.EMPTY, new Insets(0))));
// start.setBorder(
// new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID,
// CornerRadii.EMPTY, new BorderWidths(10))));
// set start point and end anchor
// create connection, provide decoration
Connection connection1 = new Connection();
connection1.setEndDecoration(new ArrowHead());
connection1.setStartAnchor(new DynamicAnchor(start));
connection1.setEndAnchor(new DynamicAnchor(end1));
Connection connection2 = new Connection();
connection2.setRouter(new OrthogonalRouter());
connection2.setEndDecoration(new ArrowHead());
connection2.setStartAnchor(
new DynamicAnchor(start, new OrthogonalProjectionStrategy()));
connection2.setEndAnchor(
new DynamicAnchor(end2, new OrthogonalProjectionStrategy()));
Group root = new Group();
root.getChildren().addAll(start, end1, end2, connection1, connection2);
return new Scene(root, 300, 300);
}
private void makeDraggable(final Node node) {
EventHandler<MouseEvent> dragNodeHandler = new EventHandler<MouseEvent>() {
Point2D initialMouse, initialTranslate;
@Override
public void handle(MouseEvent event) {
if (MouseEvent.MOUSE_PRESSED.equals(event.getEventType())) {
// save initial mouse location and initial node translation
// when the node is pressed
initialMouse = new Point2D(event.getSceneX(),
event.getSceneY());
initialTranslate = new Point2D(node.getTranslateX(),
node.getTranslateY());
} else {
// move the node by the mouse offset
node.setTranslateX(initialTranslate.getX()
+ event.getSceneX() - initialMouse.getX());
node.setTranslateY(initialTranslate.getY()
+ event.getSceneY() - initialMouse.getY());
}
}
};
node.addEventHandler(MouseEvent.MOUSE_PRESSED, dragNodeHandler);
node.addEventHandler(MouseEvent.MOUSE_DRAGGED, dragNodeHandler);
node.addEventHandler(MouseEvent.MOUSE_RELEASED, dragNodeHandler);
}
}