Cleanup & automatic refresh
This commit is contained in:
parent
0099fcbe51
commit
e233a26104
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
/test-classes/
|
||||
/gwt-unitCache/
|
||||
/war/fotostream/
|
||||
/war/img/
|
||||
/war/WEB-INF/deploy/
|
||||
.gwt-log
|
@ -1,51 +1,20 @@
|
||||
package net.molez.mandlm.fotostream.client;
|
||||
|
||||
import net.molez.mandlm.fotostream.shared.FieldVerifier;
|
||||
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.event.dom.client.LoadEvent;
|
||||
import com.google.gwt.event.dom.client.LoadHandler;
|
||||
import com.google.gwt.user.client.Timer;
|
||||
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.Image;
|
||||
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 FotoStream implements EntryPoint {
|
||||
/**
|
||||
* The message displayed to the user when the server cannot be reached or
|
||||
* returns an error.
|
||||
*/
|
||||
private static final String SERVER_ERROR = "An error occurred while "
|
||||
+ "attempting to contact the server. Please check your network " + "connection and try again.";
|
||||
|
||||
/**
|
||||
* Create a remote service proxy to talk to the server-side Greeting service.
|
||||
*/
|
||||
private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
|
||||
public class FotoStream implements EntryPoint
|
||||
{
|
||||
private final CurrentImageURLServiceAsync imageURLService = GWT.create(CurrentImageURLService.class);
|
||||
|
||||
/**
|
||||
* This is the entry point method.
|
||||
*/
|
||||
public void onModuleLoad() {
|
||||
final Button sendButton = new Button("Send");
|
||||
final TextBox nameField = new TextBox();
|
||||
nameField.setText("GWT User");
|
||||
final Label errorLabel = new Label();
|
||||
|
||||
final Image image = new Image();
|
||||
image.setVisible(false);
|
||||
image.addLoadHandler(new LoadHandler()
|
||||
@ -58,125 +27,37 @@ public class FotoStream implements EntryPoint {
|
||||
}
|
||||
});
|
||||
|
||||
imageURLService.getCurrentImageURL(new AsyncCallback<String>()
|
||||
Timer imageReloadTimer = new Timer()
|
||||
{
|
||||
@Override
|
||||
public void onFailure(Throwable caught)
|
||||
public void run()
|
||||
{
|
||||
DialogBox errorMsg = new DialogBox();
|
||||
imageURLService.getCurrentImageURL(new AsyncCallback<String>()
|
||||
{
|
||||
@Override
|
||||
public void onFailure(Throwable caught)
|
||||
{
|
||||
DialogBox errorMsg = new DialogBox();
|
||||
|
||||
errorMsg.setTitle("Error loading current immage");
|
||||
errorMsg.setHTML(caught.getMessage());
|
||||
errorMsg.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(String result)
|
||||
{
|
||||
image.setUrl(result);
|
||||
}
|
||||
});
|
||||
|
||||
// We can add style names to widgets
|
||||
sendButton.addStyleName("sendButton");
|
||||
|
||||
// Add the nameField and sendButton to the RootPanel
|
||||
// Use RootPanel.get() to get the entire body element
|
||||
RootPanel.get("nameFieldContainer").add(nameField);
|
||||
RootPanel.get("sendButtonContainer").add(sendButton);
|
||||
RootPanel.get("errorLabelContainer").add(errorLabel);
|
||||
RootPanel.get("imageContainer").add(image);
|
||||
|
||||
// Focus the cursor on the name field when the app loads
|
||||
nameField.setFocus(true);
|
||||
nameField.selectAll();
|
||||
|
||||
// Create the popup dialog box
|
||||
final DialogBox dialogBox = new DialogBox();
|
||||
dialogBox.setText("Remote Procedure Call");
|
||||
dialogBox.setAnimationEnabled(true);
|
||||
final Button closeButton = new Button("Close");
|
||||
// We can set the id of a widget by accessing its Element
|
||||
closeButton.getElement().setId("closeButton");
|
||||
final Label textToServerLabel = new Label();
|
||||
final HTML serverResponseLabel = new HTML();
|
||||
VerticalPanel dialogVPanel = new VerticalPanel();
|
||||
dialogVPanel.addStyleName("dialogVPanel");
|
||||
dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
|
||||
dialogVPanel.add(textToServerLabel);
|
||||
dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
|
||||
dialogVPanel.add(serverResponseLabel);
|
||||
dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
|
||||
dialogVPanel.add(closeButton);
|
||||
dialogBox.setWidget(dialogVPanel);
|
||||
|
||||
// Add a handler to close the DialogBox
|
||||
closeButton.addClickHandler(new ClickHandler() {
|
||||
public void onClick(ClickEvent event) {
|
||||
dialogBox.hide();
|
||||
sendButton.setEnabled(true);
|
||||
sendButton.setFocus(true);
|
||||
}
|
||||
});
|
||||
|
||||
// Create a handler for the sendButton and nameField
|
||||
class MyHandler implements ClickHandler, KeyUpHandler {
|
||||
/**
|
||||
* Fired when the user clicks on the sendButton.
|
||||
*/
|
||||
public void onClick(ClickEvent event) {
|
||||
sendNameToServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when the user types in the nameField.
|
||||
*/
|
||||
public void onKeyUp(KeyUpEvent event) {
|
||||
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
|
||||
sendNameToServer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the name from the nameField to the server and wait for a response.
|
||||
*/
|
||||
private void sendNameToServer() {
|
||||
// First, we validate the input.
|
||||
errorLabel.setText("");
|
||||
String textToServer = nameField.getText();
|
||||
if (!FieldVerifier.isValidName(textToServer)) {
|
||||
errorLabel.setText("Please enter at least four characters");
|
||||
return;
|
||||
}
|
||||
|
||||
// Then, we send the input to the server.
|
||||
sendButton.setEnabled(false);
|
||||
textToServerLabel.setText(textToServer);
|
||||
serverResponseLabel.setText("");
|
||||
greetingService.greetServer(textToServer, new AsyncCallback<String>() {
|
||||
public void onFailure(Throwable caught) {
|
||||
// Show the RPC error message to the user
|
||||
dialogBox.setText("Remote Procedure Call - Failure");
|
||||
serverResponseLabel.addStyleName("serverResponseLabelError");
|
||||
serverResponseLabel.setHTML(SERVER_ERROR);
|
||||
dialogBox.center();
|
||||
closeButton.setFocus(true);
|
||||
errorMsg.setTitle("Error loading image");
|
||||
errorMsg.setHTML(caught.getMessage());
|
||||
errorMsg.show();
|
||||
}
|
||||
|
||||
public void onSuccess(String result) {
|
||||
dialogBox.setText("Remote Procedure Call");
|
||||
serverResponseLabel.removeStyleName("serverResponseLabelError");
|
||||
serverResponseLabel.setHTML(result);
|
||||
dialogBox.center();
|
||||
closeButton.setFocus(true);
|
||||
@Override
|
||||
public void onSuccess(String result)
|
||||
{
|
||||
if (result != image.getUrl())
|
||||
{
|
||||
image.setUrl(result);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Add a handler to send the name to the server
|
||||
MyHandler handler = new MyHandler();
|
||||
sendButton.addClickHandler(handler);
|
||||
nameField.addKeyUpHandler(handler);
|
||||
imageReloadTimer.scheduleRepeating(2000);
|
||||
|
||||
RootPanel.get("imageContainer").add(image);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
package net.molez.mandlm.fotostream.client;
|
||||
|
||||
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 GreetingService extends RemoteService {
|
||||
String greetServer(String name) throws IllegalArgumentException;
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package net.molez.mandlm.fotostream.client;
|
||||
|
||||
import com.google.gwt.user.client.rpc.AsyncCallback;
|
||||
|
||||
/**
|
||||
* The async counterpart of <code>GreetingService</code>.
|
||||
*/
|
||||
public interface GreetingServiceAsync {
|
||||
void greetServer(String input, AsyncCallback<String> callback) throws IllegalArgumentException;
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package net.molez.mandlm.fotostream.server;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
|
||||
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
||||
@ -13,11 +12,10 @@ public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements
|
||||
{
|
||||
public String getCurrentImageURL()
|
||||
{
|
||||
List<String> fileList = new ArrayList<String>();
|
||||
File imageFolder = new File("img/");
|
||||
|
||||
fileList.add("img/dummy_1.jpg");
|
||||
fileList.add("img/dummy_2.jpg");
|
||||
File[] fileList = imageFolder.listFiles();
|
||||
|
||||
return fileList.get(new Random().nextInt(fileList.size()));
|
||||
return fileList[new Random().nextInt(fileList.length)].getPath();
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +0,0 @@
|
||||
package net.molez.mandlm.fotostream.server;
|
||||
|
||||
import net.molez.mandlm.fotostream.client.GreetingService;
|
||||
import net.molez.mandlm.fotostream.shared.FieldVerifier;
|
||||
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
||||
|
||||
/**
|
||||
* The server-side implementation of the RPC service.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {
|
||||
|
||||
public String greetServer(String input) throws IllegalArgumentException {
|
||||
// Verify that the input is valid.
|
||||
if (!FieldVerifier.isValidName(input)) {
|
||||
// If the input is not valid, throw an IllegalArgumentException back to
|
||||
// the client.
|
||||
throw new IllegalArgumentException("Name must be at least 4 characters long");
|
||||
}
|
||||
|
||||
String serverInfo = getServletContext().getServerInfo();
|
||||
String userAgent = getThreadLocalRequest().getHeader("User-Agent");
|
||||
|
||||
// Escape data from the client to avoid cross-site script vulnerabilities.
|
||||
input = escapeHtml(input);
|
||||
userAgent = escapeHtml(userAgent);
|
||||
|
||||
return "Hello, " + input + "!<br><br>I am running " + serverInfo + ".<br><br>It looks like you are using:<br>"
|
||||
+ userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape an html string. Escaping data received from the client helps to
|
||||
* prevent cross-site script vulnerabilities.
|
||||
*
|
||||
* @param html the html string to escape
|
||||
* @return the escaped string
|
||||
*/
|
||||
private String escapeHtml(String html) {
|
||||
if (html == null) {
|
||||
return null;
|
||||
}
|
||||
return html.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package net.molez.mandlm.fotostream.shared;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* FieldVerifier validates that the name the user enters is valid.
|
||||
* </p>
|
||||
* <p>
|
||||
* This class is in the <code>shared</code> package because we use it in both
|
||||
* the client code and on the server. On the client, we verify that the name is
|
||||
* valid before sending an RPC request so the user doesn't have to wait for a
|
||||
* network round trip to get feedback. On the server, we verify that the name is
|
||||
* correct to ensure that the input is correct regardless of where the RPC
|
||||
* originates.
|
||||
* </p>
|
||||
* <p>
|
||||
* When creating a class that is used on both the client and the server, be sure
|
||||
* that all code is translatable and does not use native JavaScript. Code that
|
||||
* is not translatable (such as code that interacts with a database or the file
|
||||
* system) cannot be compiled into client-side JavaScript. Code that uses native
|
||||
* JavaScript (such as Widgets) cannot be run on the server.
|
||||
* </p>
|
||||
*/
|
||||
public class FieldVerifier {
|
||||
|
||||
/**
|
||||
* Verifies that the specified name is valid for our service.
|
||||
*
|
||||
* In this example, we only require that the name is at least four
|
||||
* characters. In your application, you can use more complex checks to ensure
|
||||
* that usernames, passwords, email addresses, URLs, and other fields have the
|
||||
* proper syntax.
|
||||
*
|
||||
* @param name the name to validate
|
||||
* @return true if valid, false if invalid
|
||||
*/
|
||||
public static boolean isValidName(String name) {
|
||||
if (name == null) {
|
||||
return false;
|
||||
}
|
||||
return name.length() > 3;
|
||||
}
|
||||
}
|
@ -1,34 +1,8 @@
|
||||
/** Add css rules here for your application. */
|
||||
|
||||
|
||||
/** Example rules used by the template application (remove for your app) */
|
||||
h1 {
|
||||
font-size: 2em;
|
||||
font-weight: bold;
|
||||
color: #777777;
|
||||
margin: 40px 0px 70px;
|
||||
text-align: center;
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
.sendButton {
|
||||
display: block;
|
||||
font-size: 16pt;
|
||||
}
|
||||
|
||||
/** Most GWT widgets already have a style name defined */
|
||||
.gwt-DialogBox {
|
||||
width: 400px;
|
||||
}
|
||||
|
||||
.dialogVPanel {
|
||||
margin: 5px;
|
||||
}
|
||||
|
||||
.serverResponseLabelError {
|
||||
color: red;
|
||||
}
|
||||
|
||||
/** Set ids using widget.getElement().setId("idOfElement") */
|
||||
#closeButton {
|
||||
margin: 15px 6px 6px;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
<!-- -->
|
||||
<!-- Any title is fine -->
|
||||
<!-- -->
|
||||
<title>Web Application Starter Project</title>
|
||||
<title>FotoStream</title>
|
||||
|
||||
<!-- -->
|
||||
<!-- This script loads your compiled module. -->
|
||||
@ -32,10 +32,6 @@
|
||||
<!-- to create a completely dynamic UI. -->
|
||||
<!-- -->
|
||||
<body>
|
||||
|
||||
<!-- OPTIONAL: include this if you want history support -->
|
||||
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
|
||||
|
||||
<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
|
||||
<noscript>
|
||||
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
|
||||
@ -44,22 +40,6 @@
|
||||
</div>
|
||||
</noscript>
|
||||
|
||||
<h1>Web Application Starter Project</h1>
|
||||
|
||||
<table align="center">
|
||||
<tr>
|
||||
<td colspan="2" style="font-weight:bold;">Please enter your name:</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td id="nameFieldContainer"></td>
|
||||
<td id="sendButtonContainer"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" style="color:red;" id="errorLabelContainer"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" id="imageContainer"></td>
|
||||
</tr>
|
||||
</table>
|
||||
<div id="imageContainer"></div>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -6,15 +6,6 @@
|
||||
xmlns="http://java.sun.com/xml/ns/javaee">
|
||||
|
||||
<!-- Servlets -->
|
||||
<servlet>
|
||||
<servlet-name>greetServlet</servlet-name>
|
||||
<servlet-class>net.molez.mandlm.fotostream.server.GreetingServiceImpl</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>greetServlet</servlet-name>
|
||||
<url-pattern>/fotostream/greet</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>currentImageURLServlet</servlet-name>
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 733 KiB |
Binary file not shown.
Before Width: | Height: | Size: 1.0 MiB |
Reference in New Issue
Block a user