GWT app new branch

This commit is contained in:
mherbaghinyan
2018-06-28 00:42:55 +04:00
parent 4d800396b8
commit 5d2d79308e
11 changed files with 390 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
When updating your version of GWT, you should also update this DTD reference,
so that your app can take advantage of the latest GWT module capabilities.
-->
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.8.2//EN"
"http://gwtproject.org/doctype/2.8.2/gwt-module.dtd">
<module rename-to='google_web_toolkit'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class='com.baeldung.client.Google_web_toolkit'/>
<!-- Specify the paths for translatable code -->
<source path='client'/>
<source path='shared'/>
<!-- allow Super Dev Mode -->
<add-linker name="xsiframe"/>
</module>

View File

@@ -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 <code>onModuleLoad()</code>.
*/
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("<br><b>Server replies:</b>"));
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<String>() {
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);
}
}

View File

@@ -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 + "!<br><br> Time received: " + LocalDateTime.now();
}
}

View File

@@ -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;
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.shared;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* The async counterpart of <code>MessageService</code>.
*/
public interface MessageServiceAsync {
void sendMessage(String input, AsyncCallback<String> callback) throws IllegalArgumentException;
}