home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / Event.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  3.7 KB  |  128 lines  |  [TEXT/CWIE]

  1. // Event.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** Abstract Object subclass representing a user or program action, such as a
  10.   * mouse click, key press, and so on.  Events are created in response to these
  11.   * actions and directed to the application's EventLoop.  The EventLoop
  12.   * removes each Event and forwards it to the Event's processor, the object
  13.   * responsible for determining which object should ultimately receive the
  14.   * Event.  In most cases, this object is the application itself.<p>
  15.   * In general, you never work directly with the event processing mechanism.
  16.   * However, if you do need to create an Event subclass requiring special
  17.   * processing, you can set the Event subclass' processor to the object that
  18.   * knows how to process it, and hand the Event to the application's EventLoop
  19.   * using the code:
  20.   * <pre>
  21.   *     Application.application().eventLoop().addEvent(newEvent);
  22.   * </pre>
  23.   *
  24.   * @see MouseEvent
  25.   * @see KeyEvent
  26.   * @see EventLoop
  27.   * @see Application
  28.   */
  29.  
  30.  
  31. public class Event implements Cloneable {
  32.     EventProcessor      processor;
  33.     Object              synchronousLock;
  34.     long                timeStamp;
  35.     int                 type;
  36.  
  37. /* constructors */
  38.  
  39.     /** Constructs an Event, setting its time stamp to the current time. */
  40.     public Event() {
  41.         this(System.currentTimeMillis());
  42.     }
  43.  
  44.     /** Constructs an Event, setting its time stamp to <b>timeStamp</b>. */
  45.     public Event(long timeStamp) {
  46.         super();
  47.         this.timeStamp = timeStamp;
  48.     }
  49.  
  50.     /** Sets the Event's type to <b>aType</b>.
  51.       */
  52.     public void setType(int aType) {
  53.         type = aType;
  54.     }
  55.  
  56.     /** Returns the Event's type.
  57.       * @see #setType
  58.       */
  59.     public int type() {
  60.         return type;
  61.     }
  62.  
  63.     /** Sets the Event's time stamp to <b>timeStamp</b>.  You will almost never
  64.       * call this method.  Instead, construct an Event with the correct
  65.       * time stamp.
  66.       * @see Event(long)
  67.       */
  68.     public void setTimeStamp(long timeStamp) {
  69.         this.timeStamp = timeStamp;
  70.     }
  71.  
  72.     /** Returns the Event's time stamp.
  73.       */
  74.     public long timeStamp() {
  75.         return timeStamp;
  76.     }
  77.  
  78.     /** Sets the Event's processor to <b>aProcessor</b>, the object that
  79.       * determines how to respond to the Event once it has been removed
  80.       * from the EventLoop.
  81.       * @see EventProcessor
  82.       */
  83.     public void setProcessor(EventProcessor aProcessor) {
  84.         processor = aProcessor;
  85.     }
  86.  
  87.     /** Returns the Event's processor.
  88.       * @see #setProcessor
  89.       */
  90.     public EventProcessor processor() {
  91.         return processor;
  92.     }
  93.  
  94.     /** Clones the Event.
  95.       */
  96.     public Object clone() {
  97.         try {
  98.             return super.clone();
  99.         } catch (CloneNotSupportedException e) {
  100.             throw new InconsistencyException(this +
  101.                                          ": clone() not supported :" + e);
  102.         }
  103.     }
  104.  
  105.     synchronized Object synchronousLock() {
  106.         return synchronousLock;
  107.     }
  108.  
  109.     synchronized Object createSynchronousLock() {
  110.         if (synchronousLock != null) {
  111.             throw new InconsistencyException(
  112.                     "Can't create synchronous lock if one is already set");
  113.         } else {
  114.             synchronousLock = new Object();
  115.             return synchronousLock;
  116.         }
  117.     }
  118.  
  119.     synchronized void clearSynchronousLock() {
  120.         if (synchronousLock == null) {
  121.             throw new InconsistencyException(
  122.                             "Can't clear synchronous lock if one isn't set");
  123.         } else {
  124.             synchronousLock = null;
  125.         }
  126.     }
  127. }
  128.