项目作者: manuel-mauky

项目描述 :
Collection of Binding helpers for JavaFX(8)
高级语言: Java
项目地址: git://github.com/manuel-mauky/advanced-bindings.git
创建时间: 2014-06-04T21:22:57Z
项目社区:https://github.com/manuel-mauky/advanced-bindings

开源协议:Apache License 2.0

下载


Advanced-Bindings for JavaFX (8)

Build Status

advanced-bindings is a collection of useful helpers and custom binding implementations to simplify the
development of applications that are heavily based on JavaFX’s Properties
and Bindings.

New Features?

If you have ideas for new custom bindings that could be added to the library feel free to add an issue.

JavaDoc

Maven Dependencies" class="reference-link"> Maven Dependencies

Advanced-Bindings releases are available in the Maven Central Repository.
You can use it like this:

Stable release

Gradle:

  1. compile 'eu.lestard:advanced-bindings:0.4.0'

Maven:

  1. <dependency>
  2. <groupId>eu.lestard</groupId>
  3. <artifactId>advanced-bindings</artifactId>
  4. <version>0.4.0</version>
  5. </dependency>

Current Development version (Snapshot)

The development version is published automatically to the Sonatype Snapshot repository.

Gradle

  1. repositories {
  2. maven {
  3. url "https://oss.sonatype.org/content/repositories/snapshots/"
  4. }
  5. }
  6. dependencies {
  7. compile 'eu.lestard:advanced-bindings:0.5.0-SNAPSHOT'
  8. }

Maven

  1. <dependency>
  2. <groupId>eu.lestard</groupId>
  3. <artifactId>advanced-bindings</artifactId>
  4. <version>0.5.0-SNAPSHOT</version>
  5. </dependency>

Features

MathBindings

eu.lestard.advanced_bindings.api.MathBindings.*

Contains Bindings for all methods of java.lang.Math

Example:

  1. @Test
  2. public void testPow(){
  3. DoubleProperty a = new SimpleDoubleProperty(3);
  4. DoubleProperty b = new SimpleDoubleProperty(2);
  5. final DoubleBinding pow = MathBindings.pow(a, b);
  6. // 3^2 = 9
  7. assertThat(pow).hasValue(9.0);
  8. a.set(5);
  9. b.set(3);
  10. // 5^3 = 125
  11. assertThat(pow).hasValue(125.0);
  12. }

NumberBindings

Example: safeDevide
A binding to express a division like Bindings.divide with the difference that no
ArithmeticException will be thrown when a division by zero happens. Instead a default value of 0 is used (or another value defined by the user).

This can be useful because with the standard divide binding you can run into problems when defining something like this:

  1. IntegerProperty a = new SimpleIntegerProperty();
  2. IntegerProperty b = new SimpleIntegerProperty();
  3. NumberBinding result = Bindings
  4. .when(b.isEqualTo(0))
  5. .then(0)
  6. .otherwise(a.divide(b));

This won’t work as expected and will throw an ArithmeticException because the expression a.divide(b)
is evaluated independently from the b.isEqualTo(0) condition.

With Advanced-Bindings you can write this instead:

  1. IntegerProperty a = new SimpleIntegerProperty();
  2. IntegerProperty b = new SimpleIntegerProperty();
  3. IntegerBinding result = NumberBindings.divideSafe(a,b);

This won’t throw an ArithmenticException even when b has a value of 0.
While this is “wrong” from a mathematical point of view it can simplify the development
of bindings based applications a lot, for example when b is bound to a Slider that
has an initial value of 0.

Example: isNaN

  1. @Test
  2. public void testIsNan(){
  3. DoubleProperty a = new SimpleDoubleProperty();
  4. DoubleProperty b = new SimpleDoubleProperty();
  5. final DoubleBinding quotient = a.divide(b);
  6. BooleanBinding nan = NumberBindings.isNaN(quotient);
  7. a.set(2);
  8. b.set(4);
  9. assertThat(nan).isFalse();
  10. a.set(0);
  11. b.set(0);
  12. assertThat(nan).isTrue();
  13. }

Example: isInfinite

  1. @Test
  2. public void testIsInfinite(){
  3. DoubleProperty a = new SimpleDoubleProperty();
  4. DoubleProperty b = new SimpleDoubleProperty();
  5. DoubleBinding product = a.multiply(b);
  6. BooleanBinding infinite = NumberBindings.isInfinite(product);
  7. a.set(2);
  8. b.set(4);
  9. assertThat(infinite).isFalse();
  10. b.set(Double.MAX_VALUE);
  11. assertThat(infinite).isTrue();
  12. }

StringBindings

Example: RegExp

  1. @Test
  2. public void testMatches(){
  3. StringProperty text = new SimpleStringProperty();
  4. String pattern = "[0-9]*"; // only numbers are allowed
  5. BooleanBinding matches = StringBindings.matches(text, pattern);
  6. text.set("19");
  7. assertThat(matches).isTrue();
  8. text.set("no number");
  9. assertThat(matches).isFalse();
  10. }

CollectionBindings

Example: Sum of all integers in an observable list

  1. @Test
  2. public void testSum(){
  3. ObservableList<Integer> numbers = FXCollections.observableArrayList();
  4. NumberBinding sum = CollectionBindings.sum(numbers);
  5. numbers.addAll(1, 2, 3, 5, 8, 13, 21);
  6. assertThat(sum).hasValue(53.0);
  7. numbers.add(34);
  8. assertThat(sum).hasValue(87.0);
  9. }

SwitchBindings

In JavaFX there are standard methods to create IF-THEN-ELSE bindings.
But there is no way to create something like a switch for use cases
where there are a lot of cases.

Advanced-Bindings has a builder to create switch like bindings:

  1. IntegerProperty base = new SimpleIntegerProperty();
  2. ObservableValue<String> result = switchBinding(base, String.class)
  3. .bindCase(1, i -> "one")
  4. .bindCase(3, i -> "three")
  5. .bindCase(10, i -> "ten")
  6. .bindDefault(() -> "nothing")
  7. .build();

There are two differences to the normal switch statement of Java:

  • While the standard switch statement has a limitation to numbers, Strings and Enums.
    Only those types can be used as control variable. There is no such limitation for the Switch binding.
    Every type that has a properly overwritten equals method can be used.
  • There is no “fall-through” and therefore no break is needed.