forked from Col-E/fxgraph
/
CorneredEdge.java
123 lines (102 loc) · 3.96 KB
/
CorneredEdge.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
package com.fxgraph.edges;
import com.fxgraph.graph.Graph;
import com.fxgraph.graph.ICell;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.layout.Region;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public class CorneredEdge extends AbstractEdge {
private final StringProperty textProperty;
private final Orientation orientation;
public CorneredEdge(ICell source, ICell target, Orientation orientation) {
this(source, target, false, orientation);
}
public CorneredEdge(ICell source, ICell target, boolean isDirected, Orientation orientation) {
super(source, target, isDirected);
this.orientation = orientation;
textProperty = new SimpleStringProperty();
}
@Override
public EdgeGraphic getGraphic(Graph graph) {
return new EdgeGraphic(graph, this, orientation, textProperty);
}
public StringProperty textProperty() {
return textProperty;
}
public static class EdgeGraphic extends AbstractEdgeGraphic {
public EdgeGraphic(Graph graph, CorneredEdge edge, Orientation orientation, StringProperty textProperty) {
super();
final DoubleBinding sourceX = edge.getSource().getXAnchor(graph);
final DoubleBinding sourceY = edge.getSource().getYAnchor(graph);
final DoubleBinding targetX = edge.getTarget().getXAnchor(graph);
final DoubleBinding targetY = edge.getTarget().getYAnchor(graph);
text.textProperty().bind(textProperty);
text.getStyleClass().add("edge-text");
final DoubleProperty textWidth = new SimpleDoubleProperty();
final DoubleProperty textHeight = new SimpleDoubleProperty();
final Runnable recalculateWidth = () -> {
textWidth.set(text.getLayoutBounds().getWidth());
textHeight.set(text.getLayoutBounds().getHeight());
};
text.parentProperty().addListener((obs, oldVal, newVal) -> recalculateWidth.run());
text.textProperty().addListener((obs, oldVal, newVal) -> recalculateWidth.run());
if(orientation == Orientation.HORIZONTAL) {
final Line lineA = new Line();
lineA.startXProperty().bind(sourceX);
lineA.startYProperty().bind(sourceY);
lineA.endXProperty().bind(targetX);
lineA.endYProperty().bind(sourceY);
group.getChildren().add(lineA);
if (edge.isDirected()) {
Region target = graph.getGraphic(edge.getTarget());
setupArrowIntersect(target, targetX, sourceY, targetX, targetY);
group.getChildren().add(arrow);
} else {
final Line lineB = new Line();
lineB.startXProperty().bind(targetX);
lineB.startYProperty().bind(sourceY);
lineB.endXProperty().bind(targetX);
lineB.endYProperty().bind(targetY);
group.getChildren().add(lineB);
}
text.xProperty().bind(targetX.subtract(textWidth.divide(2)));
text.yProperty().bind(sourceY.subtract(textHeight.divide(2)));
} else {
final Line lineA = new Line();
lineA.startXProperty().bind(sourceX);
lineA.startYProperty().bind(sourceY);
lineA.endXProperty().bind(sourceX);
lineA.endYProperty().bind(targetY);
group.getChildren().add(lineA);
if (edge.isDirected()) {
Region target = graph.getGraphic(edge.getTarget());
setupArrowIntersect(target, sourceX, targetY, targetX, targetY);
group.getChildren().add(arrow);
} else {
final Line lineB = new Line();
lineB.startXProperty().bind(sourceX);
lineB.startYProperty().bind(targetY);
lineB.endXProperty().bind(targetX);
lineB.endYProperty().bind(targetY);
group.getChildren().add(lineB);
}
text.xProperty().bind(sourceX.subtract(textWidth.divide(2)));
text.yProperty().bind(targetY.subtract(textHeight.divide(2)));
}
group.getChildren().add(text);
getChildren().add(group);
}
public Group getGroup() {
return group;
}
public Text getText() {
return text;
}
}
}