ingo@0: package de.intevation.flys.client.client;
ingo@0:
ingo@0: import de.intevation.flys.client.shared.FieldVerifier;
ingo@0: import com.google.gwt.core.client.EntryPoint;
ingo@0: import com.google.gwt.core.client.GWT;
ingo@0: import com.google.gwt.event.dom.client.ClickEvent;
ingo@0: import com.google.gwt.event.dom.client.ClickHandler;
ingo@0: import com.google.gwt.event.dom.client.KeyCodes;
ingo@0: import com.google.gwt.event.dom.client.KeyUpEvent;
ingo@0: import com.google.gwt.event.dom.client.KeyUpHandler;
ingo@0: import com.google.gwt.user.client.rpc.AsyncCallback;
ingo@0: import com.google.gwt.user.client.ui.Button;
ingo@0: import com.google.gwt.user.client.ui.DialogBox;
ingo@0: import com.google.gwt.user.client.ui.HTML;
ingo@0: import com.google.gwt.user.client.ui.Label;
ingo@0: import com.google.gwt.user.client.ui.RootPanel;
ingo@0: import com.google.gwt.user.client.ui.TextBox;
ingo@0: import com.google.gwt.user.client.ui.VerticalPanel;
ingo@0:
ingo@0: /**
ingo@0: * Entry point classes define onModuleLoad()
.
ingo@0: */
ingo@0: public class FLYS implements EntryPoint {
ingo@0: /**
ingo@0: * The message displayed to the user when the server cannot be reached or
ingo@0: * returns an error.
ingo@0: */
ingo@0: private static final String SERVER_ERROR = "An error occurred while "
ingo@0: + "attempting to contact the server. Please check your network "
ingo@0: + "connection and try again.";
ingo@0:
ingo@0: /**
ingo@0: * Create a remote service proxy to talk to the server-side Greeting service.
ingo@0: */
ingo@0: private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
ingo@0:
ingo@0: /**
ingo@0: * This is the entry point method.
ingo@0: */
ingo@0: public void onModuleLoad() {
ingo@0: final Button sendButton = new Button("Send");
ingo@0: final TextBox nameField = new TextBox();
ingo@0: nameField.setText("GWT User");
ingo@0: final Label errorLabel = new Label();
ingo@0:
ingo@0: // We can add style names to widgets
ingo@0: sendButton.addStyleName("sendButton");
ingo@0:
ingo@0: // Add the nameField and sendButton to the RootPanel
ingo@0: // Use RootPanel.get() to get the entire body element
ingo@0: RootPanel.get("nameFieldContainer").add(nameField);
ingo@0: RootPanel.get("sendButtonContainer").add(sendButton);
ingo@0: RootPanel.get("errorLabelContainer").add(errorLabel);
ingo@0:
ingo@0: // Focus the cursor on the name field when the app loads
ingo@0: nameField.setFocus(true);
ingo@0: nameField.selectAll();
ingo@0:
ingo@0: // Create the popup dialog box
ingo@0: final DialogBox dialogBox = new DialogBox();
ingo@0: dialogBox.setText("Remote Procedure Call");
ingo@0: dialogBox.setAnimationEnabled(true);
ingo@0: final Button closeButton = new Button("Close");
ingo@0: // We can set the id of a widget by accessing its Element
ingo@0: closeButton.getElement().setId("closeButton");
ingo@0: final Label textToServerLabel = new Label();
ingo@0: final HTML serverResponseLabel = new HTML();
ingo@0: VerticalPanel dialogVPanel = new VerticalPanel();
ingo@0: dialogVPanel.addStyleName("dialogVPanel");
ingo@0: dialogVPanel.add(new HTML("Sending name to the server:"));
ingo@0: dialogVPanel.add(textToServerLabel);
ingo@0: dialogVPanel.add(new HTML("
Server replies:"));
ingo@0: dialogVPanel.add(serverResponseLabel);
ingo@0: dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
ingo@0: dialogVPanel.add(closeButton);
ingo@0: dialogBox.setWidget(dialogVPanel);
ingo@0:
ingo@0: // Add a handler to close the DialogBox
ingo@0: closeButton.addClickHandler(new ClickHandler() {
ingo@0: public void onClick(ClickEvent event) {
ingo@0: dialogBox.hide();
ingo@0: sendButton.setEnabled(true);
ingo@0: sendButton.setFocus(true);
ingo@0: }
ingo@0: });
ingo@0:
ingo@0: // Create a handler for the sendButton and nameField
ingo@0: class MyHandler implements ClickHandler, KeyUpHandler {
ingo@0: /**
ingo@0: * Fired when the user clicks on the sendButton.
ingo@0: */
ingo@0: public void onClick(ClickEvent event) {
ingo@0: sendNameToServer();
ingo@0: }
ingo@0:
ingo@0: /**
ingo@0: * Fired when the user types in the nameField.
ingo@0: */
ingo@0: public void onKeyUp(KeyUpEvent event) {
ingo@0: if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
ingo@0: sendNameToServer();
ingo@0: }
ingo@0: }
ingo@0:
ingo@0: /**
ingo@0: * Send the name from the nameField to the server and wait for a response.
ingo@0: */
ingo@0: private void sendNameToServer() {
ingo@0: // First, we validate the input.
ingo@0: errorLabel.setText("");
ingo@0: String textToServer = nameField.getText();
ingo@0: if (!FieldVerifier.isValidName(textToServer)) {
ingo@0: errorLabel.setText("Please enter at least four characters");
ingo@0: return;
ingo@0: }
ingo@0:
ingo@0: // Then, we send the input to the server.
ingo@0: sendButton.setEnabled(false);
ingo@0: textToServerLabel.setText(textToServer);
ingo@0: serverResponseLabel.setText("");
ingo@0: greetingService.greetServer(textToServer, new AsyncCallback() {
ingo@0: public void onFailure(Throwable caught) {
ingo@0: // Show the RPC error message to the user
ingo@0: dialogBox.setText("Remote Procedure Call - Failure");
ingo@0: serverResponseLabel.addStyleName("serverResponseLabelError");
ingo@0: serverResponseLabel.setHTML(SERVER_ERROR);
ingo@0: dialogBox.center();
ingo@0: closeButton.setFocus(true);
ingo@0: }
ingo@0:
ingo@0: public void onSuccess(String result) {
ingo@0: dialogBox.setText("Remote Procedure Call");
ingo@0: serverResponseLabel.removeStyleName("serverResponseLabelError");
ingo@0: serverResponseLabel.setHTML(result);
ingo@0: dialogBox.center();
ingo@0: closeButton.setFocus(true);
ingo@0: }
ingo@0: });
ingo@0: }
ingo@0: }
ingo@0:
ingo@0: // Add a handler to send the name to the server
ingo@0: MyHandler handler = new MyHandler();
ingo@0: sendButton.addClickHandler(handler);
ingo@0: nameField.addKeyUpHandler(handler);
ingo@0: }
ingo@0: }