Table of Contents Previous Section

Implementing the SimpleAssociationDestination Interface

If you write an applet, or aquire the source code for an applet, you will probably want to follow this procedure to give the applet the associative behavior it needs to be a client-side component.

  1. In the class declaration, insert the "implements SimpleAssociationDestination" clause.
       public class MyApplet extends Applet implements SimpleAssociationDestination {
          // ....
        }
    

  2. Implement the keys() method to return a list (Vector) of state keys managed by the applet.
       public Vector keys() {
            Vector keys = new Vector(1);
            keys.addElement("title");
            return keys;
        }
    
  3. Implement the takeValueForKey(Object, String) and valueForKey(String) methods to set and get the values of keys.
        synchronized public Object valueForKey(String key) {
            if (key.equals("title")) {
                return this.getLabel();
            }
        }
    
        synchronized public void takeValueForKey(Object value, String key) {
            if (key.equals("title")) {
                if ((value != null) && !(value instanceof String)) {
                    System.out.println("Object value of wrong type set for key 
                            'title'.  Value must be a String.");
                } else {
                    self.setLabel(((value == null) ? "" : (String)value));
                } 
        }
    

    You should be able to access the keys directly or, ideally, through accessor methods ("getLabel()" and "setLabel()" in the above example). It is a good idea to use the synchronize modifier with takeValueForKey(Object, String) and valueForKey(String) because these methods can be invoked from other threads to read or set data.

    The remaining steps apply only if the applet has an action.

  4. Declare an instance variable for the applet's Association object and then, in setAssociation(Association), assigned the passed-in object to that variable.
        protected Association _assoc;
        // ...
        synchronized public void setAssociation(Association assoc) {
            _assoc = assoc;
        }
    

    The Association object must be stored so it can be used later as the receiver of the invokeAction() message. The Association forwards the action to the AppletGroupController, which handles the invocation of the server-side action method.

  5. When an action is invoked in the applet, send invokeAction(String) to the applet's Association.
        synchronized public boolean action(Event evt, Object what) {
            if (_assoc != null) {
                _assoc.invokeAction("action");
            }
            return true;
        }
    

Table of Contents Next Section