suggestBox.addSelectionHandler(new PSelectionHandler<PSuggestion>() {
@Override
public void onSelection(final PSelectionEvent<PSuggestion> event) {
final String msg = "Selected item : " + event.getSelectedItem().getReplacementString();
UIContext.getRootEventBus().fireEvent(new DemoBusinessEvent(msg));
}
});
final Query query = new Query();
final FindPonysCommand command = new FindPonysCommand(query);
final Result<List<Pony>> ponys = command.execute();
final List<String> datas = new ArrayList<String>();
for (final Pony pony : ponys.getData()) {
datas.add(pony.getName());
}
suggestOracle.addAll(datas);
suggestOracle.setDefaultSuggestions(datas.subList(0, 5));
panel.add(suggestBox);
panel.add(new PHTML("<br><br>"));
panel.add(new PLabel("Manipulate the suggest box:"));
final PListBox operation = new PListBox(true);
operation.addItem("Select \"Friesian horse\"", 0);
operation.addItem("Get textbox value", 1);
operation.addItem("Enable/Disable textbox", 2);
operation.addItem("Clear", 3);
operation.addItem("Add items", 4);
operation.addChangeHandler(new PChangeHandler() {
@Override
public void onChange(final PChangeEvent event) {
final Integer item = (Integer) operation.getSelectedValue();
if (item == null) return;
if (item.equals(0)) {
suggestBox.setText("Friesian horse");
} else if (item.equals(1)) {
UIContext.getRootEventBus().fireEvent(new DemoBusinessEvent("Text content: " + suggestBox.getText()));
} else if (item.equals(2)) {
suggestBox.getTextBox().setEnabled(!suggestBox.getTextBox().isEnabled());
} else if (item.equals(3)) {
final PMultiWordSuggestOracle oracle = (PMultiWordSuggestOracle) suggestBox.getSuggestOracle();
oracle.clear();