@Override public IsListBox newListBox() { return new StubListBox(); }
public String getSelectedText() { return getItemText(getSelectedIndex()); }
@Override public void addItem(final String item) { addItem(item, item); }
public void select(final String... texts) { Integer[] indexes = new Integer[texts.length]; for (int i = 0; i < texts.length; i++) { indexes[i] = getItemIndex(texts[i]); } setSelectedIndexes(indexes); }
public void select(final String text) { setSelectedIndex(getItemIndex(text)); }
@Test public void propertyToListBoxUpdatesListBoxWhenPropertyChanges() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list(null, "a", "b"); binder.bind(s).to(listBox, values); assertThat(listBox.getSelectedIndex(), is(0)); s.set("b"); assertThat(listBox.getSelectedIndex(), is(2)); s.set(null); assertThat(listBox.getSelectedIndex(), is(0)); }
@Test public void propertyToListBoxHandlesEmptyString() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list("", "a", "b"); binder.bind(s).to(listBox, values); assertThat(listBox.getSelectedIndex(), is(0)); listBox.select("b"); assertThat(s.get(), is("b")); listBox.select(""); assertThat(s.get(), is("")); }
@Test public void propertyToListBoxWithLambdas() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list(null, "1", "2"); binder.bind(s).to(listBox, values, s -> "line " + s); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("line null")); s.set("2"); assertThat(listBox.getSelectedIndex(), is(2)); assertThat(listBox.getSelectedText(), is("line 2")); s.set(null); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("line null")); }
@Test public void listPropertyToListBoxHandlesNullValue() { final StubListBox listBox = new StubListBox(); final ListProperty<String> values = listProperty("values", list(null, "a", "b")); binder.bind(s).to(listBox, values); listBox.select("b"); assertThat(s.get(), is("b")); // null gets converted to "" (otherwise it shows up as "null"), so select that listBox.select(""); assertThat(s.get(), is(nullValue())); }
@Test public void bindEnumCreatesItems() { final SetValue<Color> v = new SetValue<Color>("v", Color.BLUE); final EnumProperty<Color> e = enumProperty(v); final StubListBox box = new StubListBox(); binder.bind(e).to(box, Color.values()); assertThat(box.getItemCount(), is(2)); assertThat(box.getItemText(0), is("Green")); assertThat(box.getItemText(1), is("Blue (not green)")); }
public List<String> getSelectedTexts() { List<String> texts = new ArrayList<String>(); for (Integer index : getSelectedIndexes()) { texts.add(getItemText(index)); } return texts; }
@Test public void listPropertyToListBoxChangesListBoxContents() { final StubListBox listBox = new StubListBox(); final ListProperty<String> values = listProperty("values", list("a", "b")); binder.bind(s).to(listBox, values); assertThat(listBox.getItems(), contains("a", "b")); values.set(list("c", "d")); assertThat(listBox.getItems(), contains("c", "d")); values.set(null); assertThat(listBox.getItems().size(), is(0)); }
@Test public void listPropertyToListBoxResetsPropertyWhenSelectedValueGoesAway() { final StubListBox listBox = new StubListBox(); // given we're binding against a list of values that does not include null final ListProperty<Integer> values = listProperty("values", list(1, 2)); // and s starts out null assertThat(s.get(), is(nullValue())); // when we bind binder.bind(s).to(listBox, values, new IntegerAdaptor()); // then s is set to the 1st option assertThat(s.get(), is("1")); assertThat(listBox.getSelectedText(), is("1")); // when later our selected value goes away values.set(list(3, 4)); // then the property is again set to the 1st option assertThat(s.get(), is("3")); assertThat(listBox.getSelectedText(), is("3")); }
@Test public void listBoxIsBound() { p.add(newListBoxFormLine(employee.employerId, employers, new EmployerDtoAdaptor())); assertThat(listBox("p-employerId").getItemCount(), is(2)); assertThat(listBox("p-employerId").getItems(), contains("Employer One", "Employer Two")); }
@Override public void setSelectedIndex(final int index) { for (final Item i : items) { i.selected = false; } if (index != -1) { items.get(index).selected = true; } fireChange(); }
private void fireChange() { fireEvent(new StubChangeEvent()); }
@Test public void propertyToListBoxDoesNotFailWhenThereAreNoOptions() { final StubListBox listBox = new StubListBox(); final ArrayList<Integer> values = new ArrayList<Integer>(); // s starts out null assertThat(s.get(), is(nullValue())); binder.bind(s).to(listBox, values, new IntegerAdaptor()); // and s is still null assertThat(s.get(), is(nullValue())); // and so we were unable to select anything assertThat(listBox.getSelectedIndex(), is(-1)); }
@Test public void propertyToListBoxHandlesNullValue() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list(null, "a", "b"); binder.bind(s).to(listBox, values); assertThat(listBox.getSelectedIndex(), is(0)); listBox.select("b"); assertThat(s.get(), is("b")); // null gets converted to "" (otherwise it shows up as "null"), so select that listBox.select(""); assertThat(s.get(), is(nullValue())); }
@Test public void propertyToListBoxWithTwoLambdas() { final StubListBox listBox = new StubListBox(); final ArrayList<Integer> values = list(null, 1, 2); binder.bind(s).to(// listBox, values, i -> (i == null) ? "int null" : "int " + i, i -> i.toString()); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("int null")); s.set("2"); assertThat(listBox.getSelectedIndex(), is(2)); assertThat(listBox.getSelectedText(), is("int 2")); s.set(null); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("int null")); }
@Test public void listPropertyToListBoxHandlesEmptyString() { final StubListBox listBox = new StubListBox(); final ListProperty<String> values = listProperty("values", list("", "a", "b")); binder.bind(s).to(listBox, values); listBox.select("b"); assertThat(s.get(), is("b")); listBox.select(""); assertThat(s.get(), is("")); }