diff --git a/google-web-toolkit/pom.xml b/google-web-toolkit/pom.xml new file mode 100644 index 0000000000..d96a589e32 --- /dev/null +++ b/google-web-toolkit/pom.xml @@ -0,0 +1,116 @@ + + + + + 4.0.0 + com.baeldung + google_web_toolkit + war + 1.0-SNAPSHOT + com.baeldung.Google_web_toolkit + + + + + 1.8 + 1.8 + + + UTF-8 + UTF-8 + + + + + + + com.google.gwt + gwt + 2.8.2 + pom + import + + + + + + + com.google.gwt + gwt-servlet + runtime + + + com.google.gwt + gwt-user + provided + + + com.google.gwt + gwt-dev + provided + + + junit + junit + 4.11 + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + + + + net.ltgt.gwt.maven + gwt-maven-plugin + 1.0-rc-8 + + + + compile + test + + + + + com.baeldung.Google_web_toolkit + Google_web_toolkit + true + + 1.8 + + + + -compileReport + -XcompilerMetrics + + + ${project.build.directory}/${project.build.finalName} + compile+runtime + + + Google_web_toolkit.html + + + + + + + maven-surefire-plugin + 2.17 + + true + + + + + + diff --git a/google-web-toolkit/src/main/java/com/baeldung/Google_web_toolkit.gwt.xml b/google-web-toolkit/src/main/java/com/baeldung/Google_web_toolkit.gwt.xml new file mode 100644 index 0000000000..1d8ca819d9 --- /dev/null +++ b/google-web-toolkit/src/main/java/com/baeldung/Google_web_toolkit.gwt.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/google-web-toolkit/src/main/java/com/baeldung/client/Google_web_toolkit.java b/google-web-toolkit/src/main/java/com/baeldung/client/Google_web_toolkit.java new file mode 100644 index 0000000000..ae0ae7da38 --- /dev/null +++ b/google-web-toolkit/src/main/java/com/baeldung/client/Google_web_toolkit.java @@ -0,0 +1,108 @@ +package com.baeldung.client; + +import com.baeldung.shared.MessageService; +import com.baeldung.shared.MessageServiceAsync; +import com.google.gwt.core.client.EntryPoint; +import com.google.gwt.core.client.GWT; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.KeyCodes; +import com.google.gwt.event.dom.client.KeyUpEvent; +import com.google.gwt.event.dom.client.KeyUpHandler; +import com.google.gwt.user.client.rpc.AsyncCallback; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.DialogBox; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.RootPanel; +import com.google.gwt.user.client.ui.TextBox; +import com.google.gwt.user.client.ui.VerticalPanel; + +/** + * Entry point classes define onModuleLoad(). + */ +public class Google_web_toolkit implements EntryPoint { + private final MessageServiceAsync messageServiceAsync = GWT.create(MessageService.class); + + public void onModuleLoad() { + Button sendButton = new Button("Submit"); + TextBox nameField = new TextBox(); + nameField.setText("Hi there"); + + Label warningLabel = new Label(); + + sendButton.addStyleName("sendButton"); + + RootPanel.get("nameFieldContainer").add(nameField); + RootPanel.get("sendButtonContainer").add(sendButton); + RootPanel.get("errorLabelContainer").add(warningLabel); + + Button closeButton = new Button("Thanks"); + closeButton.getElement().setId("closeButton"); + + Label textToServerLabel = new Label(); + HTML serverResponseLabel = new HTML(); + VerticalPanel vPanel = new VerticalPanel(); + vPanel.addStyleName("vPanel"); + vPanel.add(new HTML("Sending message to the server:")); + vPanel.add(textToServerLabel); + vPanel.add(new HTML("
Server replies:")); + vPanel.add(serverResponseLabel); + vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); + vPanel.add(closeButton); + vPanel.setVisible(false); + RootPanel.get("serverResponseContainer").add(vPanel); + + closeButton.addClickHandler(event -> { + sendButton.setEnabled(true); + sendButton.setFocus(true); + vPanel.setVisible(false); + }); + + class MyHandler implements ClickHandler, KeyUpHandler { + + public void onClick(ClickEvent event) { + sendMessageToServer(); + } + + public void onKeyUp(KeyUpEvent event) { + if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { + sendMessageToServer(); + } + } + + private void sendMessageToServer() { + + warningLabel.setText(""); + String textToServer = nameField.getText(); + if (textToServer == null || textToServer.isEmpty()) { + warningLabel.setText("Please enter the message"); + return; + } + + sendButton.setEnabled(false); + textToServerLabel.setText(textToServer); + serverResponseLabel.setText(""); + messageServiceAsync.sendMessage(textToServer, new AsyncCallback() { + public void onFailure(Throwable caught) { + serverResponseLabel.addStyleName("serverResponseLabelError"); + serverResponseLabel.setHTML("server error occurred"); + closeButton.setFocus(true); + } + + public void onSuccess(String result) { + serverResponseLabel.removeStyleName("serverResponseLabelError"); + serverResponseLabel.setHTML(result); + closeButton.setFocus(true); + vPanel.setVisible(true); + } + }); + } + } + + // Add a handler to send the name to the server + MyHandler handler = new MyHandler(); + sendButton.addClickHandler(handler); + nameField.addKeyUpHandler(handler); + } +} diff --git a/google-web-toolkit/src/main/java/com/baeldung/server/MessageServiceImpl.java b/google-web-toolkit/src/main/java/com/baeldung/server/MessageServiceImpl.java new file mode 100644 index 0000000000..41e2a4725c --- /dev/null +++ b/google-web-toolkit/src/main/java/com/baeldung/server/MessageServiceImpl.java @@ -0,0 +1,22 @@ +package com.baeldung.server; + +import com.baeldung.shared.MessageService; +import com.google.gwt.user.server.rpc.RemoteServiceServlet; + +import java.time.LocalDateTime; + +/** + * The server-side implementation of the RPC service. + */ +@SuppressWarnings("serial") +public class MessageServiceImpl extends RemoteServiceServlet implements MessageService { + + public String sendMessage(String message) throws IllegalArgumentException { + if (message == null) { + throw new IllegalArgumentException("message is null"); + } + + return "Hello, " + message + "!

Time received: " + LocalDateTime.now(); + } + +} diff --git a/google-web-toolkit/src/main/java/com/baeldung/shared/MessageService.java b/google-web-toolkit/src/main/java/com/baeldung/shared/MessageService.java new file mode 100644 index 0000000000..2c74a5c00d --- /dev/null +++ b/google-web-toolkit/src/main/java/com/baeldung/shared/MessageService.java @@ -0,0 +1,12 @@ +package com.baeldung.shared; + +import com.google.gwt.user.client.rpc.RemoteService; +import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; + +/** + * The client-side stub for the RPC service. + */ +@RemoteServiceRelativePath("greet") +public interface MessageService extends RemoteService { + String sendMessage(String message) throws IllegalArgumentException; +} diff --git a/google-web-toolkit/src/main/java/com/baeldung/shared/MessageServiceAsync.java b/google-web-toolkit/src/main/java/com/baeldung/shared/MessageServiceAsync.java new file mode 100644 index 0000000000..49d4a29b7b --- /dev/null +++ b/google-web-toolkit/src/main/java/com/baeldung/shared/MessageServiceAsync.java @@ -0,0 +1,10 @@ +package com.baeldung.shared; + +import com.google.gwt.user.client.rpc.AsyncCallback; + +/** + * The async counterpart of MessageService. + */ +public interface MessageServiceAsync { + void sendMessage(String input, AsyncCallback callback) throws IllegalArgumentException; +} diff --git a/google-web-toolkit/src/main/webapp/Google_web_toolkit.css b/google-web-toolkit/src/main/webapp/Google_web_toolkit.css new file mode 100644 index 0000000000..b02f219589 --- /dev/null +++ b/google-web-toolkit/src/main/webapp/Google_web_toolkit.css @@ -0,0 +1,31 @@ +/** Add css rules here for your application. */ +h1 { + font-size: 2em; + font-weight: bold; + color: #777777; + margin: 40px 0px 70px; + text-align: center; +} + +.sendButton { + display: block; + font-size: 16pt; +} + +/** Most GWT widgets already have a style name defined */ +.gwt-DialogBox { + width: 400px; +} + +.vPanel { + margin: 5px; +} + +.serverResponseLabelError { + color: red; +} + +/** Set ids using widget.getElement().setId("idOfElement") */ +#closeButton { + margin: 15px 6px 6px; +} \ No newline at end of file diff --git a/google-web-toolkit/src/main/webapp/Google_web_toolkit.html b/google-web-toolkit/src/main/webapp/Google_web_toolkit.html new file mode 100644 index 0000000000..20bba68eff --- /dev/null +++ b/google-web-toolkit/src/main/webapp/Google_web_toolkit.html @@ -0,0 +1,35 @@ + + + + + + + + + + + + Sample GWT Application + + + + + +

Sample GWT Application

+ + + + + + + + + + + + + + +
Please enter your message:
+ + \ No newline at end of file diff --git a/google-web-toolkit/src/main/webapp/WEB-INF/web.xml b/google-web-toolkit/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..820d3d9916 --- /dev/null +++ b/google-web-toolkit/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + + + greetServlet + com.baeldung.server.MessageServiceImpl + + + + greetServlet + /google_web_toolkit/greet + + + + + Google_web_toolkit.html + + + diff --git a/google-web-toolkit/src/main/webapp/favicon.ico b/google-web-toolkit/src/main/webapp/favicon.ico new file mode 100644 index 0000000000..858a707523 Binary files /dev/null and b/google-web-toolkit/src/main/webapp/favicon.ico differ diff --git a/pom.xml b/pom.xml index 78e0fa925e..bda4a760db 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ geotools testing-modules/groovy-spock google-cloud + google_web_toolkit gson guava guava-modules/guava-18