comparison flys-client/src/main/java/de/intevation/flys/client/client/FLYS.java @ 0:4e8be5e7855f

Start of a GWT based client for FLYS-3.0 flys-client/trunk@1305 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 08 Feb 2011 10:29:49 +0000
parents
children 0e22a19852e7
comparison
equal deleted inserted replaced
-1:000000000000 0:4e8be5e7855f
1 package de.intevation.flys.client.client;
2
3 import de.intevation.flys.client.shared.FieldVerifier;
4 import com.google.gwt.core.client.EntryPoint;
5 import com.google.gwt.core.client.GWT;
6 import com.google.gwt.event.dom.client.ClickEvent;
7 import com.google.gwt.event.dom.client.ClickHandler;
8 import com.google.gwt.event.dom.client.KeyCodes;
9 import com.google.gwt.event.dom.client.KeyUpEvent;
10 import com.google.gwt.event.dom.client.KeyUpHandler;
11 import com.google.gwt.user.client.rpc.AsyncCallback;
12 import com.google.gwt.user.client.ui.Button;
13 import com.google.gwt.user.client.ui.DialogBox;
14 import com.google.gwt.user.client.ui.HTML;
15 import com.google.gwt.user.client.ui.Label;
16 import com.google.gwt.user.client.ui.RootPanel;
17 import com.google.gwt.user.client.ui.TextBox;
18 import com.google.gwt.user.client.ui.VerticalPanel;
19
20 /**
21 * Entry point classes define <code>onModuleLoad()</code>.
22 */
23 public class FLYS implements EntryPoint {
24 /**
25 * The message displayed to the user when the server cannot be reached or
26 * returns an error.
27 */
28 private static final String SERVER_ERROR = "An error occurred while "
29 + "attempting to contact the server. Please check your network "
30 + "connection and try again.";
31
32 /**
33 * Create a remote service proxy to talk to the server-side Greeting service.
34 */
35 private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
36
37 /**
38 * This is the entry point method.
39 */
40 public void onModuleLoad() {
41 final Button sendButton = new Button("Send");
42 final TextBox nameField = new TextBox();
43 nameField.setText("GWT User");
44 final Label errorLabel = new Label();
45
46 // We can add style names to widgets
47 sendButton.addStyleName("sendButton");
48
49 // Add the nameField and sendButton to the RootPanel
50 // Use RootPanel.get() to get the entire body element
51 RootPanel.get("nameFieldContainer").add(nameField);
52 RootPanel.get("sendButtonContainer").add(sendButton);
53 RootPanel.get("errorLabelContainer").add(errorLabel);
54
55 // Focus the cursor on the name field when the app loads
56 nameField.setFocus(true);
57 nameField.selectAll();
58
59 // Create the popup dialog box
60 final DialogBox dialogBox = new DialogBox();
61 dialogBox.setText("Remote Procedure Call");
62 dialogBox.setAnimationEnabled(true);
63 final Button closeButton = new Button("Close");
64 // We can set the id of a widget by accessing its Element
65 closeButton.getElement().setId("closeButton");
66 final Label textToServerLabel = new Label();
67 final HTML serverResponseLabel = new HTML();
68 VerticalPanel dialogVPanel = new VerticalPanel();
69 dialogVPanel.addStyleName("dialogVPanel");
70 dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
71 dialogVPanel.add(textToServerLabel);
72 dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
73 dialogVPanel.add(serverResponseLabel);
74 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
75 dialogVPanel.add(closeButton);
76 dialogBox.setWidget(dialogVPanel);
77
78 // Add a handler to close the DialogBox
79 closeButton.addClickHandler(new ClickHandler() {
80 public void onClick(ClickEvent event) {
81 dialogBox.hide();
82 sendButton.setEnabled(true);
83 sendButton.setFocus(true);
84 }
85 });
86
87 // Create a handler for the sendButton and nameField
88 class MyHandler implements ClickHandler, KeyUpHandler {
89 /**
90 * Fired when the user clicks on the sendButton.
91 */
92 public void onClick(ClickEvent event) {
93 sendNameToServer();
94 }
95
96 /**
97 * Fired when the user types in the nameField.
98 */
99 public void onKeyUp(KeyUpEvent event) {
100 if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
101 sendNameToServer();
102 }
103 }
104
105 /**
106 * Send the name from the nameField to the server and wait for a response.
107 */
108 private void sendNameToServer() {
109 // First, we validate the input.
110 errorLabel.setText("");
111 String textToServer = nameField.getText();
112 if (!FieldVerifier.isValidName(textToServer)) {
113 errorLabel.setText("Please enter at least four characters");
114 return;
115 }
116
117 // Then, we send the input to the server.
118 sendButton.setEnabled(false);
119 textToServerLabel.setText(textToServer);
120 serverResponseLabel.setText("");
121 greetingService.greetServer(textToServer, new AsyncCallback<String>() {
122 public void onFailure(Throwable caught) {
123 // Show the RPC error message to the user
124 dialogBox.setText("Remote Procedure Call - Failure");
125 serverResponseLabel.addStyleName("serverResponseLabelError");
126 serverResponseLabel.setHTML(SERVER_ERROR);
127 dialogBox.center();
128 closeButton.setFocus(true);
129 }
130
131 public void onSuccess(String result) {
132 dialogBox.setText("Remote Procedure Call");
133 serverResponseLabel.removeStyleName("serverResponseLabelError");
134 serverResponseLabel.setHTML(result);
135 dialogBox.center();
136 closeButton.setFocus(true);
137 }
138 });
139 }
140 }
141
142 // Add a handler to send the name to the server
143 MyHandler handler = new MyHandler();
144 sendButton.addClickHandler(handler);
145 nameField.addKeyUpHandler(handler);
146 }
147 }

http://dive4elements.wald.intevation.org