/19/ delegation event model: implementation (practice)
// PACKAGE
AsSetupPackage("org.moock.event");

// CLASS EventObject

/**
 * The base class for an object describing an event.
 * EventObject instances are passed to methods defined
 * by classes that implement an EventListener subinterface. 
 * Each kind of event should be represented by an 
 * EventObject subclass. 
 *
 * Each EventObject instance stores a reference to its 
 * event "source", which is the object that generated event.
 *
 * @param   src   The source of the event.
 */
org.moock.event.EventObject = function (src) {
  this.source = src;
}
  
// PROPERTIES
  // The source of the event.
  org.moock.event.EventObject.prototype.source = null;

// METHODS
  /**
   * Returns the source of the event.
   */
  org.moock.event.EventObject.prototype.getSource = function () {
    return this.source;
  }
// PACKAGE
AsSetupPackage("org.moock.event");

// CLASS EventListenerList

/**
 * Manages a list of objects registered to receive events
 * (i.e., instances of a class that implements EventListener).
 * This class is used by an event source to store its
 * listeners.
 */
org.moock.event.EventListenerList = function () {
  // Create a new array in which to store listeners.
  this.listeners = new Array();
}

// PROPERTIES
  // The listener objects.
  org.moock.event.EventListenerList.prototype.listeners = null;

// METHODS
  /**
   * Adds a listener to the list.
   *
   * @param   l   The listener to add. Must implement EventListener.
   */
  org.moock.event.EventListenerList.prototype.addObj = function (l) {
    this.listeners.push(l);
    return true;
  }

  /**
   * Removes a listener from the list.
   *
   * @param   l   The listener to remove. Must implement EventListener.
   */
  org.moock.event.EventListenerList.prototype.removeObj = function (l) {
    // Search for the specified listener.
    var len = this.listeners.length;
    for (var i = 0; i < len; i++) {
      if (this.listeners[i] == l) {
        // We found the listener, so remove it.
        this.listeners.splice(i, 1);
        // Quit looking.
        return true;
      }
    }
    return false;
  }

  /**
   * Returns the complete list of listeners, used during 
   * event notification.
   */
  org.moock.event.EventListenerList.prototype.getListeners = function () {
    // Return a copy of the list, not the list itself.
    return this.listeners.slice(0);
  }
/**
 * interface org.moock.event.EventListener
 *
 * This is a "tagging" interface that marks an
 * interface as an event listener. All event listener
 * interfaces should extend this interface.
 */