home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / EncapsulatedEvent.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  6.7 KB  |  254 lines

  1. /*
  2.  *
  3.  * @(#) EncapsulatedEvent.java 1.3@(#)
  4.  *
  5.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  6.  * 
  7.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  8.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  9.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  10.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  11.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  12.  * THIS SOFTWARE OR ITS DERIVATIVES.
  13.  * 
  14.  */
  15.  
  16. /**
  17.  * <p>
  18.  * sunw.demo.encapsulatedEvents.EncapsulatedEvent
  19.  * </p>
  20.  *
  21.  * @version 1.0
  22.  * @author Laurence P. G. Cable.
  23.  */
  24.  
  25. package sunw.demo.encapsulatedEvents;
  26.  
  27. import java.lang.reflect.Method;
  28. import java.lang.reflect.InvocationTargetException;
  29.  
  30. import java.util.EventObject;
  31. import java.util.EventListener;
  32.  
  33. /**
  34.  *
  35.  * <p>
  36.  * The EncapsulatedEvent class is a subclass of java.util.EventObject and
  37.  * is designed as part of a package sun.demo.encapsulatedEvents to demonstrate
  38.  * the dynamic generation of JavaBeans Event Adaptor classes, and also a
  39.  * technique for creating a polymorphic event processing model as an extension
  40.  * of the existing JavaBeans Event Model.
  41.  * </p>
  42.  * @see sunw.demo.encapsulatedEvents.EncapsulatedEventAdaptor
  43.  * @see sunw.demo.encapsulatedEvents.EncapsulatedEventListener
  44.  * @see sunw.demo.encapsulatedEvents.EncapsulatedEventAdaptorGenerator
  45.  * @see sunw.demo.encapsulatedEvents.EncapsulatedEventException
  46.  */
  47.  
  48. public class EncapsulatedEvent extends EventObject {
  49.  
  50.     protected EventObject           event;    
  51.     
  52.     protected Method                 listenerMethod;
  53.  
  54.     protected Class                 listenerInterface;
  55.  
  56.     protected Object[]               eventArgs;
  57.  
  58.     /**
  59.      * <p>
  60.      * Construct an EncapsulatedEvent object. An EncapsulatedEvent contains
  61.      * a reference to the "actual" event occurring, and the Method/Class
  62.      * from which this "actual" event was fired.
  63.      * </p>
  64.      * @param s    The "source" of the "actual" event (may not be null).
  65.      * @param e The "actual" event itself (or null if this is a "cracked" event.
  66.      * @param m The java.lang.reflect.Method describing the Listener method that this event was emitted from.
  67.      *
  68.      * @throw NullPointerException
  69.      */
  70.  
  71.     protected EncapsulatedEvent(Object s, EventObject e, Method m, Object[] a) {
  72.         super(s);
  73.  
  74.     if (m == null || (e == null && a == null))
  75.         throw new NullPointerException("Encapsulated Event constructor args");
  76.  
  77.         if (e == null && a != null) {
  78.         for (int i = 0; i < a.length; i++) {
  79.             if (a[i] instanceof EventObject) {
  80.                 e = (EventObject)a[i];
  81.                 break;
  82.             }
  83.         }
  84.         }
  85.  
  86.         /*
  87.          * if we are forwarding an EncapsulatedEvent then unwrap it!
  88.          */
  89.  
  90.         if (e != null && e instanceof EncapsulatedEvent) {
  91.             EncapsulatedEvent ee = (EncapsulatedEvent)e;
  92.  
  93.             event = ee.getEvent();
  94.             m     = ee.getListenerMethod();
  95.         } else event = e;
  96.  
  97.         listenerMethod    = m;
  98.         listenerInterface = m.getDeclaringClass();
  99.  
  100.     // if the event object is not included in the event args then insert
  101.     // it at index 0.
  102.  
  103.     if (e != null) {
  104.         if (a == null) {
  105.         a    = new Object[1];
  106.         a[0] = event;
  107.         } else {
  108.         int i;
  109.  
  110.         for (i = 0; i < a.length && !e.equals(a[i]); i++) 
  111.         ;
  112.  
  113.         if (i == a.length) { // not found
  114.             Object[] tmp = new Object[a.length + 1];
  115.  
  116.             tmp[0] = event; // put it at the start
  117.  
  118.             for (i = 0; i < a.length; i++) tmp[i+1] = a[i];
  119.         }
  120.         }
  121.     }
  122.  
  123.         eventArgs = a;
  124.     }
  125.     
  126.     /**
  127.      * <p>
  128.      * Construct an Event Object from an intermediate.
  129.      * </p>
  130.      */
  131.  
  132.     public EncapsulatedEvent(Object s, EventObject e, Method m) {
  133.         this(s, e, m, null);
  134.     }
  135.  
  136.     /**
  137.      * <p>
  138.      * Construct an Event Object from a simple event listener method.
  139.      * </p>
  140.      */
  141.  
  142.     public EncapsulatedEvent(EventObject e, Method m) {
  143.         this(e.getSource(), e, m);
  144.     }
  145.  
  146.     /**
  147.      * <p>
  148.      * Construct an Event Object from a cracked event listener method.
  149.      * </p>
  150.      */
  151.  
  152.     public EncapsulatedEvent(Object s, Method m, Object[] a) {
  153.         this(s, null, m, a);
  154.     }
  155.  
  156.     /**
  157.      * @return The EventObject instance encapsulated or null if from a cracked event.
  158.      */
  159.  
  160.     public EventObject getEvent() {
  161.         return event;
  162.     }
  163.  
  164.     /**
  165.      * @return The java.lang.Class of the EventObject instance encapsulated or null if from a cracked event.
  166.      */
  167.  
  168.     public Class getEventClass() {
  169.         return (event != null ? event.getClass() : null);
  170.     }
  171.  
  172.     /**
  173.      *  @return The String name of the class of the EventObject instance encapsulated or null if from a cracked event.
  174.      */
  175.  
  176.     public String getEventClassName() {
  177.         return (event != null ? event.getClass().getName() : null);
  178.     }
  179.  
  180.     /**
  181.      * @return The source object of the encapsulated EventObject instance.
  182.      */
  183.  
  184.     public Object getEventSource() {
  185.         return (event != null ? event.getSource() : this.getSource());
  186.     }
  187.  
  188.     /**
  189.      * @return the java.lang.reflect.Method of the EventListener Method.
  190.      */
  191.  
  192.     public Method getListenerMethod() { return listenerMethod; }
  193.  
  194.     /**
  195.      * @return the name of the java.lang.reflect.Method of the EventListener Method.
  196.      */
  197.  
  198.     public String getListenerMethodName() {
  199.         return listenerMethod.getName();
  200.     }
  201.  
  202.  
  203.     /**
  204.      * @return the Class for the orginating EventListener subinterface
  205.      */
  206.  
  207.     public Class getListenerInterface() {
  208.         return listenerInterface;
  209.     }
  210.  
  211.     /**
  212.      * @return the name of the Class for the orginating EventListener subinterface
  213.      */
  214.  
  215.     public String getListenerInterfaceName() {
  216.         return listenerInterface.getName();
  217.     }
  218.  
  219.     /**
  220.      * @return an Object[] of the encapsulated event, if the EventObject is not already in the args, it is inserted by this class at index 0.
  221.      */
  222.  
  223.     public Object[] getEventArguments() {
  224.     return eventArgs;
  225.     }
  226.  
  227.     /**
  228.      * <p>
  229.      * This method can be used to deliver the encapsulated event to an object
  230.      * that conforms to the EventListener sub-interface that the event originated from.
  231.      </p>
  232.      *
  233.      * @param el The EventListener object to deliver the unencapsulated event to.
  234.      *
  235.      * @throws InvocationTargetException
  236.      * @throws IllegalAccessException
  237.      */
  238.  
  239.     public void deliverEvent(EventListener el) throws InvocationTargetException, IllegalAccessException {
  240.     
  241.         /*
  242.          * a likely error here is the caller passes an EventListener
  243.          * reference that does not match the EventListener instance
  244.          * this particular EncapsulatedEvent encapsulates, fortunately
  245.          * the JVM RT checks will catch this and throw an exception.
  246.          *
  247.          * In any case the caller of this method should get the
  248.          * exception (if any) so we should not catch it here.
  249.          */
  250.  
  251.         listenerMethod.invoke((Object)el, eventArgs);
  252.     }
  253. }
  254.