Skip to content

Commit dc12be0

Browse files
author
brunomnsilva
committed
New feature: SmartGraphPanel.updateAndWait()
1 parent 449d6be commit dc12be0

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

releases/JavaFXSmartGraph-0.9.jar

1.15 KB
Binary file not shown.

releases/JavaFXSmartGraph-0.9.zip

1.7 KB
Binary file not shown.

src/com/brunomnsilva/smartgraph/Main.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.brunomnsilva.smartgraph.graph.DigraphEdgeList;
4141
import com.brunomnsilva.smartgraph.graph.Edge;
4242
import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy;
43+
import com.brunomnsilva.smartgraph.graphview.SmartStylableNode;
4344

4445
/**
4546
*
@@ -232,13 +233,25 @@ private void continuously_test_adding_elements(Graph<String, String> g, SmartGra
232233
Vertex<String> existing = get_random_vertex(g);
233234
Vertex<String> vertexId = g.insertVertex(("V" + id));
234235
g.insertEdge(existing, vertexId, ("E" + id));
236+
237+
//this variant must be called to ensure the view has reflected the
238+
//underlying graph before styling a node immediately after.
239+
graphView.updateAndWait();
240+
241+
//color new vertices
242+
SmartStylableNode stylableVertex = graphView.getStylableVertex(vertexId);
243+
if(stylableVertex != null) {
244+
stylableVertex.setStyle("-fx-fill: orange;");
245+
}
235246
} else {
236247
Vertex<String> existing1 = get_random_vertex(g);
237248
Vertex<String> existing2 = get_random_vertex(g);
238249
g.insertEdge(existing1, existing2, ("E" + id));
250+
251+
graphView.update();
239252
}
240253

241-
graphView.update();
254+
242255
}
243256
};
244257

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
import static com.brunomnsilva.smartgraph.graphview.UtilitiesJavaFX.pick;
5959
import static com.brunomnsilva.smartgraph.graphview.UtilitiesPoint2D.attractiveForce;
6060
import static com.brunomnsilva.smartgraph.graphview.UtilitiesPoint2D.repellingForce;
61+
import java.util.concurrent.Callable;
62+
import java.util.concurrent.ExecutionException;
63+
import java.util.concurrent.FutureTask;
6164

6265
/**
6366
* JavaFX {@link Pane} that is capable of plotting a {@link Graph} or {@link Digraph}.
@@ -271,7 +274,7 @@ public void setAutomaticLayout(boolean value) {
271274

272275
/**
273276
* Forces a refresh of the visualization based on current state of the
274-
* underlying graph.
277+
* underlying graph, immediately returning to the caller.
275278
*
276279
* This method invokes the refresh in the graphical
277280
* thread through Platform.runLater(), so its not guaranteed that the visualization is in sync
@@ -297,6 +300,46 @@ public void update() {
297300
});
298301

299302
}
303+
304+
/**
305+
* Forces a refresh of the visualization based on current state of the
306+
* underlying graph and waits for completion of the update.
307+
*
308+
* Use this variant only when necessary, e.g., need to style an element
309+
* immediately after adding it to the underlying graph. Otherwise, use
310+
* {@link #update() } instead for performance sake.
311+
* <p>
312+
* New vertices will be added close to adjacent ones or randomly for
313+
* isolated vertices.
314+
*/
315+
public void updateAndWait() {
316+
if (this.getScene() == null) {
317+
throw new IllegalStateException("You must call this method after the instance was added to a scene.");
318+
}
319+
320+
if (!this.initialized) {
321+
throw new IllegalStateException("You must call init() method before any updates.");
322+
}
323+
324+
final FutureTask update = new FutureTask(new Callable<Boolean>() {
325+
@Override
326+
public Boolean call() throws Exception {
327+
updateNodes();
328+
return true;
329+
}
330+
});
331+
332+
//this will be called from a non-javafx thread, so this must be guaranteed to run of the graphics thread
333+
Platform.runLater(update);
334+
335+
try {
336+
//wait for completion
337+
update.get();
338+
} catch (InterruptedException | ExecutionException ex) {
339+
Logger.getLogger(SmartGraphPanel.class.getName()).log(Level.SEVERE, null, ex);
340+
}
341+
342+
}
300343

301344
private synchronized void updateNodes() {
302345
removeNodes();

0 commit comments

Comments
 (0)