// Focus the cursor on the name field when the app loads
nameField.setFocus(true);
nameField.selectAll();
// Create the popup dialog box
final DialogBox dialogBox = new DialogBox();
dialogBox.setText("Add new Gadget");
dialogBox.setAnimationEnabled(true);
final Button closeButton = new Button("Close");
// We can set the id of a widget by accessing its Element
closeButton.getElement().setId("closeButton");
final Label textToServerLabel = new Label();
final HTML serverResponseLabel = new HTML();
VerticalPanel dialogVPanel = new VerticalPanel();
dialogVPanel.addStyleName("dialogVPanel");
dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
dialogVPanel.add(textToServerLabel);
dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
dialogVPanel.add(serverResponseLabel);
dialogVPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
dialogVPanel.add(closeButton);
dialogBox.setWidget(dialogVPanel);
closeButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
dialogBox.hide();
sendButton.setEnabled(true);
sendButton.setFocus(true);
}
});
class MyHandler implements ClickHandler, KeyUpHandler {
/**
* Fired when the User clicks on the sendButton.
*/
@Override
public void onClick(ClickEvent event) {
sendNameToServer();
}
/**
* Fired when the User types in the nameField.
*/
@Override
public void onKeyUp(KeyUpEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
sendNameToServer();
}
}
/**
* Send the name from the nameField to the server and wait for a
* response.
*/
private void sendNameToServer() {
// First, we validate the input.
// create gadget
gadget.setName(nameField.getText());
gadget.setTitle(titleField.getText());
gadget.setHelpUrl(helpUrlField.getText());
gadget.setGadgetFileName(gadgetFileNameField.getText());
gadget.setDescription(descriptionField.getText());
if (statusField != null)
gadget.setStatus(Integer.parseInt(statusField
.getValue(statusField.getSelectedIndex())));
if (typeField != null)
gadget.setType(Integer.parseInt(typeField
.getValue(typeField.getSelectedIndex())));
gadget.setXmlSource(xmlSourceField.getText());
gadget.setHandlerClassName(handlerClassNameField.getValue());
gadget.setGadgletType(gadgletTypeField
.getValue(gadgletTypeField.getSelectedIndex()));
errorLabel.setText("");
String textToServer = gadget.getName();
if (!FieldVerifier.isValidName(textToServer)) {
errorLabel
.setText("the name must have at least four characters");
return;
}
// Then, we send the input to the server.
sendButton.setEnabled(false);
textToServerLabel.setText(textToServer);
serverResponseLabel.setText("");
asyService.saveGadget(gadget, new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
// Show the RPC error message to the User
dialogBox.setText("Gadget - Failure");
serverResponseLabel
.addStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(caught.getMessage());
dialogBox.center();
closeButton.setFocus(true);
}
@Override
public void onSuccess(String result) {
dialogBox.setText("Saving Gadget");
serverResponseLabel
.removeStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(result);
nameField.setReadOnly(true);
dialogBox.center();
closeButton.setFocus(true);
}
});
}