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

The following examples show how to use javafx.beans.property.Property#setValue() . 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: ModularDataModel.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the value that is wrapped inside a property
 * 
 * @param <T>
 * @param tclass
 * @param value
 */
default <T extends Property<?>> void set(Class<? extends DataType<T>> tclass, Object value) {
  // type in defined columns?
  if (!getTypes().containsKey(tclass))
    throw new TypeColumnUndefinedException(this, tclass);

  DataType realType = getTypeColumn(tclass);
  Property property = get(realType);
  // lists need to be ObservableList
  if (value instanceof List && !(value instanceof ObservableList))
    property.setValue(FXCollections.observableList((List) value));
  else if (value instanceof Map && !(value instanceof ObservableMap))
    property.setValue(FXCollections.observableMap((Map) value));
  else
    property.setValue(value);
}
 
Example 2
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 3
Source File: FlatMap.java    From EasyBind with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void setValue(U value) {
    Property<U> target = getTargetObservable();
    if(target != null) {
        target.setValue(value);
    }
}
 
Example 4
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 5
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 6
Source File: SuspendedWhenTest.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void test() {
    Property<Integer> p = new SimpleObjectProperty<>(0);
    BooleanProperty suspended = new SimpleBooleanProperty(true);
    List<Integer> emitted = new ArrayList<>();
    SuspendableEventStream<Integer> pausable = EventStreams.valuesOf(p).pausable();
    Subscription sub = pausable.suspendedWhen(suspended).subscribe(emitted::add);

    // test that the stream started suspended
    assertEquals(Arrays.asList(), emitted);

    suspended.set(false);
    assertEquals(Arrays.asList(0), emitted);

    p.setValue(1);
    assertEquals(Arrays.asList(0, 1), emitted);

    suspended.set(true);
    p.setValue(2);
    p.setValue(3);
    p.setValue(4);
    assertEquals(Arrays.asList(0, 1), emitted);

    List<Integer> emitted2 = new ArrayList<>();
    pausable.subscribe(emitted2::add);
    assertEquals(Arrays.asList(), emitted2);

    suspended.set(false);
    assertEquals(Arrays.asList(0, 1, 2, 3, 4), emitted);
    assertEquals(Arrays.asList(2, 3, 4), emitted2);

    suspended.set(true);
    p.setValue(5);
    p.setValue(6);
    assertEquals(Arrays.asList(2, 3, 4), emitted2);
    sub.unsubscribe(); // testing resume on unsubscribe
    assertEquals(Arrays.asList(0, 1, 2, 3, 4), emitted);
    assertEquals(Arrays.asList(2, 3, 4, 5, 6), emitted2);
}
 
Example 7
Source File: ReactfxUtil.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static <T extends Event> Subscription addEventHandler(Property<EventHandler<T>> addMethod, EventHandler<T> handler) {
    addMethod.setValue(handler);
    return () -> addMethod.setValue(null);
}
 
Example 8
Source File: DragSupport.java    From scenic-view with GNU General Public License v3.0 4 votes vote down vote up
public DragSupport(Node target, final KeyCode modifier, final MouseButton mouseButton, final Orientation orientation, final Property<Number> property, final double factor) {
    this.target = target;
    mouseEventHandler = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent t) {
            if (t.getEventType() != MouseEvent.MOUSE_ENTERED_TARGET
                    && t.getEventType() != MouseEvent.MOUSE_EXITED_TARGET) {
                lastMouseEvent = t;
            }
            if (t.getEventType() == MouseEvent.MOUSE_PRESSED) {
                if (t.getButton() == mouseButton
                        && isModifierCorrect(t, modifier)) {
                    anchor = property.getValue();
                    dragAnchor = getCoord(t, orientation);
                    t.consume();
                }
            } else if (t.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                if (t.getButton() == mouseButton
                        && isModifierCorrect(t, modifier)) {
                    property.setValue(anchor.doubleValue()
                            + (getCoord(t, orientation) - dragAnchor) * factor);
                    t.consume();
                }
            }
        }
    };
    keyboardEventHandler = (KeyEvent t) -> {
        if (t.getEventType() == KeyEvent.KEY_PRESSED) {
            if (t.getCode() == modifier) {
                anchor = property.getValue();
                if (lastMouseEvent != null) {
                    dragAnchor = getCoord(lastMouseEvent, orientation);
                }
                t.consume();
            }
        } else if (t.getEventType() == KeyEvent.KEY_RELEASED) {
            if (t.getCode() != modifier && isModifierCorrect(t, modifier)) {
                anchor = property.getValue();
                if (lastMouseEvent != null) {
                    dragAnchor = getCoord(lastMouseEvent, orientation);
                }
                t.consume();
            }
        }
    };
    target.addEventHandler(MouseEvent.ANY, mouseEventHandler);
    target.addEventHandler(KeyEvent.ANY, keyboardEventHandler);
}
 
Example 9
Source File: CustomBidirectionalBinding.java    From JFoenix with Apache License 2.0 4 votes vote down vote up
public CustomBidirectionalBinding(Property<A> a, Property<B> b, IPropertyConverter<A, B> converter) {
    this(a, value -> a.setValue(value),
        b, value -> b.setValue(value),
        converter);
}
 
Example 10
Source File: SelectTest.java    From EasyBind with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void test() {
    Property<A> root = new SimpleObjectProperty<>();
    Binding<String> selection = EasyBind.select(root)
            .select(a -> a.b)
            .selectObject(b -> b.s);
    Counter counter = new Counter();
    selection.addListener(obs -> counter.inc());

    assertNull(selection.getValue());

    A a1 = new A();
    B b1 = new B();
    b1.s.setValue("s1");
    a1.b.setValue(b1);
    root.setValue(a1);
    assertEquals("s1", selection.getValue());
    assertEquals(1, counter.get());
    counter.reset();

    b1.s.setValue("s2");
    assertEquals("s2", selection.getValue());
    assertEquals(1, counter.get());
    counter.reset();

    B b2 = new B();
    a1.b.setValue(b2);
    assertNull(selection.getValue());
    assertEquals(1, counter.get());
    counter.reset();

    // test that changing b1 no longer invalidates the selection
    b1.s.setValue("xyz");
    assertEquals(0, counter.get());

    A a2 = new A();
    root.setValue(a2);
    assertNull(selection.getValue());
    assertEquals(1, counter.get());
    counter.reset();

    // test that changing a1 no longer invalidates the selection
    a1.b.setValue(b1);
    assertEquals(0, counter.get());

    a2.b.setValue(b1);
    assertEquals("xyz", selection.getValue());
    assertEquals(1, counter.get());
    counter.reset();

    // test that no more invalidations arrive after dispose
    selection.dispose();
    b1.s.setValue("foo");
    assertEquals(0, counter.get());
    b2.s.setValue("bar");
    assertEquals(0, counter.get());
    a1.b.setValue(new B());
    assertEquals(0, counter.get());
    a2.b.setValue(new B());
    assertEquals(0, counter.get());
    root.setValue(new A());
    assertEquals(0, counter.get());

    // test that a second call to dispose does not throw
    selection.dispose();
}