book.findContactsByName(query, new AsyncCallback<ContactList>() {
public void onSuccess(ContactList response) {
Iterator contactIt = response.getContacts().iterator();
Collection suggestions = new ArrayList();
while (contactIt.hasNext()) {
final Contact contact = (Contact) contactIt.next();
//add the suggestion.
suggestions.add(new Suggestion() {
public String getDisplayString() {
return contact.getName();
}
public String getReplacementString() {
return contact.getName();
}
});
}
contactGrid.clear(); //clear the grid.
callback.onSuggestionsReady(request, new Response(suggestions));
}
public void onFailure(Throwable throwable) {
//do nothing if an error occurs while asking for suggestions
}
});
}
};
//the suggest box (instantiated with our oracle).
final SuggestBox suggestBox = new SuggestBox(oracle);
//The panel that will hold the suggest box and the "find" button.
HorizontalPanel findForm = new HorizontalPanel();
findForm.add(suggestBox);
//the "find" button.
Button findButton = new Button("find");
findButton.addClickListener(new ClickListener() {
public void onClick(Widget widget) {
//when "find" is clicked, make the query and populate the grid.
String text = suggestBox.getText();
book.findContactsByName(text, new AsyncCallback<ContactList>() {
public void onSuccess(ContactList response) {
contactGrid.resize(6 * response.getContacts().size(), 2);
Iterator contactIt = response.getContacts().iterator();
int i = 0;
while (contactIt.hasNext()) {
Contact contact = (Contact) contactIt.next();
contactGrid.setWidget(++i, 0, new Label("Name:"));
contactGrid.setWidget(i, 1, new Label(contact.getName()));
contactGrid.setWidget(++i, 0, new Label("Phone:"));
contactGrid.setWidget(i, 1, new Label(contact.getPhone()));
contactGrid.setWidget(++i, 0, new Label("Address:"));
contactGrid.setWidget(i, 1, new Label(contact.getAddress1()));
contactGrid.setWidget(++i, 0, new Label("City:"));
contactGrid.setWidget(i, 1, new Label(contact.getCity()));
contactGrid.setWidget(++i, 0, new Label("Type:"));
contactGrid.setWidget(i, 1, new Label(String.valueOf(contact.getContactType())));
contactGrid.setWidget(++i, 0, new HTML("<hr/>"));
contactGrid.setWidget(i, 1, new HTML("<hr/>"));
}
}
public void onFailure(Throwable throwable) {
//if an error while doing a "find," display it in the grid.
contactGrid.resize(1, 1);
contactGrid.setWidget(0, 0, new Label("ERROR: " + throwable.getMessage()));
}
});
}
});
//add the find button.
findForm.add(findButton);
byNamePanel.add(new Label("This demonstrates GWT-RPC"));
//add the find form to the panel.
byNamePanel.add(findForm);
//add the display grid to the panel.
byNamePanel.add(contactGrid);
//create the tab panel.
TabPanel panel = new TabPanel();
//add the find by name panel to the tab panel.
panel.add(byNamePanel, " <a href=\"#byname\">by name (RPC)</a> ", true);
final VerticalPanel byIdPanel = new VerticalPanel();
byIdPanel.add(new Label("This demonstrates using GWT to access a JSON-REST endpoint and using GWT JSON overlays"));
HorizontalPanel findByIdForm = new HorizontalPanel();
findByIdForm.add(new Label("id:"));
final TextBox idBox = new TextBox();
idBox.setText("1");
findByIdForm.add(idBox);
Button findByIdButton = new Button("find");
findByIdForm.add(findByIdButton);
findByIdButton.addClickListener(new ClickListener() {
public void onClick(Widget widget) {
final RequestBuilder restRequestBuilder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + "../json/contact/" + idBox.getText());
try {
restRequestBuilder.sendRequest(null, new RequestCallback() {
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
net.java.ws.addressbook.client.json.domain.Contact contact = net.java.ws.addressbook.client.json.domain.Contact.fromJson(response.getText());
byIdPanel.add(new Label("Found " + contact.getContactType().toString() + ": " + contact.getName() + ", updated: " + new java.util.Date(contact.getUpdated()) + " (" + contact.getUpdated() + ")."));
}
else {
byIdPanel.add(new Label("ERROR: " + response.getStatusText() + ": REQUEST URL: " + restRequestBuilder.getUrl()));
}
}