Java Code Examples for javafx.beans.property.Property#bind()

The following examples show how to use javafx.beans.property.Property#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDatatypes.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void simpleAvgBinding() {
  Property<Double> a = new SimpleObjectProperty<>();
  Property<Double> b = new SimpleObjectProperty<>();
  Property<Double>[] properties = new Property[] {a, b};
  Property<Double> avg = new SimpleObjectProperty<>();

  ObjectBinding<Double> avgBinding = Bindings.createObjectBinding(() -> {
    double sum = 0;
    int n = 0;
    for (Property<Double> p : properties) {
      if (p.getValue() != null) {
        sum += p.getValue().doubleValue();
        n++;
      }
    }
    return n == 0 ? 0 : sum / n;
  }, properties);
  avg.bind(avgBinding);
  logger.info("avg=" + avg.getValue().doubleValue() + "   " + avg.getValue());
  a.setValue(10d);
  logger.info("avg=" + avg.getValue().doubleValue() + "   " + avg.getValue());
  b.setValue(5d);
  logger.info("avg=" + avg.getValue().doubleValue() + "   " + avg.getValue());
}
 
Example 2
Source File: ConditionalBinding.java    From EasyBind with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public ConditionalBinding(
        Property<T> target,
        ObservableValue<? extends T> source,
        ObservableValue<Boolean> condition) {
    this.target = new WeakReference<>(target);
    this.source = source;
    this.condition = condition;

    // add an empty listener to target just to maintain a strong reference
    // to this object for the lifetime of target
    target.addListener(this);

    condition.addListener((ChangeListener<Boolean>) this);

    if(condition.getValue()) {
        target.bind(source);
    }
}
 
Example 3
Source File: TestDatatypes.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testBinding() {
  ModularFeatureListRow row = flistWithBinding.getRow(0);
  // add bindings first and check after changing values
  Property<Double> mzProperty = row.get(MZType.class);
  Property<Float> areaProperty = row.get(AreaType.class);

  DataType<Property<Float>> type = row.getTypeColumn(AreaType.class);
  if (type instanceof BindingsFactoryType) {
    ObjectBinding<Float> sumBind =
        (ObjectBinding<Float>) ((BindingsFactoryType) type).createBinding(BindingsType.SUM, row);
    areaProperty.bind(sumBind);
  }

  DataType<Property<Double>> typeMZ = row.getTypeColumn(MZType.class);
  if (typeMZ instanceof BindingsFactoryType) {
    ObjectBinding<Double> avgBind = (ObjectBinding<Double>) ((BindingsFactoryType) typeMZ)
        .createBinding(BindingsType.AVERAGE, row);
    mzProperty.bind(avgBind);
  }
  logger.info("avg mz=" + row.getMZ().getValue());
  logger.info("sum area=" + row.getArea().getValue());

  // add values
  for (int i = 0; i < rawsBinding.length; i++) {
    ModularFeature f = row.getFeature(rawsBinding[i]);
    f.set(AreaType.class, area[i]);
    f.set(MZType.class, mz[i]);
    logger.info("after settings values: avg mz=" + row.getMZ().getValue());
    logger.info("after setting values: sum area=" + row.getArea().getValue());
  }
}
 
Example 4
Source File: VisualizerPresenter.java    From HdrHistogramVisualizer with Apache License 2.0 5 votes vote down vote up
void bindChartAxisLabelWithDefault(final Property<String> property, final StringProperty textField) {
    // Default to the text that has been loaded from resources. Alternatively we could use
    // resources.getString("<name>"), but that would unnecessarily duplicate the resource strings.
    final String defaultText = property.getValue();
    checkState(defaultText != null && !defaultText.isEmpty(), "Expected non-empty default");
    property.bind(Bindings.createStringBinding(
            () -> !textField.get().isEmpty() ? textField.get() : defaultText,
            textField
    ));
}
 
