
org.fxmisc.easybind.select.LeafSelectionElement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easybind Show documentation
Show all versions of easybind Show documentation
Factory methods for easy creation of JavaFX bindings using lambdas.
The newest version!
package org.fxmisc.easybind.select;
import java.util.function.Function;
import javafx.beans.InvalidationListener;
import javafx.beans.value.ObservableValue;
class LeafSelectionElement implements NestedSelectionElement {
private final InvalidationListener observableInvalidationListener = obs -> observableInvalidated();
private final Runnable onInvalidation;
private final Function super T, ObservableValue> selector;
private ObservableValue observable = null;
public LeafSelectionElement(Runnable onInvalidation, Function super T, ObservableValue> selector) {
this.onInvalidation = onInvalidation;
this.selector = selector;
}
@Override
public void connect(T baseVal) {
if(isConnected()) {
throw new IllegalStateException("Already connected");
}
observable = selector.apply(baseVal);
observable.addListener(observableInvalidationListener);
}
@Override
public void disconnect() {
if(isConnected()) {
observable.removeListener(observableInvalidationListener);
observable = null;
}
}
@Override
public final boolean isConnected() {
return observable != null;
}
@Override
public U getValue() {
if(!isConnected()) {
throw new IllegalStateException("Not connected");
}
return observable.getValue();
}
private void observableInvalidated() {
onInvalidation.run();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy