home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / de86gnzn / examples / boink / com / next / gt / eventmanager.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.5 KB  |  110 lines

  1. /**
  2.  *
  3.  * EventManager.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 21/1996
  7.  *
  8.  * EventManager maintains a list of objects that require notification when
  9.  * a particular Event occurs.  Objects register with a specific Event or a
  10.  * list of Events that it would like notification on.
  11.  *
  12. */
  13.  
  14. package com.next.gt;
  15.  
  16. import java.util.Vector;
  17. import java.awt.Event;
  18.  
  19. public class EventManager extends java.lang.Object {
  20.   protected    Vector            objectsToNotify;
  21.  
  22.  
  23.  
  24. public EventManager() {
  25.   objectsToNotify= new Vector();
  26. } /*EventManager()*/
  27.  
  28.  
  29.  
  30. /**
  31.  * Register for notification of an event with a Vector of events. 
  32.  */
  33. public void registerForEventNotification (Object theObject, int[] theEventVector) {
  34.   int        theEventID;
  35.   
  36.   for (int i= 0; i < theEventVector.length; i ++) {
  37.     Vector        registerVector= new Vector(2);
  38.     
  39.     theEventID= theEventVector[i];
  40.     registerVector.addElement(theObject);               // the object to notify
  41.     registerVector.addElement(new Integer(theEventID)); // theEventID
  42.     objectsToNotify.addElement(registerVector);
  43.     
  44.   } /*nexti*/
  45.   
  46. } /*registerForBonusNotification*/
  47.  
  48.  
  49.  
  50. /**
  51.  * Register for a single notification of an event.
  52.  */
  53. public void registerForSingleEventNotification (Object theObject, int theEventID) {
  54.   Vector    registerVector= new Vector(2);
  55.   
  56.   registerVector.addElement(theObject);                 // the object to notify
  57.   registerVector.addElement(new Integer(theEventID));   // theEventID
  58.   
  59.   objectsToNotify.addElement(registerVector);
  60.   
  61. } /*registerForBonusNotification*/
  62.  
  63.  
  64.  
  65.  
  66. /**
  67.  * Remove object from notification registry.
  68.  */
  69. public void removeFromNotificationRegistry(Object theObject) {
  70.   Vector    anObject;
  71.  
  72.   for (int i= 0; i<objectsToNotify.size(); i++) {
  73.     anObject= (Vector) objectsToNotify.elementAt(i);
  74.     if (anObject.contains(theObject)) {
  75.       objectsToNotify.removeElementAt(i);
  76.     } /*endif*/
  77.   
  78.   } /*nexti*/
  79.   
  80. } /*removeFromNotificationRegistry*/
  81.  
  82.  
  83.  
  84. /**
  85.  * Handle the event by notifying registered objects.
  86.  *
  87.  */
  88. public boolean handleEvent (Event theEvent) {
  89.   Vector    anObject;
  90.   Integer    n1;
  91.   Integer    anInteger;
  92.   boolean    returnValue= false;
  93.   
  94.   for (int i= 0; i<objectsToNotify.size(); i++) {
  95.     anObject= (Vector) objectsToNotify.elementAt(i);
  96.     
  97.     n1= (Integer) anObject.elementAt(1);
  98.  
  99.     if(theEvent.id==n1.intValue()) {
  100.       EventHandler    theEventHandler= (EventHandler) anObject.elementAt(0);
  101.       returnValue= theEventHandler.handleRequestedEvent(theEvent);
  102.     } /*endif*/
  103.         
  104.   } /*nexti*/
  105.   return returnValue;
  106.   
  107. } /*handleEvent*/
  108.  
  109. } /*EventManager*/
  110.