Properties

Echo's server-side component synchronization peers will automatically serialize any properties (of supported types) present in a Component's "local style" to the client. That is, any property that can be updated using the Component.set() and retrieved using Component.get() will automatically be sent to the client.

Recall the server-side component implementation of SpinButton:

package example;

import nextapp.echo.app.Component;
import nextapp.echo.app.FillImage;

public class SpinButton extends Component {

    public static final String PROPERTY_BACKGROUND_IMAGE = "backgroundImage";
    public static final String VALUE_CHANGED_PROPERTY = "value";
    
    private int value;
    
    public SpinButton() {
        super();
    }
    
    public FillImage getBackgroundImage() {
        return (FillImage) get(PROPERTY_BACKGROUND_IMAGE);
    }
    
    public int getValue() {
        return value;
    }
    
    public void setBackgroundImage(FillImage newValue) {
        set(PROPERTY_BACKGROUND_IMAGE, newValue);
    }
    
    public void setValue(int newValue) {
        int oldValue = value;
        value = newValue;
        firePropertyChange(VALUE_CHANGED_PROPERTY, 
                new Integer(oldValue), new Integer(newValue));
    }
}

In the above code, the backgroundImage property is a style property, but the value property is not. Thus the synchronization peer will automatically synchronize the backgroundImage, but it will not synchronize the value. The synchronization peer will also synchronize the background, foreground, and font style properties which are defined in the parent class, Component.

There are three key requirements to ensure that a style property is synchronized automatically to the client:

  • The property must have setXXX() and getXXX() methods to store and retrieve its value.
  • The property's value must be stored/retrieved using the set() and get() methods.
  • The property must have a public static final String constant that begins with PROPERTY_ and whose value is the name of the property.

Synchronizing Non-Style Properties to the Client

If your component contains non-style properties as does our SpinButton, you will have to write synchronization code to serialize them yourself. To do this, you'll need to have your synchronization peer's constructor register the name of the property, e.g.:

    public SpinButtonPeer() {
        super();
        addOutputProperty(SpinButton.VALUE_CHANGED_PROPERTY);
    }

You'll then need to override the getOutputProperty() method of your synchronization peer to handle your added properties.

    public Object getOutputProperty(Context context, Component component, 
            String propertyName, int propertyIndex) {
        if (propertyName.equals(SpinButton.VALUE_CHANGED_PROPERTY)) {
            SpinButton spinButton = (SpinButton) component;
            return new Integer(spinButton.getValue());
        } else {
            return super.getOutputProperty(context, component, propertyName, propertyIndex);
        }
    }

This code will only send the state of properties to the client, it will not update the state of the server-side Component if the property changes on the client.