Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.beans.binding.Bindings;
- import javafx.beans.binding.ObjectBinding;
- import javafx.beans.property.*;
- import javafx.beans.value.ObservableValue;
- import java.util.Optional;
- import java.util.logging.ConsoleHandler;
- import java.util.logging.Handler;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /*
- * This shows a potential way of dealing with null path elements when using
- * JavaFX select bindings.
- *
- * NOTE: For this to work, the Bindings#select method(s) must not catch
- * java.util.NoSuchElementException.
- *
- * This goal of this example is to bind to a StringProperty with a path of:
- *
- * parent model --> child model --> string property (name)
- *
- * The parent model is never null. However, both the child model and the
- * string property can potentially be null:
- *
- * parent model --> child model --> null
- * parent model --> null
- *
- * This example shows how to use Optional along with Bindings#select to create
- * two types of bindings:
- *
- * 1. A binding that will throw and exception when one of the properties along
- * the select path contains a null value.
- *
- * 2. A binding that will take an alternate, default value that is used whenever
- * one of the properties along the select path contains a null value.
- *
- * Use the static variables to control the behaviour of the example.
- */
- public class SelectBindingExample {
- private static final boolean useNullChildInBindingPath = false;
- private static final boolean useNullNameInChildModel = true;
- private static final boolean useThrowBinding = true;
- private static final Level logLevel = Level.WARNING;
- // Taken from SO: http://stackoverflow.com/a/981230/624726
- static {
- Logger topLogger = java.util.logging.Logger.getLogger("");
- Handler consoleHandler = null;
- for (Handler handler : topLogger.getHandlers()) {
- if (handler instanceof ConsoleHandler) {
- consoleHandler = handler;
- break;
- }
- }
- if (consoleHandler == null) {
- consoleHandler = new ConsoleHandler();
- topLogger.addHandler(consoleHandler);
- }
- consoleHandler.setLevel(logLevel);
- }
- @SuppressWarnings({"PointlessBooleanExpression", "ConstantConditions"})
- public static void main(String[] args) {
- Model model = new Model();
- if(!useNullChildInBindingPath) {
- Model childModel = new Model();
- if(!useNullNameInChildModel) {
- childModel.setName("Child Model Name");
- }
- model.setChildModel(childModel);
- }
- ObjectBinding<String> orThrowBinding =
- OptionalBindings.orThrow(
- OptionalBindings.select(
- model.childModelProperty(), "name"
- ));
- ObjectBinding<String> orElseBinding =
- OptionalBindings.orElse(
- OptionalBindings.select(
- model.childModelProperty(), "name"
- ), "Alternate Name");
- printName(useThrowBinding ? orThrowBinding : orElseBinding);
- }
- private static void printName(ObjectBinding<String> nameBinding) {
- System.out.println(" Name is " + nameBinding.get() + ".");
- }
- public static class Model {
- private final ObjectProperty<Model> childModel = new SimpleObjectProperty<>();
- public final ReadOnlyObjectProperty<Model> childModelProperty() {return childModel;}
- public final Model getChildModel() {return childModel.get();}
- public final void setChildModel(Model model) {this.childModel.set(model);}
- private final StringProperty name = new SimpleStringProperty();
- public final ReadOnlyStringProperty nameProperty() {return name;}
- public final String getName() {return name.get();}
- protected final void setName(String name) {this.name.set(name);}
- }
- public static class OptionalBindings {
- public static <T> ObjectBinding<Optional<T>> select(ObservableValue<?> observable, String... steps) {
- ObjectBinding<T> selectBinding = Bindings.select(observable, steps);
- return Bindings.createObjectBinding(
- () -> Optional.ofNullable(selectBinding.getValue()), // Callable
- selectBinding // Dependencies
- );
- }
- public static <T> ObjectBinding<T> orElse(ObjectBinding<Optional<T>> optionalBinding, T other) {
- return Bindings.createObjectBinding(
- () -> optionalBinding.get().orElse(other), // Callable
- optionalBinding // Dependencies
- );
- }
- public static <T> ObjectBinding<T> orThrow(ObjectBinding<Optional<T>> optionalBinding) {
- return Bindings.createObjectBinding(
- () -> optionalBinding.get().get(), // Callable
- optionalBinding // Dependencies
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement