/** Shows {@code hasCss} only when the command is active. */ public UiCommandBinder spin(final HasCss hasCss) { if (command instanceof HasActive) { Property<Boolean> active = ((HasActive) command).active(); b.when(active).is(true).visible(hasCss); } else { throw new IllegalStateException("Not a command that has .active() available"); } return this; }
/** @return a fluent {@link WhenBinder} against {@code property}. */ public <P> WhenBinder<P> when(Property<P> property) { return new WhenBinder<P>(this, property); }
@Test public void whenIsAnyOf() { final IntegerProperty i = integerProperty("i"); final StubWidget w = new StubWidget(); binder.when(i).isAnyOf(1, 2, 3).setOrElse("c", "d").on(w); assertThat(w, hasStyle("d")); assertThat(w, not(hasStyle("c"))); i.set(1); assertThat(w, hasStyle("c")); assertThat(w, not(hasStyle("d"))); i.set(4); assertThat(w, hasStyle("d")); assertThat(w, not(hasStyle("c"))); i.set(3); assertThat(w, hasStyle("c")); assertThat(w, not(hasStyle("d"))); }
/** 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); }
/** @return a fluent {@link WhenBinder} against {@code lambda}. */ public <P> WhenBinder<P> when(LambdaValue<P> lambda) { return new WhenBinder<P>(this, derivedProperty(lambda)); }
@Override public void bind(FormPresenter p, Binder binder) { String id = p.getId() + "-" + Inflector.camelize(text); button.ensureDebugId(id); if (addOnlyIf) { command.addOnlyIf(p.allValid()); } binder.when(command.enabled()).is(true).show(button); if (command instanceof DispatchUiCommand) { binder.when(((DispatchUiCommand<?, ?>) command).active()).is(false).enable(button); } }
/** Binds clicks from {@code clickable} to our command. */ public UiCommandBinder to(final HasClickHandlers clickable) { b.add(clickable.addClickHandler(e -> { command.execute(); e.preventDefault(); })); // Assume that if command.active() is available, and HasEnabled is available, // it just makes sense to disable clickable while the command is active if (command instanceof HasActive && clickable instanceof HasEnabled) { Property<Boolean> active = ((HasActive) command).active(); b.when(active).is(true).disable((HasEnabled) clickable); } return this; }
@Test public void whenTrueRemovesFromInitialSet() { final BooleanProperty b = booleanProperty("b", false); final ArrayList<String> list = new ArrayList<String>(); list.add("foo"); binder.when(b).is(true).add("foo").to(list); assertThat(list, not(hasItem("foo"))); }
@Test public void whenTrueAddDoesInitialSet() { final BooleanProperty b = booleanProperty("b", true); final ArrayList<String> list = new ArrayList<String>(); binder.when(b).is(true).add("foo").to(list); assertThat(list, contains("foo")); }
@Test public void whenValueAddToListInitializesProperty() { final EnumProperty<Color> color = enumProperty("color"); // null so we'll do the initial set final ArrayList<String> list = new ArrayList<String>(); list.add("foo"); binder.when(color).is(Color.BLUE).add("foo").to(list); assertThat(color.get(), is(Color.BLUE)); assertThat(color.isTouched(), is(false)); assertThat(list, contains("foo")); }
@Test public void whenValueAddToListDoesNotInitializeProperty() { final EnumProperty<Color> color = enumProperty("color"); final ArrayList<String> list = new ArrayList<String>(); // foo isn't in the list, so we leave color alone binder.when(color).is(Color.BLUE).add("foo").to(list); assertThat(color.get(), is(nullValue())); assertThat(color.isTouched(), is(false)); }
@Test public void whenIsFadeInStartsFalse() { final BooleanProperty b = booleanProperty("b", false); final StubWidget w = new StubWidget(); binder.when(b).is(true).fadeIn(w); // if condition is false, jump right to display none assertThat(w, is(hidden())); assertThat(w.getStyle().getOpacity(), is(nullValue())); }
@Test public void whenValueRun() { final boolean[] ran = { false }; final BooleanProperty b = booleanProperty("b", true); binder.when(b).is(false).run(new Runnable() { public void run() { ran[0] = true; } }); assertThat(ran[0], is(false)); b.set(false); assertThat(ran[0], is(true)); }
@Test public void whenValueRemoveFromListInitializesProperty() { final EnumProperty<Color> color = enumProperty("color"); final ArrayList<String> list = new ArrayList<String>(); binder.when(color).is(Color.BLUE).remove("foo").from(list); assertThat(color.get(), is(Color.BLUE)); assertThat(color.isTouched(), is(false)); }
@Test public void whenTrueEnableLeavesEnabled() { final BooleanProperty b = booleanProperty("b", true); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).enable(w); assertThat(w.isEnabled(), is(true)); }
@Test public void whenTrueEnableIsFalseThenSetsDisabled() { final BooleanProperty b = booleanProperty("b", false); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).enable(w); assertThat(w.isEnabled(), is(false)); }
@Test public void whenTrueShowHidesWhenFalse() { final BooleanProperty b = booleanProperty("b", false); final StubWidget w = new StubWidget(); binder.when(b).is(true).show(w); assertThat(w, is(hidden())); }
@Test public void whenTrueDisabledChangesToDisabled() { final BooleanProperty b = booleanProperty("b", true); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).disable(w); assertThat(w.isEnabled(), is(false)); }
@Test public void whenTrueShowDisplaysWhenTrue() { final BooleanProperty b = booleanProperty("b", true); final StubWidget w = new StubWidget(); binder.when(b).is(true).show(w); assertThat(w, is(shown())); }
@Test public void whenSetToTakesValuesDoesNotTouchOnInitialEvaluation() { final BooleanProperty b = booleanProperty("b", true); final StringProperty s = stringProperty("s"); binder.when(b).is(true).set(s).to("t"); assertThat(s.get(), is("t")); assertThat(s.isTouched(), is(false)); }