Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

list - JavaFX - bind property to properties of every element in observable Collection

Does exist any method which bind BooleanProperty to conjunction of every element in ObservableList?

ObservableList<BooleanProperty> list;
list = FXCollections.observableList(new ArrayList<BooleanProperty>));
BooleanProperty emptyProperty = new SimpleBooleanProperty();
emptyProperty.bind(Bindings.conunction(list));`

Is there such a method as:

static BooleanBinding conjunction(ObservableList<BooleanProperty> op)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no conjunction api defined in the JavaFX 2.2 platform.

You could create a ConjunctionBooleanBinding (aka AllTrueBinding) by subclassing BooleanBinding.

Accept the ObservableList in the constructor of your new class, and use the low level binding api in an overridden computeValue method to set the binding value based upon logically anding together all of the boolean values in the list.

Here is a sample implementation. The sample could be further performance optimized and make use of WeakReferences, so it does not require manual disposition.

import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.collections.*;

public class AllTrueBinding extends BooleanBinding {
  private final ObservableList<BooleanProperty> boundList;
  private final ListChangeListener<BooleanProperty> BOUND_LIST_CHANGE_LISTENER =
    new ListChangeListener<BooleanProperty>() {
      @Override public void onChanged(
             ListChangeListener.Change<? extends BooleanProperty> change
          ) {
        refreshBinding();
      }
    };
  private BooleanProperty[] observedProperties = {};

  AllTrueBinding(ObservableList<BooleanProperty> booleanList) {
    booleanList.addListener(BOUND_LIST_CHANGE_LISTENER);
    boundList = booleanList;
    refreshBinding();
  }

  @Override protected boolean computeValue() {
    for (BooleanProperty bp: observedProperties) {
      if (!bp.get()) {
        return false;
      }
    }

    return true;
  }

  @Override public void dispose() {
    boundList.removeListener(BOUND_LIST_CHANGE_LISTENER);
    super.dispose();
  }

  private void refreshBinding() {
    super.unbind(observedProperties);
    observedProperties = boundList.toArray(new BooleanProperty[0]);
    super.bind(observedProperties);
    this.invalidate();
  }
}

And here is a test harness to demonstrate how it works:

import java.util.*;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class ListBindingTest {
  final BooleanProperty a = new SimpleBooleanProperty(true);
  final BooleanProperty b = new SimpleBooleanProperty(true);
  final BooleanProperty c = new SimpleBooleanProperty(true);
  final BooleanProperty d = new SimpleBooleanProperty(true);
  final ObservableList<BooleanProperty> booleanList =
    FXCollections.observableArrayList(a, b, c, d);

  public static void main(String[] args) {
    new ListBindingTest().test();
  }

  private void test() {
    AllTrueBinding at = new AllTrueBinding(booleanList);

    System.out.println(at.get() + forArrayString(booleanList));

    b.set(false);
    System.out.println(at.get() + forArrayString(booleanList));

    b.set(true);
    System.out.println(at.get() + forArrayString(booleanList));

    booleanList.add(new SimpleBooleanProperty(false));
    System.out.println(at.get() + forArrayString(booleanList));

    booleanList.remove(3, 5);
    System.out.println(at.get() + forArrayString(booleanList));

    at.dispose();
  }  

  private String forArrayString(List list) {
    return " for " + Arrays.toString(list.toArray());
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...