/** Binds each value in {@code p} to a view created by {@code factory}. */ public void to(final IsInsertPanel panel, final ListViewFactory<P> factory) { to(new InsertPanelListLikeAdapter(panel), factory); }
public void toMultiple(final IsListBox source, final List<P> options, final ListBoxLambdaAdaptor<P> adaptor) { toMultiple(source, options, (ListBoxAdaptor<P, P>) adaptor); }
/** @return a fluent {@link ListPropertyBinder} against {@code property}. */ public <P> ListPropertyBinder<P> bind(ListProperty<P> property) { return new ListPropertyBinder<P>(this, property); }
/** Binds our {@code p} to the selection in {@code source}, given the {@code options}. */ public <O> void toMultiple(final IsListBox source, final List<O> options, final ListBoxAdaptor<P, O> adaptor) { source.setMultipleSelect(true); addOptionsAndSetIfNull(source, options, adaptor); b.add(source.addChangeHandler(e -> { if (!active[0]) { active[0] = true; // collect all currently-selected options List<P> newOptions = new ArrayList<P>(); for (int i = 0; i < source.getItemCount(); i++) { if (source.isItemSelected(i)) { newOptions.add(adaptor.toValue(options.get(i))); } } p.set(newOptions); active[0] = false; } })); b.add(p.addPropertyChangedHandler(e -> { if (!active[0]) { active[0] = true; setToFirstIfNull(options, adaptor); for (int i = 0; i < options.size(); i++) { boolean contains = p.get().contains(adaptor.toValue(options.get(i))); source.setItemSelected(i, contains); } active[0] = false; } })); }
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); for (int j = 0; j < options.size(); j++) { boolean contains = p.get().contains(adaptor.toValue(options.get(j))); source.setItemSelected(j, contains); } }
@Test public void newPresentersAreAddedToPanel() { binder.bind(names).to(parent, panel, presenterFactory); assertThat(panel.getWidgetCount(), is(0)); names.add("one"); assertLabel(panel.getIsWidget(0), "one"); assertThat(parent.getChildren().size(), is(1)); }
/** Binds our {@code p} to the selection in {@code source}, given the {@code options}. */ public void toMultiple(final IsListBox source, final List<P> options) { toMultiple(source, options, new ListBoxIdentityAdaptor<P>()); }
@Test public void oldPresentersAreRemovedFromPanel() { binder.bind(names).to(parent, panel, presenterFactory); names.add("one"); names.add("two"); assertThat(panel.getWidgetCount(), is(2)); assertThat(parent.getChildren().size(), is(2)); names.remove("one"); assertThat(panel.getWidgetCount(), is(1)); assertLabel(panel.getIsWidget(0), "two"); assertThat(parent.getChildren().size(), is(1)); }
@Test public void toListBoxWithAdaptor() { List<String> options = Copy.list("one", "two", "three"); names.add("one"); binder.bind(names).toMultiple(box, options, o -> o.toUpperCase()); assertThat(box.getSelectedTexts(), contains("ONE")); names.add("two"); assertThat(box.getSelectedTexts(), contains("ONE", "TWO")); box.setItemSelected(2, true); assertThat(names.get(), contains("one", "two", "three")); }
@Test public void bindDoesNotNullPointerExceptionForViews() { names.set(null); binder.bind(names).to(panel, viewFactory); names.set(Copy.list("one")); assertThat(panel.getWidgetCount(), is(1)); }
@Test public void toListBox() { List<String> options = Copy.list("one", "two", "three"); names.add("one"); binder.bind(names).toMultiple(box, options); assertThat(box.getSelectedTexts(), contains("one")); names.add("two"); assertThat(box.getSelectedTexts(), contains("one", "two")); box.setItemSelected(2, true); assertThat(names.get(), contains("one", "two", "three")); }
@Test public void newNamesAreAddedToPanel() { binder.bind(names).to(panel, viewFactory); assertThat(panel.getWidgetCount(), is(0)); names.add("one"); assertLabel(panel.getIsWidget(0), "one"); }
@Test public void initialPresentersAreAddedToPanel() { names.add("one"); names.add("two"); binder.bind(names).to(parent, panel, presenterFactory); assertLabel(panel.getIsWidget(0), "one"); assertLabel(panel.getIsWidget(1), "two"); assertThat(parent.getChildren().size(), is(2)); }
@Test public void toTextList() { names.add("one"); StubTextList list = new StubTextList(); binder.bind(names).to(list); assertThat(list.getList(), contains("one")); names.add("two"); assertThat(list.getList(), contains("one", "two")); }
@Test public void reordersToDoNotRequireCreatingNewViews() { binder.bind(names).to(panel, viewFactory); names.set(list("one", "two")); assertThat(panel.getWidgetCount(), is(2)); assertLabel(panel.getIsWidget(0), "one"); assertThat(viewFactory.created, is(2)); names.set(list("two", "one")); assertThat(panel.getWidgetCount(), is(2)); assertLabel(panel.getIsWidget(0), "two"); assertThat(viewFactory.created, is(2)); }
@Test public void oldNamesAreRemovedFromPanel() { binder.bind(names).to(panel, viewFactory); names.add("one"); names.add("two"); assertThat(panel.getWidgetCount(), is(2)); names.remove("one"); assertThat(panel.getWidgetCount(), is(1)); assertLabel(panel.getIsWidget(0), "two"); }
@Test public void reordersToDoNotRequireCreatingNewPresenters() { binder.bind(names).to(parent, panel, presenterFactory); names.set(list("one", "two")); assertThat(panel.getWidgetCount(), is(2)); assertLabel(panel.getIsWidget(0), "one"); assertThat(presenterFactory.created, is(2)); names.set(list("two", "one")); assertThat(panel.getWidgetCount(), is(2)); assertLabel(panel.getIsWidget(0), "two"); assertThat(presenterFactory.created, is(2)); }
@Test public void bindDoesNotNullPointerExceptionForPresenters() { names.set(null); ParentPresenter parent = bind(new ParentPresenter()); binder.bind(names).to(parent, panel, presenterFactory); names.set(Copy.list("one")); assertThat(panel.getWidgetCount(), is(1)); }
@Test public void newNamesAreAddedWithIndexToPanel() { binder.bind(names).to(panel, viewFactory); names.add("one"); assertLabel(panel.getIsWidget(0), "one"); names.add(0, "two"); assertLabel(panel.getIsWidget(0), "two"); }
@Test public void initialNamesAreAddedToPanel() { names.add("one"); names.add("two"); binder.bind(names).to(panel, viewFactory); assertLabel(panel.getIsWidget(0), "one"); assertLabel(panel.getIsWidget(1), "two"); }