FileDocCategorySizeDatePackage
GenericButtonAdapter.javaAPI DocExample4461Tue Jun 03 23:34:52 BST 1997BeansBook.util

GenericButtonAdapter

public class GenericButtonAdapter extends Object implements Serializable, ActionListener

Fields Summary
protected Object
theTarget
protected Class
theTargetClass
protected static final Class[]
paramClasses
protected transient Hashtable
mappingTable
Constructors Summary
public GenericButtonAdapter(Object target)

  
   // constructor
     
   
      theTarget = target;
      theTargetClass = target.getClass();
   
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent evt)

      try
      {
         // invoke the registered method on the target
         Method m = (Method)mappingTable.get(evt.getSource());
         Object[] params = { evt };
         m.invoke(theTarget, params);
      }
      catch (InvocationTargetException e)
      {
         System.out.println(e);
      }
      catch (IllegalAccessException e)
      {
         System.out.println(e);
      }
   
protected voidaddMapping(java.awt.Button b, java.lang.String methodName)

      if (mappingTable == null)
      {
         mappingTable = new Hashtable();
      }

      Method m = theTargetClass.getMethod(methodName, paramClasses);
      mappingTable.put(b, m); 
   
private voidreadObject(java.io.ObjectInputStream stream)

      // use default serialization for the non-transient members
      try
      {
         stream.defaultReadObject();

         // get the number of mappings
         int cnt = stream.readInt();

         // read the keys and values for the mapping
         for (int i = 0; i < cnt; i++)
         {
            // read the button and method name
            Button b = (Button)stream.readObject();
            String name = (String)stream.readObject();

            // add the mapping
            addMapping(b, name);
         }
      }
      catch (Exception e)
      {
         throw new IOException();
      }
   
public voidregisterActionEventHandler(java.awt.Button b, java.lang.String methodName)

      addMapping(b, methodName);
      b.addActionListener(this);   
   
private voidwriteObject(java.io.ObjectOutputStream stream)

      // use default serialization for the non-transient members
      stream.defaultWriteObject();

      // store the number of mappings
      int cnt = 0;
      
      if (mappingTable == null)
      {
         // there are no mappings, so store a 0 count and return
         stream.writeInt(cnt);
         return;
      }

      // get a clone of the mapping table
      Hashtable tempmapping;
      synchronized (mappingTable)
      {
         tempmapping = (Hashtable)mappingTable.clone();
      }
      
      cnt = tempmapping.size();
      stream.writeInt(cnt);

   // get the enumerations of the keys and values
   Enumeration keys = tempmapping.keys();
   Enumeration vals = tempmapping.elements();

   // store the keys and values
   for (int i = 0; i < cnt; i++)
   {
      // get the button and associated method
      Button b = (Button)keys.nextElement();
      Method m = (Method)vals.nextElement();

      // get the method name
      String name = m.getName();

      // store the button and method name
      stream.writeObject(b);
      stream.writeObject(name);
   }