PageRenderTime 38ms CodeModel.GetById 1ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 1ms

/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 */
 14package example.client;
 15
 16
 17import example.shared.UserFormData;
 18
 19import com.google.gwt.core.client.EntryPoint;
 20import com.google.gwt.core.client.GWT;
 21import com.google.gwt.event.dom.client.ClickEvent;
 22import com.google.gwt.event.dom.client.ClickHandler;
 23
 24import com.google.gwt.event.dom.client.KeyUpEvent;
 25import com.google.gwt.event.dom.client.KeyUpHandler;
 26import com.google.gwt.user.client.Window;
 27import com.google.gwt.user.client.rpc.AsyncCallback;
 28import com.google.gwt.user.client.ui.Button;
 29import com.google.gwt.user.client.ui.DialogBox;
 30import com.google.gwt.user.client.ui.HTML;
 31import com.google.gwt.user.client.ui.Label;
 32import com.google.gwt.user.client.ui.RootPanel;
 33import com.google.gwt.user.client.ui.TextBox;
 34import com.google.gwt.user.client.ui.VerticalPanel;
 35
 36/**
 37 * Entry point classes define <code>onModuleLoad()</code>.
 38 */
 39public class ExampleEntryPoint implements EntryPoint
 40{
 41	/**
 42	 * The message displayed to the user when the server cannot be reached or
 43	 * returns an error.
 44	 */
 45	private static final String			SERVER_ERROR	= "An error occurred while " + "attempting to contact the server. Please check your network " + "connection and try again.";
 46
 47	/**
 48	 * Create a remote service proxy to talk to the server-side Greeting service.
 49	 */
 50	private final GreetingServiceAsync	greetingService	= GWT.create(GreetingService.class);
 51
 52	/**
 53	 * This is the entry point method.
 54	 */
 55	public void onModuleLoad()
 56	{
 57		final Button sendButton = new Button("Send");
 58
 59		final TextBox acctField = new TextBox();
 60		acctField.setText("nobody@gmail.com");
 61		
 62		final TextBox nameField = new TextBox();
 63		nameField.setText("nobody");
 64		
 65		final TextBox ageField = new TextBox();
 66		ageField.setText("26");
 67		
 68		final Label errorLabel = new Label();
 69
 70		// We can add style names to widgets
 71		sendButton.addStyleName("sendButton");
 72
 73		// Add the nameField and sendButton to the RootPanel
 74		// Use RootPanel.get() to get the entire body element
 75		RootPanel.get("nameFieldContainer").add(nameField);
 76		RootPanel.get("accountIdFieldContainer").add(acctField);
 77		RootPanel.get("ageFieldContainer").add(ageField);
 78		
 79		RootPanel.get("sendButtonContainer").add(sendButton);
 80		RootPanel.get("errorLabelContainer").add(errorLabel);
 81
 82		// Focus the cursor on the name field when the app loads
 83		nameField.setFocus(true);
 84		nameField.selectAll();
 85
 86		// Create the popup dialog box
 87		final DialogBox dialogBox = new DialogBox();
 88		dialogBox.setText("Remote Procedure Call");
 89		dialogBox.setAnimationEnabled(true);
 90		final Button closeButton = new Button("Close");
 91		// We can set the id of a widget by accessing its Element
 92		closeButton.getElement().setId("closeButton");
 93		final Label textToServerLabel = new Label();
 94		final HTML serverResponseLabel = new HTML();
 95		VerticalPanel dialogVPanel = new VerticalPanel();
 96		dialogVPanel.addStyleName("dialogVPanel");
 97		dialogVPanel.add(new HTML("<b>Sending data to the server...</b>"));
 98		//dialogVPanel.add(textToServerLabel);
 99		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
100		dialogVPanel.add(serverResponseLabel);
101		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
102		dialogVPanel.add(closeButton);
103		dialogBox.setWidget(dialogVPanel);
104
105		// Add a handler to close the DialogBox
106		closeButton.addClickHandler(new ClickHandler()
107		{
108			public void onClick(ClickEvent event)
109			{
110				dialogBox.hide();
111				sendButton.setEnabled(true);
112				sendButton.setFocus(true);
113			}
114		});
115
116		// Create a handler for the sendButton and nameField
117		class MyHandler implements ClickHandler, KeyUpHandler
118		{
119			/**
120			 * Fired when the user clicks on the sendButton.
121			 */
122			public void onClick(ClickEvent event)
123			{
124				sendNameToServer();
125			}
126
127			/**
128			 * Fired when the user types in the nameField.
129			 */
130			public void onKeyUp(KeyUpEvent event)
131			{
132			}
133
134			/**
135			 * Send the name from the nameField to the server and wait for a response.
136			 */
137			private void sendNameToServer()
138			{
139				// First, we validate the input.
140				errorLabel.setText("");
141				String name = nameField.getText();
142				String account = acctField.getText();
143				String age = ageField.getText();
144
145				// Then, we send the input to the server.
146				sendButton.setEnabled(false);
147				textToServerLabel.setText("sending...");
148				serverResponseLabel.setText("");
149				
150				Integer theAge = null;
151				try
152				{
153					theAge = new Integer(age);
154				}
155				catch(Exception e)
156				{
157					Window.alert("Age does not appear to be a number");
158					return;
159				}
160				
161				UserFormData data = new UserFormData();
162				data.setAccountId(account);
163				data.setAge(theAge);
164				data.setName(name);
165				
166				greetingService.addUserFormData(data, new AsyncCallback<String>()
167				{
168					public void onFailure(Throwable caught)
169					{
170						caught.printStackTrace();
171						// Show the RPC error message to the user
172						dialogBox.setText("Remote Procedure Call - Failure");
173						serverResponseLabel.addStyleName("serverResponseLabelError");
174						serverResponseLabel.setHTML(SERVER_ERROR);
175						dialogBox.center();
176						closeButton.setFocus(true);
177					}
178
179					public void onSuccess(String resp)
180					{
181						dialogBox.setText("Remote Procedure Call");
182						serverResponseLabel.removeStyleName("serverResponseLabelError");
183						serverResponseLabel.setHTML(resp);
184						dialogBox.center();
185						closeButton.setFocus(true);
186					}
187				});
188			}
189		}
190
191		// Add a handler to send the name to the server
192		MyHandler handler = new MyHandler();
193		sendButton.addClickHandler(handler);
194		nameField.addKeyUpHandler(handler);
195	}
196}