All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javafx.scene.input.CommandBase Maven / Gradle / Ivy

There is a newer version: 18-ea+1
Show newest version
package javafx.scene.input;

import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableBooleanValue;
import javafx.beans.value.ObservableValue;

public abstract class CommandBase implements Command {

    private final BooleanProperty executable = new SimpleBooleanProperty(this, "executable", true);
    private final ReadOnlyBooleanWrapper executing = new ReadOnlyBooleanWrapper(this, "executing", false);
    private final ReadOnlyBooleanWrapper effectivelyExecutable = new ReadOnlyBooleanWrapper();

    protected CommandBase() {
        effectivelyExecutable.bind(executable);
    }

    protected CommandBase(ObservableValue condition) {
        effectivelyExecutable.bind(Bindings.createBooleanBinding(() -> {
            if (condition instanceof ObservableBooleanValue) {
                return ((ObservableBooleanValue)condition).get() && executable.get();
            }

            Boolean value = condition.getValue();
            return executable.get() && value != null && value;
        }, condition, executable));
    }

    @Override
    public final ReadOnlyBooleanProperty executableProperty() {
        return effectivelyExecutable.getReadOnlyProperty();
    }

    @Override
    public final ReadOnlyBooleanProperty executingProperty() {
        return executing.getReadOnlyProperty();
    }

    protected void startExecution() {
        if (!Platform.isFxApplicationThread()) {
            throw new RuntimeException(
                "Not on FX application thread; currentThread = " + Thread.currentThread().getName());
        }

        if (!isExecutable()) {
            throw new IllegalStateException("Command is not executable.");
        }

        executable.set(false);
        executing.set(true);
    }

    protected void endExecution() {
        executing.set(false);
        executable.set(true);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy