Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Integrating with the Server-Side

For client-to-server communication, a connector can make remote procedure calls (RPC) to the server-side. Also, the server-side component can make RPC calls to the connector. For a thorough description of the RPC mechanism, refer to Section 16.6, “RPC Calls Between Clientand Server-Side”.

16.5. Shared State

The basic communication from a server-side component to its the client-side widget counterpart is handled using a shared state. The shared state is serialized transparently. It should be considered read-only on the client-side, as it is not serialized back to the server-side.

A shared state object simply needs to extend the ComponentState. It should contain setters and getters for the properties that are to be synchronized.

public class MyComponentState extends ComponentState { private String text;

public String getText() { return text;

}

public void setText(String text) { this.text = text;

}

}

You can also use public member variables and access them directly. However, if you do have setters and getters, as in the above example, the corresponding member variables may not be public or they will create a name conflict during serialization.

16.5.1. Accessing Shared State on Server-Side

A server-side component can access the shared state with the getState() method, which returns the ComponentState object of the shared state type of the component.

It is required that you override the base implementation with one that returns the proper shared state type, as follows:

@Override

public MyComponentState getState() {

return (MyComponentState) super.getState();

}

You can then use the getState() to access the shared state object with the proper type.

public MyComponent() {

getState().setText("This is the initial state");

....

}

16.5.2. Handing Shared State in a Connector

A connector can access a shared state with the getState() method. The access should be read-only. It is required that you override the base implementation with one that returns the proper shared state type, as follows:

@Override

public MyComponentState getState() {

return (MyComponentState) super.getState();

}

366

Shared State

Integrating with the Server-Side

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. You should should always call the superclass method before anything else to handle changes to common component properties.

@Override

public void onStateChanged(StateChangeEvent stateChangeEvent) { super.onStateChanged(stateChangeEvent);

// Do something useful with the data final String text = getState().getText(); getWidget().setText(text);

}

16.5.3. Referring to Components in Shared State

While you can pass any regular Java objects through a shared state, referring to another component requires special handling because on the server-side you can only refer to a server-side component, while on the client-side you only have widgets. References to components can be made by referring to their connectors, which are shared.

public class MyComponentState extends ComponentState { private Connector otherComponent;

public Connector getOtherComponent() { return otherComponent;

}

public void setOtherComponent(Connector otherComponent) { this.otherComponent = otherComponent;

}

}

You could then access the component on the server-side as follows:

public class MyComponent {

public void MyComponent(Component otherComponent) { getState().setOtherComponent(otherComponent);

}

public Component getOtherComponent() {

return (Component)getState().getOtherComponent();

}

// And the cast method @Override

public MyComponentState getState() {

return (MyComponentState) super.getState();

}

}

On the client-side, you should cast it in a similar fashion to a ComponentConnector, or possibly to the specific connector type if it is known.

16.5.4. Sharing Resources

Resources, which commonly are references to icons or other images, are another case of objects that require special handling in sharing. A Resource object exists only on the server-side and on the client-side you have an URL to the resource. The shared state object needs to pass the reference as a URLReference object.

Referring to Components in Shared State

367

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]