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

GenericButtonAdapter.java

// This example is from the book Developing Java Beans by Robert Englander. 
// Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

// Chapter 5 -- The GenericButtonAdapter class

package BeansBook.util;

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import java.util.*;
import java.io.*;

public class GenericButtonAdapter 
         implements ActionListener, // the adapter receives the events
                    Serializable
{
   // the target object
   protected Object theTarget;

   // the class of the target object
   protected Class theTargetClass;

   // the class array for the parameters used for
   // the target method
   protected final static Class paramClasses[] = 
                    { java.awt.event.ActionEvent.class };

   // the mapping of source objects to callback methods
   protected transient Hashtable mappingTable;
  
   // constructor
   public GenericButtonAdapter(Object target)
   {
      theTarget = target;
      theTargetClass = target.getClass();
   }
   
   // add an action object to listen for, along with the
   // method to call on the target when the action event
   // is received
   public void registerActionEventHandler(Button b, String methodName)
                 throws NoSuchMethodException
   {
      addMapping(b, methodName);
      b.addActionListener(this);   
   }

   // add a method mapping
   protected void addMapping(Button b, String methodName)
                 throws NoSuchMethodException
   {
      if (mappingTable == null)
      {
         mappingTable = new Hashtable();
      }

      Method m = theTargetClass.getMethod(methodName, paramClasses);
      mappingTable.put(b, m); 
   }

   // implement the listener method
   public void actionPerformed(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);
      }
   }

   // handle the writing of the object state
   private void writeObject(ObjectOutputStream stream)
             throws IOException
   {
      // 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);
   }
   }

   // handle the reading of the object state
   private void readObject(ObjectInputStream stream)
             throws IOException
   {
      // 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();
      }
   }
}