33import com .google .common .eventbus .EventBus ;
44import com .google .common .eventbus .Subscribe ;
55import com .google .inject .Singleton ;
6+ import com .sun .javafx .application .PlatformImpl ;
67import edu .wpi .grip .core .*;
78import edu .wpi .grip .core .events .*;
89import edu .wpi .grip .ui .annotations .ParametrizedController ;
910import edu .wpi .grip .ui .pipeline .input .InputSocketController ;
1011import edu .wpi .grip .ui .pipeline .source .SourceController ;
1112import edu .wpi .grip .ui .pipeline .source .SourceControllerFactory ;
12- import edu .wpi .grip .ui .util .GRIPPlatform ;
1313import edu .wpi .grip .ui .util .ControllerMap ;
14- import javafx .application .Platform ;
1514import javafx .beans .InvalidationListener ;
1615import javafx .beans .property .ReadOnlyObjectProperty ;
1716import javafx .collections .ObservableList ;
@@ -56,8 +55,6 @@ public final class PipelineController {
5655 private StepController .Factory stepControllerFactory ;
5756 @ Inject
5857 private AddSourceView addSourceView ;
59- @ Inject
60- private GRIPPlatform platform ;
6158
6259 private ControllerMap <StepController , Node > stepsMapManager ;
6360 private ControllerMap <SourceController , Node > sourceMapManager ;
@@ -182,7 +179,11 @@ private ConnectionView findConnectionView(Connection connection) {
182179 * details of adding the connection.
183180 */
184181 private void addConnectionView (Connection connection ) {
185- platform .runAsSoonAsPossible (() -> {
182+ // runAndWait() is used here because we don't want the main thread to resume, possibly causing more actions
183+ // that manipulate the list of connections, until the GUI component is added. This prevents rendering issues.
184+ // Since the code in here JUST manipulates JavaFX components and doesn't post stuff to the EventBus, it's safe
185+ // to put in runAndWait() without fear of deadlock.
186+ PlatformImpl .runAndWait (() -> {
186187 // Before adding a connection control, we have to look up the controls for both sockets in the connection so
187188 // we know where to position it.
188189 final OutputSocketController outputSocketView = findOutputSocketView (connection .getOutputSocket ());
@@ -207,35 +208,33 @@ private void addConnectionView(Connection connection) {
207208 final double x2 = inputSocketBounds .getMinX () + inputSocketBounds .getWidth () / 2.0 ;
208209 final double y2 = inputSocketBounds .getMinY () + inputSocketBounds .getHeight () / 2.0 ;
209210
210- // This can run whenever. Don't wait for it to complete.
211- // This should be Platform.runLater
212- Platform .runLater (() -> {
211+ PlatformImpl .runAndWait (() -> {
213212 connectionView .inputHandleProperty ().setValue (new Point2D (x1 , y1 ));
214213 connectionView .outputHandleProperty ().setValue (new Point2D (x2 , y2 ));
215214 ((ReadOnlyObjectProperty ) observable ).get ();
216215 });
217216 }
218217 };
219218
220- inputSocketController .getRoot ().localToSceneTransformProperty ().addListener (handleListener );
221- outputSocketView .getRoot ().localToSceneTransformProperty ().addListener (handleListener );
222- handleListener .invalidated (inputSocketController . getRoot ().localToSceneTransformProperty ());
219+ inputSocketController .getHandle ().localToSceneTransformProperty ().addListener (handleListener );
220+ outputSocketView .getHandle ().localToSceneTransformProperty ().addListener (handleListener );
221+ handleListener .invalidated (outputSocketView . getHandle ().localToSceneTransformProperty ());
223222
224223 connections .getChildren ().add (connectionView );
225224 });
226225 }
227226
228227 @ Subscribe
229228 public void onSourceAdded (SourceAddedEvent event ) {
230- platform . runAsSoonAsPossible (() -> {
229+ PlatformImpl . runAndWait (() -> {
231230 final SourceController sourceController = sourceControllerFactory .create (event .getSource ());
232231 sourceMapManager .add (sourceController );
233232 });
234233 }
235234
236235 @ Subscribe
237236 public void onSourceRemoved (SourceRemovedEvent event ) {
238- platform . runAsSoonAsPossible (() -> {
237+ PlatformImpl . runAndWait (() -> {
239238 final SourceController sourceController = findSourceView (event .getSource ());
240239 sourceMapManager .remove (sourceController );
241240 eventBus .unregister (sourceController );
@@ -245,7 +244,7 @@ public void onSourceRemoved(SourceRemovedEvent event) {
245244 @ Subscribe
246245 public void onStepAdded (StepAddedEvent event ) {
247246 // Add a new view to the pipeline for the step that was added
248- platform . runAsSoonAsPossible (() -> {
247+ PlatformImpl . runAndWait (() -> {
249248 final int index = event .getIndex ().or (stepBox .getChildren ().size ());
250249 final Step step = event .getStep ();
251250
@@ -259,7 +258,7 @@ public void onStepAdded(StepAddedEvent event) {
259258 @ Subscribe
260259 public void onStepRemoved (StepRemovedEvent event ) {
261260 // Remove the control that corresponds with the step that was removed
262- platform . runAsSoonAsPossible (() -> {
261+ PlatformImpl . runAndWait (() -> {
263262 final StepController stepController = findStepController (event .getStep ());
264263
265264 stepsMapManager .remove (stepController );
@@ -269,7 +268,7 @@ public void onStepRemoved(StepRemovedEvent event) {
269268
270269 @ Subscribe
271270 public void onStepMoved (StepMovedEvent event ) {
272- platform . runAsSoonAsPossible (() -> {
271+ PlatformImpl . runAndWait (() -> {
273272 final StepController stepController = findStepController (event .getStep ());
274273 stepsMapManager .moveByDistance (stepController , event .getDistance ());
275274 });
@@ -278,13 +277,13 @@ public void onStepMoved(StepMovedEvent event) {
278277 @ Subscribe
279278 public void onConnectionAdded (ConnectionAddedEvent event ) {
280279 // Add the new connection view
281- platform . runAsSoonAsPossible (() -> addConnectionView (event .getConnection () ));
280+ addConnectionView (event .getConnection ());
282281 }
283282
284283 @ Subscribe
285284 public void onConnectionRemoved (ConnectionRemovedEvent event ) {
286285 // Remove the control that corresponds with the connection that was removed
287- platform . runAsSoonAsPossible (() -> {
286+ PlatformImpl . runAndWait (() -> {
288287 final ConnectionView connectionView = findConnectionView (event .getConnection ());
289288 connections .getChildren ().remove (connectionView );
290289 eventBus .unregister (connectionView );
0 commit comments