Example usage for javafx.beans.property ReadOnlyBooleanProperty addListener

List of usage examples for javafx.beans.property ReadOnlyBooleanProperty addListener

Introduction

In this page you can find the example usage for javafx.beans.property ReadOnlyBooleanProperty addListener.

Prototype

void addListener(ChangeListener<? super T> listener);

Source Link

Document

Adds a ChangeListener which will be notified whenever the value of the ObservableValue changes.

Usage

From source file:Main.java

/**
 * This methods blocks until the worker is done and returns the result value of the worker.
 * If the worker was canceled an exception will be thrown.
 *
 * @param worker The worker//from w w  w. ja  va  2 s.c o  m
 * @param <T> result type of the worker
 * @return the result
 * @throws InterruptedException if the worker was canceled
 */
public static <T> T waitFor(Worker<T> worker) throws InterruptedException {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    lock.lock();
    try {
        ReadOnlyBooleanProperty doneProperty = createIsDoneProperty(worker);
        if (doneProperty.get()) {
            return worker.getValue();
        } else {
            doneProperty.addListener(e -> {
                boolean locked = lock.tryLock();
                if (locked) {
                    try {
                        condition.signal();
                    } finally {
                        lock.unlock();
                    }
                } else {
                    throw new RuntimeException("Concurreny Error");
                }
            });
            condition.await();
            return worker.getValue();
        }
    } finally {
        lock.unlock();
    }
}

From source file:Main.java

/**
 * The given consumer will be called once the worker is finished. The result of the worker
 * will be passed to the consumer.//from  ww w  .  j  a  v  a 2s  . c o m
 * @param worker the worker
 * @param consumer the consumer
 * @param <T> the resukt type of the worker
 */
public static <T> void then(Worker<T> worker, Consumer<T> consumer) {
    ReadOnlyBooleanProperty doneProperty = createIsDoneProperty(worker);
    ChangeListener<Boolean> listener = (o, oldValue, newValue) -> {
        if (newValue) {
            consumer.accept(worker.getValue());
        }
    };
    doneProperty.addListener(listener);
}