Processing User Input

By default, a component synchronize peer will not accept any input from the client for security reasons. The developer must specify any properties that can be modified on the client that will result in a change in a server-side state.

Both the ComponentSynchronizePeer and Component objects will need to be modified to process input.

Synchronization Peer

With the SpinButton example, we need to update the server-side state of the value property when it is changed on the client. To do this, two methods must be added to the ComponentSynchronizePeer implementation, getInputPropertyClass() and storeInputProperty().

    public Class getInputPropertyClass(String propertyName) {
        if (SpinButton.VALUE_CHANGED_PROPERTY.equals(propertyName)) {
            return Integer.class;
        }
        return null;
    }

    public void storeInputProperty(Context context, Component component, 
            String propertyName, int propertyIndex, Object newValue) {
        if (propertyName.equals(SpinButton.VALUE_CHANGED_PROPERTY)) {
            ClientUpdateManager clientUpdateManager = 
                    (ClientUpdateManager) context.get(ClientUpdateManager.class);
            clientUpdateManager.setComponentProperty(component, 
                    SpinButton.VALUE_CHANGED_PROPERTY, newValue);
        }
    }

The getInputPropertyClass() will be used to determine the type of the property, and thus how to serialize its state into a server-side Java object. If this method returns null, the property will be ignored.

The storeInputProperty() method informs the peer that the client-side state of a property has changed. The implementation of this method should first determine if the property is allowed to be modified, and if so, it should then use the ClientUpdateManager to record the change. The ClientUpdateManager will then update the state of the component.

It is critically important to use the ClientUpdateManager rather than directly manipulating the state of the associated Component instance. This is because several properties may be updated across numerous components in any given client-server synchronization, and it's important that each bit of processing code see the component hierarchy in an unchanged state. This would not be the case if every property processor immediately altered the state of its supported component.

Component

The Component object needs to be modified to process the input data that it will be sent by the ClientUpdateManager. To do this, we create a processInput method in the Componetn class, such as the following to handle value updates in SpinButton:

    public void processInput(String inputName, Object inputValue) {
        super.processInput(inputName, inputValue);
        if (VALUE_CHANGED_PROPERTY.equals(inputName)) {
            Integer value = (Integer) inputValue;
            setValue(value == null ? 0 : value.intValue());
        }
    }