home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / com / next / gt / eventbackup / EventManager.java < prev   
Encoding:
Java Source  |  1998-04-10  |  2.5 KB  |  111 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.*;
  18. import java.awt.event.*;
  19.  
  20. public class EventManager extends java.lang.Object {
  21.   protected    Vector            objectsToNotify;
  22.  
  23.  
  24.  
  25. public EventManager() {
  26.   objectsToNotify= new Vector();
  27. } /*EventManager()*/
  28.  
  29.  
  30.  
  31. /**
  32.  * Register for notification of an event with a Vector of events. 
  33.  */
  34. public void registerForEventNotification (Object theObject, int[] theEventVector) {
  35.   int        theEventID;
  36.   
  37.   for (int i= 0; i < theEventVector.length; i ++) {
  38.     Vector        registerVector= new Vector(2);
  39.     
  40.     theEventID= theEventVector[i];
  41.     registerVector.addElement(theObject);               // the object to notify
  42.     registerVector.addElement(new Integer(theEventID)); // theEventID
  43.     objectsToNotify.addElement(registerVector);
  44.     
  45.   } /*nexti*/
  46.   
  47. } /*registerForBonusNotification*/
  48.  
  49.  
  50.  
  51. /**
  52.  * Register for a single notification of an event.
  53.  */
  54. public void registerForSingleEventNotification (Object theObject, int theEventID) {
  55.   Vector    registerVector= new Vector(2);
  56.   
  57.   registerVector.addElement(theObject);                 // the object to notify
  58.   registerVector.addElement(new Integer(theEventID));   // theEventID
  59.   
  60.   objectsToNotify.addElement(registerVector);
  61.   
  62. } /*registerForBonusNotification*/
  63.  
  64.  
  65.  
  66.  
  67. /**
  68.  * Remove object from notification registry.
  69.  */
  70. public void removeFromNotificationRegistry(Object theObject) {
  71.   Vector    anObject;
  72.  
  73.   for (int i= 0; i<objectsToNotify.size(); i++) {
  74.     anObject= (Vector) objectsToNotify.elementAt(i);
  75.     if (anObject.contains(theObject)) {
  76.       objectsToNotify.removeElementAt(i);
  77.     } /*endif*/
  78.   
  79.   } /*nexti*/
  80.   
  81. } /*removeFromNotificationRegistry*/
  82.  
  83.  
  84.  
  85. /**
  86.  * Handle the event by notifying registered objects.
  87.  *
  88.  */
  89. public boolean handleEvent (AWTEvent theEvent) {
  90.   Vector    anObject;
  91.   Integer    n1;
  92.   Integer    anInteger;
  93.   boolean    returnValue= false;
  94.   
  95.   for (int i= 0; i<objectsToNotify.size(); i++) {
  96.     anObject= (Vector) objectsToNotify.elementAt(i);
  97.     
  98.     n1= (Integer) anObject.elementAt(1);
  99.  
  100.     if(theEvent.getID()==n1.intValue()) {
  101.       EventHandler    theEventHandler= (EventHandler) anObject.elementAt(0);
  102.       returnValue = theEventHandler.handleRequestedEvent(theEvent);
  103.     } /*endif*/
  104.         
  105.   } /*nexti*/
  106.   return returnValue;
  107.   
  108. } /*handleEvent*/
  109.  
  110. } /*EventManager*/
  111.