Google Web Toolkit

Example

/*
 * MainEntryPoint.java
 *
 * Created on April 3, 2008, 9:05 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package edu.sc.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
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.Widget;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author jmvidal
 */
public class MainEntryPoint implements EntryPoint {

    /** Creates a new instance of MainEntryPoint */
    public MainEntryPoint() {
    }
    /** List of the actual todo items as kept on the client */
    private List todoList = new ArrayList();
    /** Table used for displaying the todo items. */
    private FlexTable table = new FlexTable();

    /**
     * The entry point method, called automatically by loading a module
     * that declares an implementing class as an entry-point
     */
    public void onModuleLoad() {

        getFromServer();

        final HorizontalPanel addPanel = new HorizontalPanel();
        final TextBox addTextBox = new TextBox();
        final Button addNewItemButton = new Button("Add");
        addPanel.add(addTextBox);
        addPanel.add(addNewItemButton);

        addNewItemButton.addClickListener(new ClickListener() {

            public void onClick(Widget w) {
                todoList.add(addTextBox.getText());
                saveToServer();
                addTextBox.setText("");
                updateTodoList();
            }
        });

        RootPanel.get().add(table);
        RootPanel.get().add(addPanel);
        updateTodoList();
    }

    /** Copies everything from todoList into table, thus updating the view.
     */
    private void updateTodoList() {
        table.clear();
        for (int i = 0; i < todoList.size(); i++) {
            Label l = new Label((String) todoList.get(i));
            table.setWidget(i, 0, l);
            table.setWidget(i, 1, makeDeleteButton(i));
        }
    }

    /** Creates a delete button. Notice the use of the 'final' keyword in the argument.
     * This final creates a closure so that the callback function (onClick) will be able to use the
     * correct value of id.
     * @param id the row that this button should remove.
     * @return
     */
    private Widget makeDeleteButton(final int id) {
        Button deleteMeButton = new Button("X");
        deleteMeButton.addClickListener(new ClickListener() {

            public void onClick(Widget w) {
                todoList.remove(id);
                saveToServer();
                updateTodoList();
            }
            });
        return deleteMeButton;
    }

    /** Save the contents of todoList to the server. */
    private void saveToServer() {
        // Create an asynchronous callback to handle the result.
        final AsyncCallback callback = new AsyncCallback() {

            public void onSuccess(Object result) {
                
            }

            public void onFailure(Throwable caught) {
                Window.alert("Failed to write to server.");
            }
        };
        getService().save(todoList, callback);
    }

    /** Retrieve todoList from server */
    private void getFromServer() {
        final AsyncCallback callback = new AsyncCallback() {

            public void onSuccess(Object result) {
                todoList = (List)result;
                updateTodoList();
            }

            public void onFailure(Throwable caught) {
                Window.alert("Failed to get from server.");
            }
        };
        getService().get(callback);
    }

    /** Get a service proxy. This bit of code automatically generated by netbeans */
    public static SaveTodoListServiceAsync getService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of
        // the interface. The cast is always safe because the generated proxy
        // implements the asynchronous interface automatically.
        SaveTodoListServiceAsync service = (SaveTodoListServiceAsync) GWT.create(SaveTodoListService.class);
        // Specify the URL at which our service implementation is running.
        // Note that the target URL must reside on the same domain and port from
        // which the host page was served.
        //
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String moduleRelativeURL = GWT.getModuleBaseURL() + "savetodolistservice";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        return service;
    }
}
/*
 * SaveTodoListService.java
 *
 * Created on April 3, 2008, 7:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.sc.client;
import com.google.gwt.user.client.rpc.RemoteService;
import java.util.List;

/**
 *
 * @author jmvidal
 */
public interface SaveTodoListService extends RemoteService{
    public void save(List l);
    public List get();
}
/*
 * SaveTodoListServiceAsync.java
 *
 * Created on April 3, 2008, 7:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.sc.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.List;


/**
 *
 * @author jmvidal
 */
public interface SaveTodoListServiceAsync {
    public void save(List l, AsyncCallback callback);
    public void get(AsyncCallback callback);
}
/*
 * SaveTodoListServiceImpl.java
 *
 * Created on April 3, 2008, 7:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.sc.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import edu.sc.client.SaveTodoListService;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author jmvidal
 */
public class SaveTodoListServiceImpl extends RemoteServiceServlet implements
        SaveTodoListService {
    
    public static final String todoKey = "todo";
    
    public void save(List l) {
        // Do something interesting with 's' here on the server.
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession();
        session.setAttribute(todoKey, l);
    }
    
    public List get() {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession();
        if (session.getAttribute(todoKey) == null) {
            List todoList = new ArrayList();
            todoList.add("Hi there.");
            return todoList;
        } else {
            return (List) session.getAttribute(todoKey);
        }
    }
}

José M. Vidal .

4 of 4