[jboss-cvs] jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client ...

Shane Bryzak sbryzak at redhat.com
Sun Apr 15 20:01:25 EDT 2007


  User: sbryzak2
  Date: 07/04/15 20:01:25

  Added:       examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client      
                        AskQuestionWidget.java HelloWorld.java
                        MyService.java MyServiceAsync.java
                        ValidationUtility.java package.html
  Log:
  gwt example
  
  Revision  Changes    Path
  1.1      date: 2007/04/16 00:01:25;  author: sbryzak2;  state: Exp;jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client/AskQuestionWidget.java
  
  Index: AskQuestionWidget.java
  ===================================================================
  package org.jboss.seam.example.remoting.gwt.client;
  
  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.AbsolutePanel;
  import com.google.gwt.user.client.ui.Button;
  import com.google.gwt.user.client.ui.ClickListener;
  import com.google.gwt.user.client.ui.Composite;
  import com.google.gwt.user.client.ui.Label;
  import com.google.gwt.user.client.ui.TextBox;
  import com.google.gwt.user.client.ui.Widget;
  
  /**
   * This shows how to do a a "composite" widget in GWT, as well as how to call back to the server.
   * @author Michael Neale
   */
  public class AskQuestionWidget extends Composite
  {
  
     private AbsolutePanel panel = new AbsolutePanel();
     
     public AskQuestionWidget() {
        
        Label lbl = new Label("OK, what do you want to know?");
        panel.add(lbl);
        final TextBox box = new TextBox();
        box.setText("What is the meaning of life?");
        panel.add(box);
        Button ok = new Button("Ask");
        ok.addClickListener(new ClickListener() {
  
           public void onClick(Widget w)
           {
              ValidationUtility valid = new ValidationUtility();
              if (!valid.isValid(box.getText())) {
                 Window.alert("A question has to end with a '?'");
              } else {
                 askServer(box.getText());
              }
              
           }
           
        });
        panel.add(ok);
        
        initWidget(panel);
     }
  
     /** Now lets actually go to the server, using a callback - its called Ajax for a reason ! */
     private void askServer(String text)
     {
        getService().askIt(text, new AsyncCallback() {
  
           public void onFailure(Throwable t)
           {
              Window.alert(t.getMessage());
           }
  
           public void onSuccess(Object data)
           {
              Window.alert((String) data);
           }
           
        });
        
     }
     
    
     /**
      * This gets the async service client stub. 
      */
     private MyServiceAsync getService() {
        // define the service you want to call        
        MyServiceAsync svc =
            (MyServiceAsync) GWT.create(MyService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) svc;
        
        String endpointURL = "seam/resource/gwt";
        
        endpoint.setServiceEntryPoint(endpointURL);
        return svc;
       
     }
     
  }
  
  
  
  1.1      date: 2007/04/16 00:01:25;  author: sbryzak2;  state: Exp;jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client/HelloWorld.java
  
  Index: HelloWorld.java
  ===================================================================
  package org.jboss.seam.example.remoting.gwt.client;
  
  import com.google.gwt.core.client.EntryPoint;
  import com.google.gwt.user.client.ui.RootPanel;
  
  /**
   * This is the "main" entry point, as per GWT.
   * Generally this is a lean class, you tend to use seperate widget classes after this point.
   * This is pretty much boiler plate, you can mostly ignore this.
   * 
   * Entry point classes define <code>onModuleLoad()</code>.
   */
  public class HelloWorld implements EntryPoint {
  
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
      // Assume that the host HTML has elements defined whose
      // IDs are "slot1", "slot2".  In a real app, you probably would not want
      // to hard-code IDs.  Instead, you could, for example, search for all 
      // elements with a particular CSS class and replace them with widgets.
      //
      RootPanel.get("slot1").add(new AskQuestionWidget());
  
    }
  }
  
  
  
  1.1      date: 2007/04/16 00:01:25;  author: sbryzak2;  state: Exp;jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client/MyService.java
  
  Index: MyService.java
  ===================================================================
  package org.jboss.seam.example.remoting.gwt.client;
  
  import com.google.gwt.user.client.rpc.RemoteService;
  
  /**
   * This is a GWT service, which will be implemented as a Seam component on the server
   * (see the server package). GWT uses strongly typed RPC interfaces.
   * 
   * @author Michael Neale
   */
  public interface MyService extends RemoteService
  {
        public String askIt(String question);
        
  }
  
  
  
  1.1      date: 2007/04/16 00:01:25;  author: sbryzak2;  state: Exp;jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client/MyServiceAsync.java
  
  Index: MyServiceAsync.java
  ===================================================================
  package org.jboss.seam.example.remoting.gwt.client;
  
  import com.google.gwt.user.client.rpc.AsyncCallback;
  import com.google.gwt.user.client.rpc.RemoteService;
  
  /**
   * This is the interface the client code uses. You NEVER implement this directly, 
   * GWT does this for you, and calls are marshalled through to the Sync equivalent method in MyService
   * on the server (which is a Seam component).
   * 
   * @author Michael Neale
   */
  public interface MyServiceAsync extends RemoteService 
  {
     public void askIt(String question, AsyncCallback callback);
  }
  
  
  
  1.1      date: 2007/04/16 00:01:25;  author: sbryzak2;  state: Exp;jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client/ValidationUtility.java
  
  Index: ValidationUtility.java
  ===================================================================
  package org.jboss.seam.example.remoting.gwt.client;
  
  /**
   * This simple validation utility shows how you can have the same code on the "client" 
   * as on the server (ie the server can re-use some code from the client - one of the GWT advantages).
   * 
   * @author michael
   */
  public class ValidationUtility
  {
  
     public boolean isValid(String question) {
        if ("".equals(question)) {
           return false;
        } else if (!question.trim().endsWith("?")) {
           return false;
        } else {
           return true;
        }
     }
     
  }
  
  
  
  1.1      date: 2007/04/16 00:01:25;  author: sbryzak2;  state: Exp;jboss-seam/examples/remoting/gwt/src/org/jboss/seam/example/remoting/gwt/client/package.html
  
  Index: package.html
  ===================================================================
  <body>
  This package is the client side GWT code - which can be run as java in debug mode, 
  or compiled to javascript via GWT.
  Code in this package and sub packages can be used on the server, but not vice versa. 
  To communicate with the server, the GWT RPC mechanism with Seam remoting is used. 
  </body>
  
  



More information about the jboss-cvs-commits mailing list