Advertisement
Guest User

JavaFX Optional select bindings.

a guest
Jan 24th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. import javafx.beans.binding.Bindings;
  2. import javafx.beans.binding.ObjectBinding;
  3. import javafx.beans.property.*;
  4. import javafx.beans.value.ObservableValue;
  5.  
  6. import java.util.Optional;
  7. import java.util.logging.ConsoleHandler;
  8. import java.util.logging.Handler;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /*
  13.  * This shows a potential way of dealing with null path elements when using
  14.  * JavaFX select bindings.
  15.  *
  16.  * NOTE: For this to work, the Bindings#select method(s) must not catch
  17.  * java.util.NoSuchElementException.
  18.  *
  19.  * This goal of this example is to bind to a StringProperty with a path of:
  20.  *
  21.  *   parent model --> child model --> string property (name)
  22.  *
  23.  * The parent model is never null.  However, both the child model and the
  24.  * string property can potentially be null:
  25.  *
  26.  *   parent model --> child model --> null
  27.  *   parent model --> null
  28.  *
  29.  * This example shows how to use Optional along with Bindings#select to create
  30.  * two types of bindings:
  31.  *
  32.  * 1. A binding that will throw and exception when one of the properties along
  33.  * the select path contains a null value.
  34.  *
  35.  * 2. A binding that will take an alternate, default value that is used whenever
  36.  * one of the properties along the select path contains a null value.
  37.  *
  38.  * Use the static variables to control the behaviour of the example.
  39.  */
  40.  
  41. public class SelectBindingExample {
  42.     private static final boolean useNullChildInBindingPath = false;
  43.     private static final boolean useNullNameInChildModel = true;
  44.     private static final boolean useThrowBinding = true;
  45.  
  46.     private static final Level logLevel = Level.WARNING;
  47.  
  48.     // Taken from SO: http://stackoverflow.com/a/981230/624726
  49.     static {
  50.         Logger topLogger = java.util.logging.Logger.getLogger("");
  51.  
  52.         Handler consoleHandler = null;
  53.         for (Handler handler : topLogger.getHandlers()) {
  54.             if (handler instanceof ConsoleHandler) {
  55.                 consoleHandler = handler;
  56.                 break;
  57.             }
  58.         }
  59.  
  60.         if (consoleHandler == null) {
  61.             consoleHandler = new ConsoleHandler();
  62.             topLogger.addHandler(consoleHandler);
  63.         }
  64.         consoleHandler.setLevel(logLevel);
  65.     }
  66.  
  67.     @SuppressWarnings({"PointlessBooleanExpression", "ConstantConditions"})
  68.     public static void main(String[] args) {
  69.         Model model = new Model();
  70.  
  71.         if(!useNullChildInBindingPath) {
  72.             Model childModel = new Model();
  73.             if(!useNullNameInChildModel) {
  74.                 childModel.setName("Child Model Name");
  75.             }
  76.             model.setChildModel(childModel);
  77.         }
  78.  
  79.         ObjectBinding<String> orThrowBinding =
  80.                 OptionalBindings.orThrow(
  81.                         OptionalBindings.select(
  82.                                 model.childModelProperty(), "name"
  83.                         ));
  84.  
  85.         ObjectBinding<String> orElseBinding =
  86.                 OptionalBindings.orElse(
  87.                         OptionalBindings.select(
  88.                                 model.childModelProperty(), "name"
  89.                         ), "Alternate Name");
  90.  
  91.         printName(useThrowBinding ? orThrowBinding : orElseBinding);
  92.     }
  93.  
  94.     private static void printName(ObjectBinding<String> nameBinding) {
  95.         System.out.println("  Name is " + nameBinding.get() + ".");
  96.     }
  97.  
  98.     public static class Model {
  99.         private final ObjectProperty<Model> childModel = new SimpleObjectProperty<>();
  100.         public final ReadOnlyObjectProperty<Model> childModelProperty() {return childModel;}
  101.         public final Model getChildModel() {return childModel.get();}
  102.         public final void setChildModel(Model model) {this.childModel.set(model);}
  103.  
  104.         private final StringProperty name = new SimpleStringProperty();
  105.         public final ReadOnlyStringProperty nameProperty() {return name;}
  106.         public final String getName() {return name.get();}
  107.         protected final void setName(String name) {this.name.set(name);}
  108.     }
  109.  
  110.     public static class OptionalBindings {
  111.         public static <T> ObjectBinding<Optional<T>> select(ObservableValue<?> observable, String... steps) {
  112.             ObjectBinding<T> selectBinding = Bindings.select(observable, steps);
  113.  
  114.             return Bindings.createObjectBinding(
  115.                     () -> Optional.ofNullable(selectBinding.getValue()), // Callable
  116.                     selectBinding // Dependencies
  117.             );
  118.         }
  119.  
  120.         public static <T> ObjectBinding<T> orElse(ObjectBinding<Optional<T>> optionalBinding, T other) {
  121.             return Bindings.createObjectBinding(
  122.                     () -> optionalBinding.get().orElse(other), // Callable
  123.                     optionalBinding // Dependencies
  124.             );
  125.         }
  126.  
  127.         public static <T> ObjectBinding<T> orThrow(ObjectBinding<Optional<T>> optionalBinding) {
  128.             return Bindings.createObjectBinding(
  129.                     () -> optionalBinding.get().get(), // Callable
  130.                     optionalBinding // Dependencies
  131.             );
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement