Skip to content

Commit 3ec5e31

Browse files
committed
Styles applied to edges are propagated to arrows #31.
1 parent fa76ed0 commit 3ec5e31

5 files changed

Lines changed: 63 additions & 13 deletions

File tree

smartgraph.css

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,40 @@ This should help you understand which styles you can apply.
4444
-fx-opacity: 0.8;
4545
}
4646

47+
.edge:hover {
48+
-fx-stroke-width: 3;
49+
}
50+
4751
.edge-label {
4852
-fx-font: normal 5pt "sans-serif";
4953
}
5054

55+
/* Since version 2.0.0-rc2 this style is cumulatively applied to arrows, after the "edge" class.
56+
* Use to, e.g., to remove the dash effect (that will not look good in arrows).
57+
* Afterwards, styles applied to the edges are propagated to the respective arrows. You can, however, apply
58+
* specific styles to the arrows programmatically. See example programs.
59+
*/
5160
.arrow {
52-
-fx-stroke-width: 2;
53-
-fx-stroke: #FF6D66;
54-
-fx-opacity: 0.8;
61+
-fx-stroke-dash-array: none;
5562
}
5663

57-
/* Custom vertex. You should revert any unwanted styling in the default element,
58-
style, since custom styles will be appended to the default style */
64+
/* Custom vertex class. If you use node.setStyleClass("myVertex"), any previous styling
65+
* will be overwritten. If you use node.addStyleClass("myVertex"), the styles are applied
66+
* cumulatively; in the later case, any properties not wanted from the default "vertex" class
67+
* must be overwritten.
68+
*/
5969
.myVertex {
6070
-fx-stroke-width: 4;
6171
-fx-stroke: green;
6272
-fx-stroke-type: inside; /* you should keep this if using arrows */
6373
-fx-fill: yellowgreen;
74+
}
75+
76+
/* Custom edge class. The same above logic applies to edges.
77+
*/
78+
.myEdge {
79+
-fx-stroke-width: 2;
80+
-fx-stroke: red;
81+
-fx-opacity: 1;
82+
-fx-fill: transparent; /* important to keep for curved edges */
6483
}

src/main/java/com/brunomnsilva/smartgraph/Main.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ public void start(Stage ignored) {
109109

110110
graphView.setEdgeDoubleClickAction(graphEdge -> {
111111
System.out.println("Edge contains element: " + graphEdge.getUnderlyingEdge().element());
112-
//dynamically change the style when clicked
113-
graphEdge.setStyleInline("-fx-stroke: black; -fx-stroke-width: 3;");
114-
115-
graphEdge.getStylableArrow().setStyleInline("-fx-stroke: black; -fx-stroke-width: 3;");
112+
//dynamically change the style when clicked; style propagated to the arrows
113+
graphEdge.setStyleClass("myEdge");
114+
115+
// can apply different styling to the arrows programmatically.
116+
// graphEdge.getStylableArrow().setStyleClass("arrow");
116117

117118
//uncomment to see edges being removed after click
118119
//Edge<String, String> underlyingEdge = graphEdge.getUnderlyingEdge();

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartArrow.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public SmartArrow(double size) {
5252

5353
/* Add the corresponding css class */
5454
styleProxy = new SmartStyleProxy(this);
55-
styleProxy.addStyleClass("arrow");
55+
styleProxy.addStyleClass("edge"); // the same style as the edge
56+
styleProxy.addStyleClass("arrow"); // it can initially be styled through this class
5657
}
5758

5859
@Override

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphEdgeCurve.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public class SmartGraphEdgeCurve<E, V> extends CubicCurve implements SmartGraphE
5353

5454
private static final double MAX_EDGE_CURVE_ANGLE = 45;
5555
private static final double MIN_EDGE_CURVE_ANGLE = 3;
56+
57+
/** Distance (in pixels) that establishes the maximum curve threshold */
5658
public static final int DISTANCE_THRESHOLD = 400;
59+
60+
/** Radius applied to loop curves */
5761
public static final int LOOP_RADIUS_FACTOR = 4;
5862

5963
private final Edge<E, V> underlyingEdge;
@@ -110,24 +114,36 @@ public SmartGraphEdgeCurve(Edge<E, V> edge, SmartGraphVertexNode<V> inbound, Sma
110114
enableListeners();
111115
}
112116

113-
@Override
114117
public void setStyleInline(String css) {
115118
styleProxy.setStyleInline(css);
119+
if(attachedArrow != null) {
120+
attachedArrow.setStyleInline(css);
121+
}
116122
}
117123

118124
@Override
119125
public void setStyleClass(String cssClass) {
120126
styleProxy.setStyleClass(cssClass);
127+
if(attachedArrow != null) {
128+
attachedArrow.setStyleClass(cssClass);
129+
}
121130
}
122131

123132
@Override
124133
public void addStyleClass(String cssClass) {
125134
styleProxy.addStyleClass(cssClass);
135+
if(attachedArrow != null) {
136+
attachedArrow.addStyleClass(cssClass);
137+
}
126138
}
127139

128140
@Override
129141
public boolean removeStyleClass(String cssClass) {
130-
return styleProxy.removeStyleClass(cssClass);
142+
boolean result = styleProxy.removeStyleClass(cssClass);
143+
if(attachedArrow != null) {
144+
attachedArrow.removeStyleClass(cssClass);
145+
}
146+
return result;
131147
}
132148

133149
private void update() {

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphEdgeLine.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,34 @@ public SmartGraphEdgeLine(Edge<E, V> edge, SmartGraphVertexNode<V> inbound, Smar
8181
@Override
8282
public void setStyleInline(String css) {
8383
styleProxy.setStyleInline(css);
84+
if(attachedArrow != null) {
85+
attachedArrow.setStyleInline(css);
86+
}
8487
}
8588

8689
@Override
8790
public void setStyleClass(String cssClass) {
8891
styleProxy.setStyleClass(cssClass);
92+
if(attachedArrow != null) {
93+
attachedArrow.setStyleClass(cssClass);
94+
}
8995
}
9096

9197
@Override
9298
public void addStyleClass(String cssClass) {
9399
styleProxy.addStyleClass(cssClass);
100+
if(attachedArrow != null) {
101+
attachedArrow.addStyleClass(cssClass);
102+
}
94103
}
95104

96105
@Override
97106
public boolean removeStyleClass(String cssClass) {
98-
return styleProxy.removeStyleClass(cssClass);
107+
boolean result = styleProxy.removeStyleClass(cssClass);
108+
if(attachedArrow != null) {
109+
attachedArrow.removeStyleClass(cssClass);
110+
}
111+
return result;
99112
}
100113

101114

0 commit comments

Comments
 (0)