Skip to content

Commit a618810

Browse files
committed
Check click consumers separately when null.
1 parent 332237b commit a618810

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public class SmartGraphPanel<V, E> extends Pane {
9999
/*
100100
INTERACTION WITH VERTICES AND EDGES
101101
*/
102-
private Consumer<SmartGraphVertex<V>> vertexClickConsumer = null;
103-
private Consumer<SmartGraphEdge<E, V>> edgeClickConsumer = null;
102+
private Consumer<SmartGraphVertex<V>> vertexClickConsumer;
103+
private Consumer<SmartGraphEdge<E, V>> edgeClickConsumer;
104104

105105
/*
106106
OPTIONAL PROVIDERS FOR LABELS, RADII AND SHAPE TYPES OF NODES.
@@ -308,6 +308,11 @@ public SmartGraphPanel(@NamedArg("graph") Graph<V, E> theGraph,
308308
this.edgeNodes = new HashMap<>();
309309
this.connections = new HashMap<>();
310310

311+
// consumers initially are not set. This initialization is not necessary, but we make it explicit
312+
// for the sake of readability
313+
this.vertexClickConsumer = null;
314+
this.edgeClickConsumer = null;
315+
311316
//set stylesheet and class
312317
loadAndApplyStylesheet(cssFile);
313318

@@ -1293,10 +1298,6 @@ private void enableDoubleClickListener() {
12931298
setOnMouseClicked((MouseEvent mouseEvent) -> {
12941299
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
12951300
if (mouseEvent.getClickCount() == 2) {
1296-
//no need to continue otherwise
1297-
if (vertexClickConsumer == null && edgeClickConsumer == null) {
1298-
return;
1299-
}
13001301

13011302
Node node = pick(SmartGraphPanel.this, mouseEvent.getSceneX(), mouseEvent.getSceneY());
13021303
if (node == null) {
@@ -1305,10 +1306,14 @@ private void enableDoubleClickListener() {
13051306

13061307
if (node instanceof SmartGraphVertex) {
13071308
SmartGraphVertex<V> v = (SmartGraphVertex<V>) node;
1308-
vertexClickConsumer.accept(v);
1309+
if(vertexClickConsumer != null) { // Only if the consumer is set
1310+
vertexClickConsumer.accept(v);
1311+
}
13091312
} else if (node instanceof SmartGraphEdge) {
13101313
SmartGraphEdge<E,V> e = (SmartGraphEdge<E,V>) node;
1311-
edgeClickConsumer.accept(e);
1314+
if(edgeClickConsumer != null) { // Only if the consumer is set
1315+
edgeClickConsumer.accept(e);
1316+
}
13121317
}
13131318
}
13141319
}

0 commit comments

Comments
 (0)