org.jhotdraw.gui.event
Class GenericListener

java.lang.Object
  extended by org.jhotdraw.gui.event.GenericListener

public abstract class GenericListener
extends java.lang.Object

The GenericListener creates anonymous listener classes at runtime.

Usage:

 public class Demo {
   JPanel root = new JPanel(new BorderLayout());
   JLabel label = new JLabel(" ");

   public void myButtonAction(ActionEvent e) {
     label.setText("buttonAction");
   }

   public void myMouseEntered(MouseEvent e) {
     label.setText("mouseEntered: "+e.toString());
   }

   Demo() {
     JButton button = new JButton("Button with Dynamic Listener");

     //This listener will be generated at run-time, for example at run-time
     // an ActionListener class will be code-generated and then
     // class-loaded.  Only one of these is actually created, even
     // if many calls to GenericListener.create(ActionListener.class ...)
     // are made.
     ActionListener actionListener = (ActionListener)(GenericListener.create(
       ActionListener.class,
       "actionPerformed",
       this,
       "myButtonAction")
     );
     button.addActionListener(actionListener);

     // Here's another dynamically generated listener.  This one is
     // a little different because the listenerMethod argument actually
     // specifies one of many listener methods.  In the previous example
     // "actionPerformed" named the one and only ActionListener method.
     MouseListener mouseListener = (MouseListener)(GenericListener.create(
       MouseListener.class,
       "mouseEntered",
       this,
       "myMouseEntered")
     );
     button.addMouseListener(mouseListener);
 

Version:
$Id: GenericListener.java 535 2009-06-14 08:03:23Z rawcoder $
Author:
Werner Randelshofer

Nested Class Summary
private static class GenericListener.DefaultInvoker
          Implementation of the InvocationHandler which handles the basic object methods.
 
Constructor Summary
private GenericListener()
           
 
Method Summary
static java.lang.Object create(java.lang.Class listenerInterface, java.lang.String listenerMethodName, java.lang.Object target, java.lang.String targetMethodName)
          A convenient version of create(listenerMethod, targetObject, targetMethod).
static java.lang.Object create(java.lang.reflect.Method listenerMethod, java.lang.Object target, java.lang.reflect.Method targetMethod)
          Return an instance of a class that implements the interface that contains the declaration for listenerMethod.
private static java.lang.reflect.Method getListenerMethod(java.lang.Class listenerInterface, java.lang.String listenerMethodName)
           
private static java.lang.reflect.Method getTargetMethod(java.lang.Object target, java.lang.String targetMethodName, java.lang.Class[] parameterTypes)
           
private static java.lang.reflect.Method raiseToPublicClass(java.lang.reflect.Method m)
           
private static java.lang.reflect.Method raiseToPublicClass(java.lang.reflect.Method m, java.lang.Class c)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericListener

private GenericListener()
Method Detail

create

public static java.lang.Object create(java.lang.Class listenerInterface,
                                      java.lang.String listenerMethodName,
                                      java.lang.Object target,
                                      java.lang.String targetMethodName)
A convenient version of create(listenerMethod, targetObject, targetMethod). This version looks up the listener and target Methods, so you don't have to.


create

public static java.lang.Object create(java.lang.reflect.Method listenerMethod,
                                      java.lang.Object target,
                                      java.lang.reflect.Method targetMethod)
Return an instance of a class that implements the interface that contains the declaration for listenerMethod. In this new class, listenerMethod will apply target.targetMethod to the incoming Event.


getListenerMethod

private static java.lang.reflect.Method getListenerMethod(java.lang.Class listenerInterface,
                                                          java.lang.String listenerMethodName)

getTargetMethod

private static java.lang.reflect.Method getTargetMethod(java.lang.Object target,
                                                        java.lang.String targetMethodName,
                                                        java.lang.Class[] parameterTypes)

raiseToPublicClass

private static java.lang.reflect.Method raiseToPublicClass(java.lang.reflect.Method m)

raiseToPublicClass

private static java.lang.reflect.Method raiseToPublicClass(java.lang.reflect.Method m,
                                                           java.lang.Class c)