Skip to content

Commit bee2aef

Browse files
author
brunomnsilva
committed
Fixed updateAndWait() and added get/set vertex position manually on panel. #21
1 parent 61e9483 commit bee2aef

4 files changed

Lines changed: 68 additions & 15 deletions

File tree

861 Bytes
Binary file not shown.
5.84 KB
Binary file not shown.

src/com/brunomnsilva/smartgraph/Main.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.brunomnsilva.smartgraph.graph.Digraph;
4040
import com.brunomnsilva.smartgraph.graph.DigraphEdgeList;
4141
import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy;
42+
import com.brunomnsilva.smartgraph.graphview.SmartGraphVertex;
4243
import com.brunomnsilva.smartgraph.graphview.SmartStylableNode;
4344

4445
/**
@@ -90,18 +91,18 @@ public void start(Stage ignored) {
9091

9192
/*
9293
Bellow you can see how to attach actions for when vertices and edges are double clicked
93-
*/
94-
graphView.setVertexDoubleClickAction(graphVertex -> {
94+
*/
95+
graphView.setVertexDoubleClickAction((SmartGraphVertex<String> graphVertex) -> {
9596
System.out.println("Vertex contains element: " + graphVertex.getUnderlyingVertex().element());
96-
97+
9798
//toggle different styling
9899
if( !graphVertex.removeStyleClass("myVertex") ) {
99100
/* 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);
101+
css class. Otherwise, it has priority. Test and uncomment. */
102+
//graphVertex.setStyle(null);
102103

103104
graphVertex.addStyleClass("myVertex");
104-
}
105+
}
105106

106107
//want fun? uncomment below with automatic layout
107108
//g.removeVertex(graphVertex.getUnderlyingVertex());

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

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
import java.util.concurrent.Callable;
6363
import java.util.concurrent.ExecutionException;
6464
import java.util.concurrent.FutureTask;
65+
import java.util.concurrent.TimeUnit;
66+
import java.util.concurrent.TimeoutException;
6567

6668
/**
6769
* JavaFX {@link Pane} that is capable of plotting a {@link Graph} or {@link Digraph}.
@@ -345,17 +347,22 @@ public Boolean call() throws Exception {
345347
return true;
346348
}
347349
});
348-
349-
//this will be called from a non-javafx thread, so this must be guaranteed to run of the graphics thread
350-
Platform.runLater(update);
351350

352-
try {
353-
//wait for completion
354-
update.get();
355-
} catch (InterruptedException | ExecutionException ex) {
356-
Logger.getLogger(SmartGraphPanel.class.getName()).log(Level.SEVERE, null, ex);
351+
//
352+
if(!Platform.isFxApplicationThread()) {
353+
//this will be called from a non-javafx thread, so this must be guaranteed to run of the graphics thread
354+
Platform.runLater(update);
355+
356+
//wait for completion, only outside javafx thread; otherwise -> deadlock
357+
try {
358+
update.get(1, TimeUnit.SECONDS);
359+
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
360+
Logger.getLogger(SmartGraphPanel.class.getName()).log(Level.SEVERE, null, ex);
361+
}
362+
} else {
363+
updateNodes();
357364
}
358-
365+
359366
}
360367

361368
private synchronized void updateNodes() {
@@ -899,6 +906,51 @@ private Collection<Edge<E, V>> unplottedEdges() {
899906

900907
return unplotted;
901908
}
909+
910+
/**
911+
* Sets a vertex position (its center) manually.
912+
*
913+
* The positioning should be inside the boundaries of the panel, but
914+
* no restrictions are enforced by this method, so be aware.
915+
*
916+
* @param v underlying vertex
917+
* @param x x-coordinate on panel
918+
* @param y y-coordinate on panel
919+
*/
920+
public void setVertexPosition(Vertex<V> v, double x, double y) {
921+
SmartGraphVertexNode<V> node = vertexNodes.get(v);
922+
if(node != null) {
923+
node.setPosition(x, y);
924+
}
925+
}
926+
927+
/**
928+
* Return the current x-coordinate (relative to the panel) of a vertex.
929+
*
930+
* @param v underlying vertex
931+
* @return the x-coordinate or NaN if the vertex does not exist
932+
*/
933+
public double getVertexPositionX(Vertex<V> v) {
934+
SmartGraphVertexNode<V> node = vertexNodes.get(v);
935+
if(node != null) {
936+
return node.getPositionCenterX();
937+
}
938+
return Double.NaN;
939+
}
940+
941+
/**
942+
* Return the current y-coordinate (relative to the panel) of a vertex.
943+
*
944+
* @param v underlying vertex
945+
* @return the y-coordinate or NaN if the vertex does not exist
946+
*/
947+
public double getVertexPositionY(Vertex<V> v) {
948+
SmartGraphVertexNode<V> node = vertexNodes.get(v);
949+
if(node != null) {
950+
return node.getPositionCenterY();
951+
}
952+
return Double.NaN;
953+
}
902954

903955
/**
904956
* Returns the associated stylable element with a graph vertex.

0 commit comments

Comments
 (0)