public void to(final IsListBox source, final List<P> options, final Function<P, String> optionToDisplay) { to(source, options, new ListBoxAdaptor<P, P>() { @Override public String toDisplay(P option) { return optionToDisplay.get(option); } @Override public P toValue(P option) { return option; } }); }
/** @return a fluent {@link PropertyBinder} against {@code property}. */ public <P> PropertyBinder<P> bind(Property<P> property) { return new PropertyBinder<P>(this, property); }
private <O> void addOptionsAndSetIfNull(final IsListBox source, final List<O> options, final ListBoxAdaptor<P, O> adaptor) { int i = 0; for (final O option : options) { source.addItem(adaptor.toDisplay(option), Integer.toString(i++)); } setToFirstIfNull(options, adaptor); source.setSelectedIndex(indexInOptions(adaptor, options)); }
/** Binds our {@code p} to the selection in {@code source}, given the {@code options}. */ public <O> void to(final IsListBox source, final List<O> options, final ListBoxAdaptor<P, O> adaptor) { addOptionsAndSetIfNull(source, options, adaptor); b.add(source.addChangeHandler(e -> { final int i = source.getSelectedIndex(); // getSelectedIndex within an onchange should never be -1, but check just in case if (i != -1) { p.set(adaptor.toValue(options.get(i))); } })); b.add(p.addPropertyChangedHandler(e -> { setToFirstIfNull(options, adaptor); source.setSelectedIndex(indexInOptions(adaptor, options)); })); }
/** Binds our {@code p} to the selection in {@code source}, given the {@code options}. */ public <O> void to(final IsListBox source, final ListProperty<O> options, final ListBoxAdaptor<P, O> adaptor) { if (options.get() != null) { addOptionsAndSetIfNull(source, options.get(), adaptor); } b.add(source.addChangeHandler(e -> { final int i = source.getSelectedIndex(); // getSelectedIndex within an onchange should never be -1, but check just in case if (i != -1) { p.set(adaptor.toValue(options.get().get(i))); } })); b.add(p.addPropertyChangedHandler(e -> { setToFirstIfNull(options.get(), adaptor); source.setSelectedIndex(indexInOptions(adaptor, options.get())); })); options.addPropertyChangedHandler(e -> { // it looks like this does not cause an onchange in the browser source.clear(); if (options.get() != null) { addOptionsAndSetIfNull(source, options.get(), adaptor); // reselect the 1st value if p's current value is not available. if (p.get() != null && indexInOptions(adaptor, options.get()) == -1 && !options.get().isEmpty()) { p.setInitialValue(adaptor.toValue(options.get().get(0))); } } }); }
/** Binds our {@code p} to the selection in {@code source}, given the {@code options}. */ public <O> void to( final IsListBox source, final ListProperty<O> options, final Function<O, String> optionToDisplay, final Function<O, P> optionToValue) { to(source, options, new ListBoxFunctionsAdaptor<P, O>(optionToDisplay, optionToValue)); }
/** @return a fluent {@link PropertyBinder} against {@code property}. */ public <P> PropertyBinder<P> bind(LambdaValue<P> value) { return new PropertyBinder<P>(this, derivedProperty(value)); }
/** Binds our {@code p} to the selection in {@code source}, given the {@code options}. */ public void to(final IsListBox source, final List<P> options) { to(source, options, new ListBoxIdentityAdaptor<P>()); }
public <O> void to(// final IsListBox source, final List<O> options, final Function<O, String> optionToDisplay, final Function<O, P> optionToValue) { to(source, options, new ListBoxFunctionsAdaptor<P, O>(optionToDisplay, optionToValue)); }
/** Bind our property to source, two-way, and shows view when our property is true. */ public void to(final HasValue<Boolean> source, HasCss view) { super.to(source); b.when(bp).is(true).show(view); }
@Override public void bind(final FormPresenter p, PropertyGroup all, Binder binder) { super.bind(p, all, binder); textBox.ensureDebugId(id); errorList.ensureDebugId(id + "-errors"); p.watchForEnterKey(textBox); binder.bind(property).to(textBox, errorList); binder.fireChangeOnBlur(textBox); }
@Override public void onBind() { super.onBind(); // two-way binding between checked and view.box() binder.bind(checked).to(view.box()); // one-way binding between our derived value and view.label()'s text binder.bind(() -> checked.isTrue() ? "Checked!" : "Not checked").to(textOf(view.label())); }
@Test public void stringPropertyToWidgetSetsMaxLengthViaGenericPropertyBinder() { s.max(100); // use a Property<String>, so overloading returns the regular // PropertyBinder instead of StringPropertyBinder Property<String> p = s; binder.bind(p).to(box); assertThat(box.getMaxLength(), is(100)); }
@Test public void testSettingInitialValueWhenBound() { IntegerProperty i = integerProperty("i"); Property<String> s = i.asString(); StubTextBox textBox = new StubTextBox(); Binder b = new Binder(); b.bind(s).to(textBox); i.setInitialValue(1); assertThat(i.isTouched(), is(false)); }
@Test public void testSettingValueWithTouchIsFalseWhenBound() { IntegerProperty i = integerProperty("i"); Property<String> s = i.asString(); StubTextBox textBox = new StubTextBox(); Binder b = new Binder(); b.bind(s).to(textBox); i.set(1, false); assertThat(i.isTouched(), is(false)); }
@Test public void testLambda() { s.set("foo"); binder.bind(() -> s.get() + "bar").to(textOf(anchor)); assertThat(anchor.getText(), is("foobar")); s.set("zaz"); assertThat(anchor.getText(), is("zazbar")); }
@Test public void binderDoesFireChangedIfPropertyAlreadySet() { f.name.set("foo"); final StubHasValue<Integer> value = new StubHasValue<Integer>(); Binder b = new Binder(); b.bind(f.name.remaining()).to(value); assertThat(value.getValue(), is(7)); }
@Test public void setBadStringThatIsBoundToATextBox() { IntegerProperty i = integerProperty("i", 1).req(); Binder binder = new Binder(); StubTextBox box = new StubTextBox(); binder.bind(i.asString()).to(box); listenTo(i); box.type("a"); assertThat(box.getText(), is("a")); assertMessages("I must be an integer"); assertThat(i.get(), is(1)); assertThat(box.getText(), is("a")); i.set(2); assertNoMessages(); box.type(""); assertMessages("I is required"); }