Client-server connector and onStateChanged

Hi everyone, i’m in the condition of
https://vaadin.com/book/vaadin7/-/page/gwt.connector.html
since i have my gwt widget and implemented the server side and all the rest but the onStateChanged method isn’t ever fired…when it should be raised? what can I check? the rest seems working.

Did you already create a
shared state
for your component. the onStatechagned changed method is normally called when this state changes.
“State changes made on the server-side are communicated transparently to the client-side. When a state change occurs, the onStateChanged() method in the connector is called.”

emmh nop, i din’t considered the sharedstate because i thought the state returned from the connector was enough and the shared was supposed to inter-widgets comunications but i guess i have to read more about it.
So far I have overrides in the connector that return my state

@Override
public UCFooState getState() {
return (UCFooState) super.getState();
}

and
@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {

I’ll go reading the link about shared states,thanks

well already a question: I actually did almost the same of the link, the main difference is that i create a class extending AbstractComponentState instead ComponentState, what is the difference? is that the cause of not raising the statechanged method?

BTW ComponentState can’t be resolved in my probject…

The ComponentState one seems to be an old documentation. It’s better to follow
this one
which uses AbstractComponentState.
The shared state should be used to communicate data between the server side and the client side. The data communication between client/GWT widget and connector can be made directly in in the onStateChanged method (which is called when state changes on the server side) like getWidget().setsomevariable(getState().somesharedvariable()).

ok, then it’s what i already did…but seems nothing happens…it’s kind of hard to debug it, system.out.println give me no result in console neither

No System.out.println in the Connector (or any other client side class) won’t be printed to the console.
To do that, have a look at
this
(the Client-side chapter).

yeh, it’s almost what i’m doing, the basic solution i’m using is to have in my widget a label where i write as stack trace, just a temporary logger i can see, i init that label as “test” and change it each time the statechanged is invoked with code like

getWidget().SetLabel(new Date().ToString() + somethingusefulltome);

where SetLabel in the widget do the myLabel.setText(blabla);

but i never saw it change …it’s always “Test”

I’m losing something really basic i suppose

the part i’m sure of is:

The state is returned of the correct type by connector, the property inside the state change (i actually have a java class instance private in the state and two methods get/set to modify it, i could try adding something basic like string)
the widget doesn’t change any when the server side change the state.

I would definately recommend you to use the client side debugging described in the article i linked to. Although it might sound complicated it’s so worth it.
Maybe you also have errors in your client side. Can you check the JavaScript console of any Browser debugging tool like firebug and see if there are some strange exceptions thrown?
If that doesn’t help can you maybe post little code snippets from the State class and where you declare it on the server and the client side (although the client side declaration you posted a few posts ago i just saw) and your onStateChanged method.
Doesn’t need to be the whole thing…just the important/interesting part…because guessing your problem is not that easy when you say you didn’t do anything different from the tutorial.

ye, you right, thanks for the support, i’m doing some check client side…maybe it’s just some null pointer passing data from server to client

ok the problem is here:

State class:

public class UCFooState extends AbstractComponentState{

private static final long serialVersionUID = 1L;
private myClass catalog;

public void setCatalog(myClass catalog){    this.catalog=catalog;    }
public myClass getCatalog(){    return this.catalog;}

}

public class UCFoo extends AbstractComponent{

private static final long serialVersionUID = 1L;
public UCClinicViewer(myClass catalog) {
    super();
    this.setCatalog(catalog);
}

public void setCatalog(myClass catalog){
    if (catalog==null)
        System.out.println("setCatalog:null");
    else
        System.out.println("setCatalog:not null");
    this.getState().setCatalog(catalog);
}

@Override
public UCFooState getState() {
    if (((UCFooState) super.getState()).getCatalog()==null)
        System.out.println("readCatalog:null");
    else
        System.out.println("readCatalog:not null");
     
    return (UCFooState) super.getState();
}

and here the result:

setCatalog:not null
readCatalog:null

somehow i write the class and read null so…two chances: or the set goes wrong or i’m reading a different class, since the set seems trivial i have to suppose that set the state in the constructor doesn’t work.
Any other idea? something wrong in the pattern?

Looking at your code it makes sense that in the getState method the catalog will still be null because the catalog will be set after getState() method finished executing.
Better debugging would be to do:

this.getState().setCatalog(catalog);
if(this.getState().getCatalog() != null)
  System.out.println("Catalog: not null");

Btw. If you just can’t get it working using states you could try if
RPC
works better for you.

Ok, you right again:

public void setCatalog(myClass catalog){
if (catalog==null)
System.out.println(“setCatalog:null”);
else
System.out.println(“setCatalog:not null”);
this.getState().setCatalog(catalog);
if (this.getState().getCatalog()==null)
System.out.println(“setCatalog:null”);
else
System.out.println(“setCatalog:not null”);
}

@Override
public UCFooState getState() {
if (((UCFooState) super.getState()).getCatalog()==null)
System.out.println(“readCatalog:null”);
else
System.out.println(“readCatalog:not null”);

    return (UCfooState) super.getState();
}

give as result:

setCatalog:not null
readCatalog:null
readCatalog:not null
setCatalog:not null

that means that the getCatalog at the end of constructor is ok, but this means that the change doenst go to the widget :

@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
super.onStateChanged(stateChangeEvent);
getWidget().setCatalog(getState().getCatalog());

don’t show the result i would like to see…need some other test client side, what i miss is: if everything goes right when the onStateChanged is invoked? what trigger it? can I force to test results?

I would do it by putting a GWT.log(“Something here”); in your onStatechange method and then using GWT dev mode to see if it’s getting called and/or check the State’s…state… (how to start GWT dev mode is described in the Debbuging Vaadin application wiki article).

the log says that is invoked just the first time when the catalog is null, then nothing anymore, the state changed don’t go up anymore

I mean from your code onStateChanged should only be called once because you’re cahging the state only once by setting catalog to something not null…
Does the onStateChanged method run without any errors/exceptions?
If yes,maybe you just need a getWidget().redraw(); to update the gwt widget. (is for example necessary for the Gflot visualization library when you add /change Dataseries at runtime).

Well the method of the widget (I wrote it) contain the change of a label i’m using to understand what happens and some other stuff i can remove for test.
the on state change runs just this method and happens just once and again this could be perfect…but the state the widget get as parameter and that comes from @Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
super.onStateChanged(stateChangeEvent);
getWidget().setCatalog(getState().getCatalog());

results null
The guess i can do is that the “event” happens before i finally set for real my catalog and then doesn’t happen anymore, here the reason why I would like to force the event somehow at the end of my set

FollowUp: thanks marius, using debug properly seems my Catalog can’t be deserialized…