org.apache.click
Interface Stateful

All Known Implementing Classes:
AbstractLink, ActionButton, ActionLink, Button, Checkbox, Field, FieldSet, FileField, Form, HiddenField, ImageSubmit, Label, PageLink, PasswordField, Radio, RadioGroup, Reset, Select, Submit, Table, TextArea, TextField

public interface Stateful

Provides an interface that controls can implement that need to preserve state across multiple requests.

Please note: Control state is not saved and restored automatically by Click. Instead, state saving and restoring is under full control of the developer through a public API.

Controls implementing this interface are expected to save and restore their internal state, as well as the state of their child controls. Controls can further expose a public API for saving and restoring their state.

An example implementation is shown below:

 public class MyControl extends AbstractControl implements Stateful {

     private String value;

     ...

     // Return the Control internal state
     public Object getState() {
         return getValue();
     }

     // Set the Control internal state
     public void setState(Object state) {
         String fieldState = (String) state;
         setValue(fieldState);
     }

    // A save state helper method
     public void saveState(Context context) {
         // Save the control state in the session
         ClickUtils.saveState(this, getName(), context);
     }
    // A restore state helper method
     public void restoreState(Context context) {
         // Load the control state from the session
           ClickUtils.restoreState(this, getName(), context);
     }

    // A remove state helper method
     public void removeState(Context context) {
         // Remove the control state from the session
          ClickUtils.removeState(this, getName(), context);
     }

 } 
Saving and restoring Control state is controlled by the developer.

For example:

 public class MyPage extends Page {

    private MyControl control = new MyControl("mycontrol");

     public MyPage() {
         // Load the control state from the session
         control.loadState(getContext());
     }

     public void onPost() {

         Context context = getContext();

         String id = context.getParameter("id");
         control.setValue(id);

         // Save control state to the session
         control.saveState(context);
     }
 }
 


Method Summary
 Object getState()
          Return the Control internal state.
 void setState(Object state)
          Restore the Control internal state from the given state object.
 

Method Detail

getState

Object getState()
Return the Control internal state. State will generally be stored in the Session, so it is recommended to ensure the state is serializable.

Example implementation below:

 public Object getState() {
     Object stateArray[] = new Object[3];
     stateArray[0] = getValue();
     stateArray[1] = Number.valueOf(getNumber());
     stateArray[2] = Boolean.valueOf(getBoolean());
     return stateArray;
 } 

Returns:
the control internal state

setState

void setState(Object state)
Restore the Control internal state from the given state object.

Example below:

 public void setState(Object state) {

     Object[] stateArray = (Object[]) state;
     String storedValue = stateArray[0];
     setValue(storedValue);

     int storedNumber = ((Integer) stateArray[1]).intValue();
     setNumber(storedNumber);

     boolean storedBoolen = ((Boolean) stateArray[2]).booleanValue();
     setBoolean(storedBoolean);
 } 

Parameters:
state - the control state to restore