Example usage for javafx.application Platform isFxApplicationThread

List of usage examples for javafx.application Platform isFxApplicationThread

Introduction

In this page you can find the example usage for javafx.application Platform isFxApplicationThread.

Prototype

public static boolean isFxApplicationThread() 

Source Link

Document

Returns true if the calling thread is the JavaFX Application Thread.

Usage

From source file:org.eclipse.jubula.rc.javafx.driver.EventThreadQueuerJavaFXImpl.java

/**
 * Executes the given Callable on the JavaFX-Thread and waits for the
 * termination/*from  www . jav a 2 s.c  o m*/
 *
 * @param name
 *            a name to identifier which Callable is being executed
 * @param <V>
 *            return value type
 * @param call
 *            the Callable
 * @return
 * @return the return value of the given Callable
 * @throws ExecutionException
 * @throws InterruptedException
 */
public static <V> V invokeAndWait(String name, Callable<V> call) {

    if (Platform.isFxApplicationThread()) {
        try {
            return call.call();
        } catch (Exception e) {
            // the run() method from IRunnable has thrown an exception
            // -> log on info
            // -> throw a StepExecutionException
            Throwable thrown = e.getCause();
            if (thrown instanceof StepExecutionException) {
                if (log.isInfoEnabled()) {
                    log.info(e);
                }
                throw (StepExecutionException) thrown;
            }

            // any other (unchecked) Exception from IRunnable.run()
            log.error("exception thrown by '" + name //$NON-NLS-1$
                    + "':", thrown); //$NON-NLS-1$
            throw new StepExecutionException(thrown);
        }
    }
    try {
        FutureTask<V> task = new FutureTask<>(call);
        Platform.runLater(task);
        return task.get();

    } catch (InterruptedException ie) {
        // this (the waiting) thread was interrupted -> error
        log.error(ie);
        throw new StepExecutionException(ie);
    } catch (ExecutionException ee) {
        // the run() method from IRunnable has thrown an exception
        // -> log on info
        // -> throw a StepExecutionException
        Throwable thrown = ee.getCause();
        if (thrown instanceof StepExecutionException) {
            if (log.isInfoEnabled()) {
                log.info(ee);
            }
            throw (StepExecutionException) thrown;
        }

        // any other (unchecked) Exception from IRunnable.run()
        log.error("exception thrown by '" + name //$NON-NLS-1$
                + "':", thrown); //$NON-NLS-1$
        throw new StepExecutionException(thrown);
    }
}

From source file:org.eclipse.jubula.rc.javafx.driver.EventThreadQueuerJavaFXImpl.java

/**
 * // w  ww  .  java  2  s. c om
 * @throws IllegalStateException if the current thread is not the FX Thread.
 */
public static void checkEventThread() throws IllegalStateException {
    if (!Platform.isFxApplicationThread()) {
        throw new IllegalStateException("Not on FX application thread; currentThread = " //$NON-NLS-1$
                + Thread.currentThread().getName());
    }
}

From source file:org.eclipse.jubula.rc.javafx.driver.EventThreadQueuerJavaFXImpl.java

/**
 * /*from w  ww. j  a v  a 2 s .  c  o m*/
 * @throws IllegalStateException if the current thread is the FX Thread.
 */
public static void checkNotEventThread() throws IllegalStateException {
    if (Platform.isFxApplicationThread()) {
        throw new IllegalStateException("On FX application thread, although this is not allowed."); //$NON-NLS-1$
    }
}

From source file:org.nmrfx.processor.gui.MainApp.java

@Override
public void datasetAdded(Dataset dataset) {
    if (Platform.isFxApplicationThread()) {
        FXMLController.updateDatasetList();
    } else {//  w  w  w  . j  av  a  2  s  .c o  m
        Platform.runLater(() -> {
            FXMLController.updateDatasetList();
        });
    }
}

From source file:org.nmrfx.processor.gui.MainApp.java

@Override
public void datasetRemoved(Dataset dataset) {
    if (Platform.isFxApplicationThread()) {
        FXMLController.updateDatasetList();
    } else {//from ww w . j a va  2 s.  co m
        Platform.runLater(() -> {
            FXMLController.updateDatasetList();
        });
    }
}

From source file:org.nmrfx.processor.gui.MainApp.java

@Override
public void datasetRenamed(Dataset dataset) {
    if (Platform.isFxApplicationThread()) {
        FXMLController.updateDatasetList();
    } else {//from w  w  w  .  j av  a 2 s. c  o m
        Platform.runLater(() -> {
            FXMLController.updateDatasetList();
        });
    }
}

From source file:org.nmrfx.processor.gui.spectra.DatasetAttributes.java

public void config(String name, Object value) {
    if (Platform.isFxApplicationThread()) {
        try {/* www.j a va  2s  . c  o m*/
            PropertyUtils.setSimpleProperty(this, name, value);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            Logger.getLogger(DatasetAttributes.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        Platform.runLater(() -> {
            try {
                PropertyUtils.setProperty(this, name, value);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
                Logger.getLogger(DatasetAttributes.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    }
}

From source file:org.openbase.display.DisplayView.java

private <V> Future<V> runTask(final Callable<V> callable) throws CouldNotPerformException {
    try {//from   w ww .j  av  a  2  s. c om

        if (Platform.isFxApplicationThread()) {
            try {
                return CompletableFuture.completedFuture(callable.call());
            } catch (Exception ex) {
                ExceptionPrinter.printHistory(new CouldNotPerformException("Could not perform task!", ex),
                        logger);
            }
        }

        FutureTask<V> future = new FutureTask(() -> {
            try {
                return callable.call();
            } catch (Exception ex) {
                throw ExceptionPrinter.printHistoryAndReturnThrowable(ex, logger);
            }
        });
        Platform.runLater(future);
        return future;
    } catch (Exception ex) {
        throw new CouldNotPerformException("Could not perform task!", ex);
    }
}

From source file:org.phoenicis.javafx.UiMessageSenderJavaFXImplementation.java

@Override
public <R> R run(Supplier<R> function) {

    // runBackground synchronously on JavaFX thread
    if (Platform.isFxApplicationThread()) {
        return function.get();
    }//from w  w  w.j  av a2  s  .c o  m

    // queue on JavaFX thread and wait for completion
    final CountDownLatch doneLatch = new CountDownLatch(1);
    final MutableObject result = new MutableObject();
    Platform.runLater(() -> {
        try {
            result.setValue(function.get());
        } finally {
            doneLatch.countDown();
        }
    });

    try {
        doneLatch.await();
    } catch (InterruptedException e) {
        // ignore exception
    }

    return (R) result.getValue();
}

From source file:qupath.lib.gui.panels.survival.KaplanMeierDisplay.java

@Override
public void hierarchyChanged(PathObjectHierarchyEvent event) {
    if (!Platform.isFxApplicationThread())
        Platform.runLater(() -> hierarchyChanged(event));
    else/*from w w w .jav a2 s  .  c  o  m*/
        generatePlot();
}