Skip to content

Commit 61e9483

Browse files
author
brunomnsilva
committed
Improved css styling behavior for #14
1 parent 34a1f3a commit 61e9483

12 files changed

Lines changed: 185 additions & 31 deletions
66.1 KB
Binary file not shown.
377 KB
Binary file not shown.

src/com/brunomnsilva/smartgraph/Main.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.brunomnsilva.smartgraph.containers.SmartGraphDemoContainer;
3939
import com.brunomnsilva.smartgraph.graph.Digraph;
4040
import com.brunomnsilva.smartgraph.graph.DigraphEdgeList;
41-
import com.brunomnsilva.smartgraph.graph.Edge;
4241
import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy;
4342
import com.brunomnsilva.smartgraph.graphview.SmartStylableNode;
4443

@@ -95,8 +94,14 @@ public void start(Stage ignored) {
9594
graphView.setVertexDoubleClickAction(graphVertex -> {
9695
System.out.println("Vertex contains element: " + graphVertex.getUnderlyingVertex().element());
9796

98-
//style differently with other css class:
99-
graphVertex.setStyleClass("myVertex");
97+
//toggle different styling
98+
if( !graphVertex.removeStyleClass("myVertex") ) {
99+
/* for the golden vertex, this is necessary to clear the inline
100+
css class. Otherwise, it has priority. Test and uncomment. */
101+
//graphVertex.setStyle(null);
102+
103+
graphVertex.addStyleClass("myVertex");
104+
}
100105

101106
//want fun? uncomment below with automatic layout
102107
//g.removeVertex(graphVertex.getUnderlyingVertex());
@@ -108,6 +113,7 @@ public void start(Stage ignored) {
108113
//dynamically change the style when clicked
109114
graphEdge.setStyle("-fx-stroke: black; -fx-stroke-width: 2;");
110115

116+
111117
//uncomment to see edges being removed after click
112118
//Edge<String, String> underlyingEdge = graphEdge.getUnderlyingEdge();
113119
//g.removeEdge(underlyingEdge);

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
*/
3535
public class SmartArrow extends Path implements SmartStylableNode {
3636

37+
/* Styling proxy */
38+
private final SmartStyleProxy styleProxy;
39+
3740
public SmartArrow() {
3841

3942
/* Create this arrow shape */
@@ -43,12 +46,23 @@ public SmartArrow() {
4346
getElements().add(new LineTo(-5, -5));
4447

4548
/* Add the corresponding css class */
46-
getStyleClass().add("arrow");
49+
styleProxy = new SmartStyleProxy(this);
50+
styleProxy.addStyleClass("arrow");
4751
}
4852

4953
@Override
5054
public void setStyleClass(String cssClass) {
51-
getStyleClass().add(cssClass);
55+
styleProxy.setStyleClass(cssClass);
56+
}
57+
58+
@Override
59+
public void addStyleClass(String cssClass) {
60+
styleProxy.addStyleClass(cssClass);
61+
}
62+
63+
@Override
64+
public boolean removeStyleClass(String cssClass) {
65+
return styleProxy.removeStyleClass(cssClass);
5266
}
5367

5468
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class SmartGraphEdgeCurve<E, V> extends CubicCurve implements SmartGraphE
6262
private SmartArrow attachedArrow = null;
6363

6464
private double randomAngleFactor = 0;
65+
66+
/* Styling proxy */
67+
private final SmartStyleProxy styleProxy;
6568

6669
public SmartGraphEdgeCurve(Edge<E, V> edge, SmartGraphVertexNode inbound, SmartGraphVertexNode outbound) {
6770
this(edge, inbound, outbound, 0);
@@ -73,7 +76,8 @@ public SmartGraphEdgeCurve(Edge<E, V> edge, SmartGraphVertexNode inbound, SmartG
7376

7477
this.underlyingEdge = edge;
7578

76-
getStyleClass().add("edge");
79+
styleProxy = new SmartStyleProxy(this);
80+
styleProxy.addStyleClass("edge");
7781

7882
//bind start and end positions to vertices centers through properties
7983
this.startXProperty().bind(outbound.centerXProperty());
@@ -90,9 +94,17 @@ public SmartGraphEdgeCurve(Edge<E, V> edge, SmartGraphVertexNode inbound, SmartG
9094

9195
@Override
9296
public void setStyleClass(String cssClass) {
93-
getStyleClass().clear();
94-
setStyle(null);
95-
getStyleClass().add(cssClass);
97+
styleProxy.setStyleClass(cssClass);
98+
}
99+
100+
@Override
101+
public void addStyleClass(String cssClass) {
102+
styleProxy.addStyleClass(cssClass);
103+
}
104+
105+
@Override
106+
public boolean removeStyleClass(String cssClass) {
107+
return styleProxy.removeStyleClass(cssClass);
96108
}
97109

98110
private void update() {

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public class SmartGraphEdgeLine<E, V> extends Line implements SmartGraphEdgeBase
4747
private SmartLabel attachedLabel = null;
4848
private SmartArrow attachedArrow = null;
4949

50+
/* Styling proxy */
51+
private final SmartStyleProxy styleProxy;
52+
5053
public SmartGraphEdgeLine(Edge<E, V> edge, SmartGraphVertexNode inbound, SmartGraphVertexNode outbound) {
5154
if( inbound == null || outbound == null) {
5255
throw new IllegalArgumentException("Cannot connect null vertices.");
@@ -57,7 +60,8 @@ public SmartGraphEdgeLine(Edge<E, V> edge, SmartGraphVertexNode inbound, SmartGr
5760

5861
this.underlyingEdge = edge;
5962

60-
getStyleClass().add("edge");
63+
styleProxy = new SmartStyleProxy(this);
64+
styleProxy.addStyleClass("edge");
6165

6266
//bind start and end positions to vertices centers through properties
6367
this.startXProperty().bind(outbound.centerXProperty());
@@ -68,9 +72,17 @@ public SmartGraphEdgeLine(Edge<E, V> edge, SmartGraphVertexNode inbound, SmartGr
6872

6973
@Override
7074
public void setStyleClass(String cssClass) {
71-
getStyleClass().clear();
72-
setStyle(null);
73-
getStyleClass().add(cssClass);
75+
styleProxy.setStyleClass(cssClass);
76+
}
77+
78+
@Override
79+
public void addStyleClass(String cssClass) {
80+
styleProxy.addStyleClass(cssClass);
81+
}
82+
83+
@Override
84+
public boolean removeStyleClass(String cssClass) {
85+
return styleProxy.removeStyleClass(cssClass);
7486
}
7587

7688

src/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ private void addVertex(SmartGraphVertexNode<V> v) {
485485
if (graphProperties.getUseVertexLabel()) {
486486
SmartLabel label = new SmartLabel(labelText);
487487

488-
label.getStyleClass().add("vertex-label");
488+
label.addStyleClass("vertex-label");
489489
this.getChildren().add(label);
490490
v.attachLabel(label);
491491
}
@@ -508,7 +508,7 @@ private void addEdge(SmartGraphEdgeBase e, Edge<E, V> edge) {
508508
if (graphProperties.getUseEdgeLabel()) {
509509
SmartLabel label = new SmartLabel(labelText);
510510

511-
label.getStyleClass().add("edge-label");
511+
label.addStyleClass("edge-label");
512512
this.getChildren().add(label);
513513
e.attachLabel(label);
514514
}

src/com/brunomnsilva/smartgraph/graphview/SmartGraphVertexNode.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public class SmartGraphVertexNode<T> extends Circle implements SmartGraphVertex<
6464
private final PointVector forceVector = new PointVector(0, 0);
6565
private final PointVector updatedPosition = new PointVector(0, 0);
6666

67+
/* Styling proxy */
68+
private final SmartStyleProxy styleProxy;
69+
6770
/**
6871
* Constructor which sets the instance attributes.
6972
*
@@ -82,20 +85,14 @@ public SmartGraphVertexNode(Vertex<T> v, double x, double y, double radius, bool
8285

8386
this.adjacentVertices = new HashSet<>();
8487

85-
getStyleClass().add("vertex");
88+
styleProxy = new SmartStyleProxy(this);
89+
styleProxy.addStyleClass("vertex");
8690

8791
if (allowMove) {
8892
enableDrag();
8993
}
9094
}
9195

92-
@Override
93-
public void setStyleClass(String cssClass) {
94-
getStyleClass().clear();
95-
setStyle(null);
96-
getStyleClass().add(cssClass);
97-
}
98-
9996
/**
10097
* Adds a vertex to the internal list of adjacent vertices.
10198
*
@@ -336,6 +333,22 @@ public Vertex<T> getUnderlyingVertex() {
336333
return underlyingVertex;
337334
}
338335

336+
337+
@Override
338+
public void setStyleClass(String cssClass) {
339+
styleProxy.setStyleClass(cssClass);
340+
}
341+
342+
@Override
343+
public void addStyleClass(String cssClass) {
344+
styleProxy.addStyleClass(cssClass);
345+
}
346+
347+
@Override
348+
public boolean removeStyleClass(String cssClass) {
349+
return styleProxy.removeStyleClass(cssClass);
350+
}
351+
339352
/**
340353
* Internal representation of a 2D point or vector for quick access to its
341354
* attributes.

src/com/brunomnsilva/smartgraph/graphview/SmartLabel.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,35 @@
3434
* @author Bruno Silva
3535
*/
3636
public class SmartLabel extends Text implements SmartStylableNode {
37-
37+
38+
private final SmartStyleProxy styleProxy;
39+
3840
public SmartLabel() {
41+
this(0,0,"");
3942
}
4043

4144
public SmartLabel(String text) {
42-
super(text);
45+
this(0, 0, text);
4346
}
4447

4548
public SmartLabel(double x, double y, String text) {
4649
super(x, y, text);
50+
styleProxy = new SmartStyleProxy(this);
4751
}
4852

4953
@Override
5054
public void setStyleClass(String cssClass) {
51-
getStyleClass().add(cssClass);
55+
styleProxy.setStyleClass(cssClass);
56+
}
57+
58+
@Override
59+
public void addStyleClass(String cssClass) {
60+
styleProxy.addStyleClass(cssClass);
61+
}
62+
63+
@Override
64+
public boolean removeStyleClass(String cssClass) {
65+
return styleProxy.removeStyleClass(cssClass);
5266
}
5367

5468
}

src/com/brunomnsilva/smartgraph/graphview/SmartStylableNode.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@
3636
public interface SmartStylableNode {
3737

3838
/**
39-
* Applies cumulatively the <code>css</code> styles to the node.
39+
* Applies cumulatively the <code>css</code> inline styles to the node.
4040
*
41-
* Note that JavaFX styles are cumulative.
41+
* These inline JavaFX styles have higher priority and are not overwritten by
42+
* any css classes set by {@link SmartStylableNode#addStyleClass(java.lang.String) }.
43+
* But will be discarded if you use {@link SmartStylableNode#setStyleClass(java.lang.String) }
44+
*
45+
* If you need to clear any previously set inline styles, use
46+
* <code>.setStyle(null)</code>
4247
*
4348
* @param css styles
4449
*/
@@ -50,12 +55,40 @@ public interface SmartStylableNode {
5055
* The <code>cssClass</code> string must not contain a preceding dot, e.g.,
5156
* "myClass" instead of ".myClass".
5257
*
53-
* The CSS Class must be defined in <code>smartpgraph.css</code> file.
58+
* The CSS Class must be defined in <code>smartpgraph.css</code> file or
59+
* in the custom provided stylesheet.
5460
*
55-
* The expected behavior is to remove all current styling before
61+
* The expected behavior is to remove all current styling before
5662
* applying the class css.
5763
*
5864
* @param cssClass name of the CSS class.
5965
*/
6066
public void setStyleClass(String cssClass);
67+
68+
/**
69+
* Applies cumulatively the CSS styling defined in class selector
70+
* <code>cssClass</code>.
71+
*
72+
* The CSS Class must be defined in <code>smartpgraph.css</code> file or
73+
* in the custom provided stylesheet.
74+
*
75+
* The cumulative operation will overwrite any existing styling elements
76+
* previously defined for previous classes.
77+
*
78+
* @param cssClass name of the CSS class.
79+
*/
80+
public void addStyleClass(String cssClass);
81+
82+
/**
83+
* Removes a previously <code>cssClass</code> existing CSS styling.
84+
*
85+
* Given styles can be added sequentially, the removal of a css class
86+
* will be a removal that keeps the previous ordering of kept styles.
87+
*
88+
* @param cssClass name of the CSS class.
89+
*
90+
* @return true if successful; false if <code>cssClass</code> wasn't
91+
* previously set.
92+
*/
93+
public boolean removeStyleClass(String cssClass);
6194
}

0 commit comments

Comments
 (0)