Table of Contents Previous Section

Creating a Subclass of Association

If you have an applet, but do not have the source code for it, you must follow the following strategy for making the applet a client-side component. You must know the applet's accessor methods for setting and getting state, and, if the applet triggers actions, there must be some way for your Association to detect this. If the applet doesn't have API for getting and setting state, you cannot make the applet into a client-side component.

  1. Declare an subclass of the Association class.
        class MyAssociation extends Association {
            // ...
        }
    

  2. Implement the keys() method to return a list (Vector) of keys managed by the applet. See "Implementing the SimpleAssociationDestination Interface" for an example.

  3. Implement the takeValueForKey(Object, String) and valueForKey(String) methods to set and get the values of keys. Use Association's destination() method to obtain the destination object (that is, the applet).
        synchronized public Object valueForKey(String key) {
            Object dest = this.destination();
            if (key.equals("title")) {
                return ((MyApplet)dest).getLabel();
            }
        }
    
        synchronized public void takeValueForKey(Object value, String key) {
            Object dest = this.destination();
            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 {
                    ((MyApplet)dest).setLabel(((value == null) ? "" : (String)value));
                } 
        }
    

    Note that the class of the destination applet ("MyApplet" in the example) must be cast.

    If the applet triggers an action method, it must some mechanism for communicating this event to observers (such as an "observeGadget()" method).

  4. The Association responds to the triggering of the applet's action by sending invokeAction(String) to itself.
        public void observeGadget(Object sender, String action) { // fictictious
            if ((sender instanceof Gadget) && action.equals("vacuum")) {
                this.invokeAction(action);
            }
        }
    
    Note that in this hypothetical example, the Association must first set itself up as an observer.