Example 5
Source File: ConditionalBinding.java    From EasyBind with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void changed(ObservableValue<? extends Boolean> cond,
        Boolean wasTrue, Boolean isTrue) {
    Property<T> tgt = this.target.get();
    if(tgt == null) {
        condition.removeListener((ChangeListener<Boolean>) this);
    } else if(isTrue) {
        tgt.bind(source);
    } else {
        tgt.unbind();
    }
}
 
Example 6
Source File: FlatMap.java    From EasyBind with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void bind(ObservableValue<? extends U> other) {
    Property<U> target = getTargetObservable();
    if(target != null) {
        target.bind(other);
    }
    boundTo = other;
    resetOnUnbind = false;
    resetTo = null;
}
 
Example 7
Source File: FlatMap.java    From EasyBind with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void bind(ObservableValue<? extends U> other, U resetToOnUnbind) {
    Property<U> target = getTargetObservable();
    if(target != null) {
        target.bind(other);
    }
    boundTo = other;
    resetOnUnbind = true;
    resetTo = resetToOnUnbind;
}
 
Example 8
Source File: ConditionalBindingTest.java    From EasyBind with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void test() {
    Property<String> target = new SimpleStringProperty();
    Property<String> source = new SimpleStringProperty("1");
    BooleanProperty condition = new SimpleBooleanProperty(true);

    Subscription sub = EasyBind.bindConditionally(target, source, condition);

    assertTrue(target.isBound());
    assertEquals("1", target.getValue());

    source.setValue("2");
    assertEquals("2", target.getValue());

    condition.set(false);
    assertFalse(target.isBound());

    source.setValue("3");
    assertEquals("2", target.getValue());

    condition.set(true);
    assertTrue(target.isBound());
    assertEquals("3", target.getValue());

    sub.unsubscribe();
    assertFalse(target.isBound());

    condition.set(false);
    condition.set(true);
    assertFalse(target.isBound());

    source.setValue("4");
    assertEquals("3", target.getValue());

    target.bind(source);
    sub.unsubscribe();
    assertTrue(target.isBound());
}
 
Example 9
Source File: WhenTest.java    From EasyBind with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testPropertyBind() {
    Property<String> target = new SimpleStringProperty();
    Property<String> source = new SimpleStringProperty("1");
    BooleanProperty condition = new SimpleBooleanProperty(true);

    Subscription sub = EasyBind.when(condition).bind(target, source);

    assertTrue(target.isBound());
    assertEquals("1", target.getValue());

    source.setValue("2");
    assertEquals("2", target.getValue());

    condition.set(false);
    assertFalse(target.isBound());

    source.setValue("3");
    assertEquals("2", target.getValue());

    condition.set(true);
    assertTrue(target.isBound());
    assertEquals("3", target.getValue());

    sub.unsubscribe();
    assertFalse(target.isBound());

    condition.set(false);
    condition.set(true);
    assertFalse(target.isBound());

    source.setValue("4");
    assertEquals("3", target.getValue());

    target.bind(source);
    sub.unsubscribe();
    assertTrue(target.isBound());
}
 
Example 10
Source File: FibTest.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setupFor(Property<Number> resultHolder) {
    resultHolder.bind(fib[N-2].add(fib[N-1]));

    // count the invalidations of the result
    resultHolder.addListener(o -> {
        invalidationCount += 1;
        resultHolder.getValue(); // force recomputation
    });
}
 
Example 11
Source File: DesignerUtil.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Like rewireInit, with no initialisation. */
public static <T> Subscription rewire(Property<T> underlying, ObservableValue<? extends T> source) {
    underlying.unbind();
    underlying.bind(source); // Bindings are garbage collected after the popup dies
    return underlying::unbind;
}
 
Example 12
Source File: ReactfxUtil.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Like rewireInit, with no initialisation. */
public static <T> Subscription rewire(Property<T> underlying, ObservableValue<? extends T> source) {
    underlying.unbind();
    underlying.bind(source); // Bindings are garbage collected after the popup dies
    return underlying::unbind;
}