Skip to content

Commit 5d2d793

Browse files
committed
GWT app new branch
1 parent 4d80039 commit 5d2d793

11 files changed

Lines changed: 390 additions & 0 deletions

File tree

google-web-toolkit/pom.xml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<!-- POM file generated with GWT webAppCreator -->
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>google_web_toolkit</artifactId>
9+
<packaging>war</packaging>
10+
<version>1.0-SNAPSHOT</version>
11+
<name>com.baeldung.Google_web_toolkit</name>
12+
13+
<properties>
14+
15+
<!-- Setting maven.compiler.source to something different to 1.8
16+
needs that you configure the sourceLevel in gwt-maven-plugin since
17+
GWT compiler 2.8 requires 1.8 (see gwt-maven-plugin block below) -->
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
21+
<!-- Don't let your Mac use a crazy non-standard encoding -->
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
</properties>
25+
26+
<dependencyManagement>
27+
<dependencies>
28+
<!-- ensure all GWT deps use the same version (unless overridden) -->
29+
<dependency>
30+
<groupId>com.google.gwt</groupId>
31+
<artifactId>gwt</artifactId>
32+
<version>2.8.2</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.gwt</groupId>
42+
<artifactId>gwt-servlet</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.gwt</groupId>
47+
<artifactId>gwt-user</artifactId>
48+
<scope>provided</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.gwt</groupId>
52+
<artifactId>gwt-dev</artifactId>
53+
<scope>provided</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.11</version>
59+
<scope>test</scope>
60+
</dependency>
61+
</dependencies>
62+
63+
<build>
64+
<!-- Output classes directly into the webapp, so that IDEs and "mvn process-classes" update them in DevMode -->
65+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
66+
67+
<plugins>
68+
69+
<!-- GWT Maven Plugin-->
70+
<plugin>
71+
<groupId>net.ltgt.gwt.maven</groupId>
72+
<artifactId>gwt-maven-plugin</artifactId>
73+
<version>1.0-rc-8</version>
74+
<executions>
75+
<execution>
76+
<goals>
77+
<goal>compile</goal>
78+
<goal>test</goal>
79+
</goals>
80+
</execution>
81+
</executions>
82+
<configuration>
83+
<moduleName>com.baeldung.Google_web_toolkit</moduleName>
84+
<moduleShortName>Google_web_toolkit</moduleShortName>
85+
<failOnError>true</failOnError>
86+
<!-- GWT compiler 2.8 requires 1.8, hence define sourceLevel here if you use
87+
a different source language for java compilation -->
88+
<sourceLevel>1.8</sourceLevel>
89+
<!-- Compiler configuration -->
90+
<compilerArgs>
91+
<!-- Ask GWT to create the Story of Your Compile (SOYC) (gwt:compile) -->
92+
<arg>-compileReport</arg>
93+
<arg>-XcompilerMetrics</arg>
94+
</compilerArgs>
95+
<!-- DevMode configuration -->
96+
<warDir>${project.build.directory}/${project.build.finalName}</warDir>
97+
<classpathScope>compile+runtime</classpathScope>
98+
<!-- URL(s) that should be opened by DevMode (gwt:devmode). -->
99+
<startupUrls>
100+
<startupUrl>Google_web_toolkit.html</startupUrl>
101+
</startupUrls>
102+
</configuration>
103+
</plugin>
104+
105+
<!-- Skip normal test execution, we use gwt:test instead -->
106+
<plugin>
107+
<artifactId>maven-surefire-plugin</artifactId>
108+
<version>2.17</version>
109+
<configuration>
110+
<skip>true</skip>
111+
</configuration>
112+
</plugin>
113+
114+
</plugins>
115+
</build>
116+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
When updating your version of GWT, you should also update this DTD reference,
4+
so that your app can take advantage of the latest GWT module capabilities.
5+
-->
6+
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.8.2//EN"
7+
"http://gwtproject.org/doctype/2.8.2/gwt-module.dtd">
8+
<module rename-to='google_web_toolkit'>
9+
<!-- Inherit the core Web Toolkit stuff. -->
10+
<inherits name='com.google.gwt.user.User'/>
11+
12+
<!-- Inherit the default GWT style sheet. You can change -->
13+
<!-- the theme of your GWT application by uncommenting -->
14+
<!-- any one of the following lines. -->
15+
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
16+
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
17+
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
18+
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
19+
20+
<!-- Other module inherits -->
21+
22+
<!-- Specify the app entry point class. -->
23+
<entry-point class='com.baeldung.client.Google_web_toolkit'/>
24+
25+
<!-- Specify the paths for translatable code -->
26+
<source path='client'/>
27+
<source path='shared'/>
28+
29+
<!-- allow Super Dev Mode -->
30+
<add-linker name="xsiframe"/>
31+
</module>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.baeldung.client;
2+
3+
import com.baeldung.shared.MessageService;
4+
import com.baeldung.shared.MessageServiceAsync;
5+
import com.google.gwt.core.client.EntryPoint;
6+
import com.google.gwt.core.client.GWT;
7+
import com.google.gwt.event.dom.client.ClickEvent;
8+
import com.google.gwt.event.dom.client.ClickHandler;
9+
import com.google.gwt.event.dom.client.KeyCodes;
10+
import com.google.gwt.event.dom.client.KeyUpEvent;
11+
import com.google.gwt.event.dom.client.KeyUpHandler;
12+
import com.google.gwt.user.client.rpc.AsyncCallback;
13+
import com.google.gwt.user.client.ui.Button;
14+
import com.google.gwt.user.client.ui.DialogBox;
15+
import com.google.gwt.user.client.ui.HTML;
16+
import com.google.gwt.user.client.ui.Label;
17+
import com.google.gwt.user.client.ui.RootPanel;
18+
import com.google.gwt.user.client.ui.TextBox;
19+
import com.google.gwt.user.client.ui.VerticalPanel;
20+
21+
/**
22+
* Entry point classes define <code>onModuleLoad()</code>.
23+
*/
24+
public class Google_web_toolkit implements EntryPoint {
25+
private final MessageServiceAsync messageServiceAsync = GWT.create(MessageService.class);
26+
27+
public void onModuleLoad() {
28+
Button sendButton = new Button("Submit");
29+
TextBox nameField = new TextBox();
30+
nameField.setText("Hi there");
31+
32+
Label warningLabel = new Label();
33+
34+
sendButton.addStyleName("sendButton");
35+
36+
RootPanel.get("nameFieldContainer").add(nameField);
37+
RootPanel.get("sendButtonContainer").add(sendButton);
38+
RootPanel.get("errorLabelContainer").add(warningLabel);
39+
40+
Button closeButton = new Button("Thanks");
41+
closeButton.getElement().setId("closeButton");
42+
43+
Label textToServerLabel = new Label();
44+
HTML serverResponseLabel = new HTML();
45+
VerticalPanel vPanel = new VerticalPanel();
46+
vPanel.addStyleName("vPanel");
47+
vPanel.add(new HTML("Sending message to the server:"));
48+
vPanel.add(textToServerLabel);
49+
vPanel.add(new HTML("<br><b>Server replies:</b>"));
50+
vPanel.add(serverResponseLabel);
51+
vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
52+
vPanel.add(closeButton);
53+
vPanel.setVisible(false);
54+
RootPanel.get("serverResponseContainer").add(vPanel);
55+
56+
closeButton.addClickHandler(event -> {
57+
sendButton.setEnabled(true);
58+
sendButton.setFocus(true);
59+
vPanel.setVisible(false);
60+
});
61+
62+
class MyHandler implements ClickHandler, KeyUpHandler {
63+
64+
public void onClick(ClickEvent event) {
65+
sendMessageToServer();
66+
}
67+
68+
public void onKeyUp(KeyUpEvent event) {
69+
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
70+
sendMessageToServer();
71+
}
72+
}
73+
74+
private void sendMessageToServer() {
75+
76+
warningLabel.setText("");
77+
String textToServer = nameField.getText();
78+
if (textToServer == null || textToServer.isEmpty()) {
79+
warningLabel.setText("Please enter the message");
80+
return;
81+
}
82+
83+
sendButton.setEnabled(false);
84+
textToServerLabel.setText(textToServer);
85+
serverResponseLabel.setText("");
86+
messageServiceAsync.sendMessage(textToServer, new AsyncCallback<String>() {
87+
public void onFailure(Throwable caught) {
88+
serverResponseLabel.addStyleName("serverResponseLabelError");
89+
serverResponseLabel.setHTML("server error occurred");
90+
closeButton.setFocus(true);
91+
}
92+
93+
public void onSuccess(String result) {
94+
serverResponseLabel.removeStyleName("serverResponseLabelError");
95+
serverResponseLabel.setHTML(result);
96+
closeButton.setFocus(true);
97+
vPanel.setVisible(true);
98+
}
99+
});
100+
}
101+
}
102+
103+
// Add a handler to send the name to the server
104+
MyHandler handler = new MyHandler();
105+
sendButton.addClickHandler(handler);
106+
nameField.addKeyUpHandler(handler);
107+
}
108+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.server;
2+
3+
import com.baeldung.shared.MessageService;
4+
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
5+
6+
import java.time.LocalDateTime;
7+
8+
/**
9+
* The server-side implementation of the RPC service.
10+
*/
11+
@SuppressWarnings("serial")
12+
public class MessageServiceImpl extends RemoteServiceServlet implements MessageService {
13+
14+
public String sendMessage(String message) throws IllegalArgumentException {
15+
if (message == null) {
16+
throw new IllegalArgumentException("message is null");
17+
}
18+
19+
return "Hello, " + message + "!<br><br> Time received: " + LocalDateTime.now();
20+
}
21+
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.shared;
2+
3+
import com.google.gwt.user.client.rpc.RemoteService;
4+
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
5+
6+
/**
7+
* The client-side stub for the RPC service.
8+
*/
9+
@RemoteServiceRelativePath("greet")
10+
public interface MessageService extends RemoteService {
11+
String sendMessage(String message) throws IllegalArgumentException;
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.shared;
2+
3+
import com.google.gwt.user.client.rpc.AsyncCallback;
4+
5+
/**
6+
* The async counterpart of <code>MessageService</code>.
7+
*/
8+
public interface MessageServiceAsync {
9+
void sendMessage(String input, AsyncCallback<String> callback) throws IllegalArgumentException;
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** Add css rules here for your application. */
2+
h1 {
3+
font-size: 2em;
4+
font-weight: bold;
5+
color: #777777;
6+
margin: 40px 0px 70px;
7+
text-align: center;
8+
}
9+
10+
.sendButton {
11+
display: block;
12+
font-size: 16pt;
13+
}
14+
15+
/** Most GWT widgets already have a style name defined */
16+
.gwt-DialogBox {
17+
width: 400px;
18+
}
19+
20+
.vPanel {
21+
margin: 5px;
22+
}
23+
24+
.serverResponseLabelError {
25+
color: red;
26+
}
27+
28+
/** Set ids using widget.getElement().setId("idOfElement") */
29+
#closeButton {
30+
margin: 15px 6px 6px;
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!doctype html>
2+
<!-- The DOCTYPE declaration above will set the -->
3+
<!-- browser's rendering engine into -->
4+
<!-- "Standards Mode". Replacing this declaration -->
5+
<!-- with a "Quirks Mode" doctype is not supported. -->
6+
<html>
7+
<head>
8+
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
9+
10+
<link type="text/css" rel="stylesheet" href="Google_web_toolkit.css">
11+
12+
<title>Sample GWT Application</title>
13+
14+
<script type="text/javascript" language="javascript" src="google_web_toolkit/google_web_toolkit.nocache.js"></script>
15+
</head>
16+
17+
<body>
18+
<h1>Sample GWT Application</h1>
19+
<table align="center">
20+
<tr>
21+
<td colspan="2" style="font-weight:bold;">Please enter your message:</td>
22+
</tr>
23+
<tr>
24+
<td id="nameFieldContainer"></td>
25+
<td id="sendButtonContainer"></td>
26+
</tr>
27+
<tr>
28+
<td colspan="2" style="color:red;" id="errorLabelContainer"></td>
29+
</tr>
30+
<tr>
31+
<td colspan="2" style="color:red;" id="serverResponseContainer"></td>
32+
</tr>
33+
</table>
34+
</body>
35+
</html>

0 commit comments

Comments
 (0)