// creation / insert / update
updateUI(new RequestHandler() {
@Override
public void onRequest() {
final PListBox listBox2 = new PListBox(false, true);
listBox2.ensureDebugId("listBox2");
listBox2.addItem("Item 1", new Long(1));
listBox2.addItem("Item 2", new Long(2));
listBox2.addItem("Item 4", new Long(4));
listBox2.insertItem("Item 3", new Long(3), 2);
listBox2.addItem("Item A", new Long(5));
listBox2.addItem("Item 6", new Long(6));
listBox2.setItemText(4, "Item 5");
listBox2.addChangeHandler(eventsListener);
PRootPanel.get().add(listBox2);
register(listBox2);
}
});
final WebElement listBox2 = findElementById("listBox2");
List<WebElement> options2 = listBox2.findElements(By.tagName("option"));
final PListBox plistBox2 = get("listBox2");
Assert.assertEquals(false, plistBox2.isEmptySelection());
Assert.assertEquals(true, plistBox2.isMultipleSelect());
Assert.assertEquals(6, options2.size());
Assert.assertEquals("Item 1", options2.get(0).getText());
Assert.assertEquals("Item 2", options2.get(1).getText());
Assert.assertEquals("Item 3", options2.get(2).getText());
Assert.assertEquals("Item 4", options2.get(3).getText());
Assert.assertEquals("Item 5", options2.get(4).getText());
Assert.assertEquals("Item 6", options2.get(5).getText());
Assert.assertEquals("Item 1", plistBox2.getItem(0));
Assert.assertEquals("Item 2", plistBox2.getItem(1));
Assert.assertEquals("Item 3", plistBox2.getItem(2));
Assert.assertEquals("Item 4", plistBox2.getItem(3));
Assert.assertEquals("Item 5", plistBox2.getItem(4));
Assert.assertEquals("Item 6", plistBox2.getItem(5));
Assert.assertEquals(new Long(1), plistBox2.getValue(0));
Assert.assertEquals(new Long(2), plistBox2.getValue(1));
Assert.assertEquals(new Long(3), plistBox2.getValue(2));
Assert.assertEquals(new Long(4), plistBox2.getValue(3));
Assert.assertEquals(new Long(5), plistBox2.getValue(4));
Assert.assertEquals(new Long(6), plistBox2.getValue(5));
final Select select2 = new Select(listBox2);
select2.selectByIndex(4); // select item5
final PChangeEvent e2 = eventsListener.poll();
Assert.assertNotNull(e2);
Assert.assertEquals(4, plistBox2.getSelectedIndex());
Assert.assertEquals("Item 5", plistBox2.getSelectedItem());
Assert.assertEquals(new Long(5), plistBox2.getSelectedValue());
// remove
updateUI(new RequestHandler() {
@Override
public void onRequest() {
final PListBox listBox2 = get("listBox2");
listBox2.removeItem("Item 2");
listBox2.removeItem(2);
listBox2.removeValue(new Long(6));
}
});
options2 = listBox2.findElements(By.tagName("option"));
Assert.assertEquals(3, options2.size());
Assert.assertEquals("Item 1", options2.get(0).getText());
Assert.assertEquals("Item 3", options2.get(1).getText());
Assert.assertEquals("Item 5", options2.get(2).getText());
Assert.assertEquals(3, plistBox2.getItemCount());
Assert.assertEquals("Item 1", plistBox2.getItem(0));
Assert.assertEquals("Item 3", plistBox2.getItem(1));
Assert.assertEquals("Item 5", plistBox2.getItem(2));
Assert.assertEquals(new Long(1), plistBox2.getValue(0));
Assert.assertEquals(new Long(3), plistBox2.getValue(1));
Assert.assertEquals(new Long(5), plistBox2.getValue(2));
// server-side selection
updateUI(new RequestHandler() {
@Override
public void onRequest() {
final PListBox listBox2 = get("listBox2");
listBox2.setSelectedValue(new Long(3)); // select item3
}
});
final WebElement selectedOption2 = select2.getFirstSelectedOption();
Assert.assertEquals("Item 3", selectedOption2.getText());