Violet_82 89 Posting Whiz in Training

I'd like to start from the basics if I may please. I've read a few tutorial here and there and I've just created a test application with probably the simplest RPC calls you can possibly have (basically I want to use the RPC call to call a method to print a string). I'm using Eclipse and I've created a maven project, loosely based on this tutorial and the following ones but things are not working very well (forgive me but I'm new to vaadin and still learning java). This is the structure of my project, including packages (the structure is just the one of a normal maven project):

src/main/java        
    -test_proj.rpc_1
        -MyComponent.java
        -MyUI.java
    -test_proj.rpc_1.client
        -MyComponentClientRpc.java
        -MyComponentServerRpc.java
        -MyComponentConnector.java
        -MyComponentWidget.java
src/main/resources
    -test_proj    
        -rpc_1    
            -MyAppWidgetset.gwt.xml

Right, so here is the content of the classes. MyComponent.java: implements and registers the RPC interface

package test_proj.rpc_1;
import test_proj.rpc_1.client.MyComponentServerRpc;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.TextField;
public class MyComponent extends AbstractComponent
{
    public String text = "";
    private MyComponentServerRpc rpc = new MyComponentServerRpc()
    {
        @Override
        public void setString(String myString)
        {
            System.out.println("setString() called. myString is " + myString);
        }
    };
    public MyComponent() {
        registerRpc(rpc);
        System.out.println("MyComponent called");
        final TextField name = new TextField();
        name.setCaption("another label.");
    }
}

MyComponentClientRpc.java: still empty at the moment as I'm not sure what goes in it

package test_proj.rpc_1.client;
import com.vaadin.shared.communication.ClientRpc;
public interface MyComponentClientRpc extends ClientRpc
{
}

MyComponentServerRpc.java: contains the abstract method I want the RPC call to use:

package test_proj.rpc_1.client;
import com.vaadin.shared.communication.ServerRpc;
public interface MyComponentServerRpc extends ServerRpc
{
    public void setString(String string);
}

MyComponentConnector.java: gets the implementation of the RPC interface and makes the actual call

package test_proj.rpc_1.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.communication.RpcProxy;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.ui.Connect;
@Connect(test_proj.rpc_1.MyComponent.class)
public class MyComponentConnector extends AbstractComponentConnector
{
    MyComponentServerRpc rpc = RpcProxy.create(MyComponentServerRpc.class, this);
    @Override
    protected Widget createWidget() {
        return GWT.create(MyComponentWidget.class);
    }
    public MyComponentConnector(){
        rpc.setString("My string");
    }
}

MyComponentWidget.java: contains a component

package test_proj.rpc_1.client;
import com.google.gwt.user.client.ui.Label;
public class MyComponentWidget extends Label
{
    public static final String CLASSNAME = "mycomponent";
    public MyComponentWidget() {
        setText("This is MyComponent");
        setStyleName(CLASSNAME);
    }
}

You'll notice I left the MyUI class as last, because that's the one I'm having problems with

In my java experience I've learned that to implement a class you have to create an object of that class, but here, being new to the whole framework I don't get whether I need to create an object for all the classes I've used or not. I know that the MyUI init() method kicks everything off, but what then? IN anycase, in MyUI I've created a textField and button with the associated click handler. I've also created an object of type MyComponent which of course calls the MyComponent() constructor in MyCOmponent.java (the constructor you'll remember registers the rpc call, print something to the console and creates a new textField which I haven't attached to the layout as yet.) So, what do I need to do to get the string printed, meaning how do I get the setString() method executed?

MyUI.java

package test_proj.rpc_1;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

/**
 * This UI is the application entry point. A UI may either represent a browser window
 * (or tab) or some part of a html page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@Theme("mytheme")
@Widgetset("test_proj.rpc_1.MyAppWidgetset")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();

        final TextField name = new TextField();
        name.setCaption("Type your name here:");

        MyComponent myComponent = new MyComponent();//instantiate MyComponent class


        final Button button = new Button("Open popup");
        button.addClickListener(new Button.ClickListener(){
            @Override
            public void buttonClick(ClickEvent event){
                //button.setCaption("you clicked me!");
            }
        });
        Page.getCurrent().setTitle("my_page");
        layout.addComponents(name, button);
        layout.setMargin(true);
        layout.setSpacing(true);

        setContent(layout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.