PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/example/src/main/java/example/client/ExampleEntryPoint.java

https://code.google.com/p/gwtrpccommlayer/
Java | 196 lines | 122 code | 26 blank | 48 comment | 0 complexity | 446bd570a4ceba4b88bc082f7be44410 MD5 | raw file
  1. /*
  2. * Copyright 2010 Jeff McHugh (Segue Development LLC)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. */
  14. package example.client;
  15. import example.shared.UserFormData;
  16. import com.google.gwt.core.client.EntryPoint;
  17. import com.google.gwt.core.client.GWT;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.event.dom.client.KeyUpEvent;
  21. import com.google.gwt.event.dom.client.KeyUpHandler;
  22. import com.google.gwt.user.client.Window;
  23. import com.google.gwt.user.client.rpc.AsyncCallback;
  24. import com.google.gwt.user.client.ui.Button;
  25. import com.google.gwt.user.client.ui.DialogBox;
  26. import com.google.gwt.user.client.ui.HTML;
  27. import com.google.gwt.user.client.ui.Label;
  28. import com.google.gwt.user.client.ui.RootPanel;
  29. import com.google.gwt.user.client.ui.TextBox;
  30. import com.google.gwt.user.client.ui.VerticalPanel;
  31. /**
  32. * Entry point classes define <code>onModuleLoad()</code>.
  33. */
  34. public class ExampleEntryPoint implements EntryPoint
  35. {
  36. /**
  37. * The message displayed to the user when the server cannot be reached or
  38. * returns an error.
  39. */
  40. private static final String SERVER_ERROR = "An error occurred while " + "attempting to contact the server. Please check your network " + "connection and try again.";
  41. /**
  42. * Create a remote service proxy to talk to the server-side Greeting service.
  43. */
  44. private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
  45. /**
  46. * This is the entry point method.
  47. */
  48. public void onModuleLoad()
  49. {
  50. final Button sendButton = new Button("Send");
  51. final TextBox acctField = new TextBox();
  52. acctField.setText("nobody@gmail.com");
  53. final TextBox nameField = new TextBox();
  54. nameField.setText("nobody");
  55. final TextBox ageField = new TextBox();
  56. ageField.setText("26");
  57. final Label errorLabel = new Label();
  58. // We can add style names to widgets
  59. sendButton.addStyleName("sendButton");
  60. // Add the nameField and sendButton to the RootPanel
  61. // Use RootPanel.get() to get the entire body element
  62. RootPanel.get("nameFieldContainer").add(nameField);
  63. RootPanel.get("accountIdFieldContainer").add(acctField);
  64. RootPanel.get("ageFieldContainer").add(ageField);
  65. RootPanel.get("sendButtonContainer").add(sendButton);
  66. RootPanel.get("errorLabelContainer").add(errorLabel);
  67. // Focus the cursor on the name field when the app loads
  68. nameField.setFocus(true);
  69. nameField.selectAll();
  70. // Create the popup dialog box
  71. final DialogBox dialogBox = new DialogBox();
  72. dialogBox.setText("Remote Procedure Call");
  73. dialogBox.setAnimationEnabled(true);
  74. final Button closeButton = new Button("Close");
  75. // We can set the id of a widget by accessing its Element
  76. closeButton.getElement().setId("closeButton");
  77. final Label textToServerLabel = new Label();
  78. final HTML serverResponseLabel = new HTML();
  79. VerticalPanel dialogVPanel = new VerticalPanel();
  80. dialogVPanel.addStyleName("dialogVPanel");
  81. dialogVPanel.add(new HTML("<b>Sending data to the server...</b>"));
  82. //dialogVPanel.add(textToServerLabel);
  83. dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
  84. dialogVPanel.add(serverResponseLabel);
  85. dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
  86. dialogVPanel.add(closeButton);
  87. dialogBox.setWidget(dialogVPanel);
  88. // Add a handler to close the DialogBox
  89. closeButton.addClickHandler(new ClickHandler()
  90. {
  91. public void onClick(ClickEvent event)
  92. {
  93. dialogBox.hide();
  94. sendButton.setEnabled(true);
  95. sendButton.setFocus(true);
  96. }
  97. });
  98. // Create a handler for the sendButton and nameField
  99. class MyHandler implements ClickHandler, KeyUpHandler
  100. {
  101. /**
  102. * Fired when the user clicks on the sendButton.
  103. */
  104. public void onClick(ClickEvent event)
  105. {
  106. sendNameToServer();
  107. }
  108. /**
  109. * Fired when the user types in the nameField.
  110. */
  111. public void onKeyUp(KeyUpEvent event)
  112. {
  113. }
  114. /**
  115. * Send the name from the nameField to the server and wait for a response.
  116. */
  117. private void sendNameToServer()
  118. {
  119. // First, we validate the input.
  120. errorLabel.setText("");
  121. String name = nameField.getText();
  122. String account = acctField.getText();
  123. String age = ageField.getText();
  124. // Then, we send the input to the server.
  125. sendButton.setEnabled(false);
  126. textToServerLabel.setText("sending...");
  127. serverResponseLabel.setText("");
  128. Integer theAge = null;
  129. try
  130. {
  131. theAge = new Integer(age);
  132. }
  133. catch(Exception e)
  134. {
  135. Window.alert("Age does not appear to be a number");
  136. return;
  137. }
  138. UserFormData data = new UserFormData();
  139. data.setAccountId(account);
  140. data.setAge(theAge);
  141. data.setName(name);
  142. greetingService.addUserFormData(data, new AsyncCallback<String>()
  143. {
  144. public void onFailure(Throwable caught)
  145. {
  146. caught.printStackTrace();
  147. // Show the RPC error message to the user
  148. dialogBox.setText("Remote Procedure Call - Failure");
  149. serverResponseLabel.addStyleName("serverResponseLabelError");
  150. serverResponseLabel.setHTML(SERVER_ERROR);
  151. dialogBox.center();
  152. closeButton.setFocus(true);
  153. }
  154. public void onSuccess(String resp)
  155. {
  156. dialogBox.setText("Remote Procedure Call");
  157. serverResponseLabel.removeStyleName("serverResponseLabelError");
  158. serverResponseLabel.setHTML(resp);
  159. dialogBox.center();
  160. closeButton.setFocus(true);
  161. }
  162. });
  163. }
  164. }
  165. // Add a handler to send the name to the server
  166. MyHandler handler = new MyHandler();
  167. sendButton.addClickHandler(handler);
  168. nameField.addKeyUpHandler(handler);
  169. }
  170. }