home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Observable.java < prev    next >
Text File  |  1996-05-03  |  4KB  |  162 lines

  1. /*
  2.  * @(#)Observable.java    1.13 95/12/15 Chris Warth
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.util;
  21.  
  22. /*
  23.  * This class is used to hold the set of observers of an observable
  24.  * object whenever there is more than one observer.
  25.  */
  26. class ObserverList extends Vector {
  27.     /** 
  28.      * Notifies all the observers in the list.  This goes from
  29.      * back to front, so that it is OK to remove Observers from
  30.      * the list as a result of this call. 
  31.      * @param who the list of observers
  32.      * @param arg what is being notified
  33.      */
  34.     public void notifyObservers(Observable who, Object arg) {
  35.     int i = size();
  36.  
  37.     while (--i >= 0) {
  38.         Observer o;
  39.  
  40.         o = (Observer) elementAt(i);
  41.         o.update(who, arg);
  42.     }
  43.     }
  44. }
  45.  
  46.  
  47. /**
  48.  * This class should be subclassed by observable object, or "data"
  49.  * in the Model-View paradigm.  An Observable object may have any
  50.  * number of Observers.  Whenever the Observable instance changes, it
  51.  * notifies all of its observers.  Notification is done by calling
  52.  * the update() method on all observers.
  53.  *
  54.  * @version     1.13, 15 Dec 1995
  55.  * @author    Chris Warth
  56.  */
  57. public class Observable {
  58.     private boolean changed = false;
  59.     private Object obs;
  60.  
  61.     /**
  62.      * Adds an observer to the observer list.
  63.      * @param o the observer to be added
  64.      */
  65.     public synchronized void addObserver(Observer o) {
  66.     if (obs != null) {
  67.         if (obs instanceof ObserverList) {
  68.         if (!((ObserverList) obs).contains(o)) {
  69.             ((ObserverList) obs).addElement(o);
  70.         }
  71.         } else if (obs != o) {
  72.         ObserverList tmp = new ObserverList();
  73.  
  74.         tmp.addElement(obs);
  75.         tmp.addElement(o);
  76.         obs = tmp;
  77.         }
  78.     } else {
  79.         obs = o;
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * Deletes an observer from the observer list.
  85.      * @param o the observer to be deleted
  86.      */
  87.     public synchronized void deleteObserver(Observer o) {
  88.     if (obs == o) {
  89.         obs = null;
  90.     } else if (obs != null && obs instanceof ObserverList) {
  91.         ((ObserverList) obs).removeElement(o);
  92.     } 
  93.     }
  94.  
  95.     /**
  96.      * Notifies all observers if an observable change occurs.
  97.      */
  98.     public void notifyObservers() {
  99.     notifyObservers(null);
  100.     }
  101.  
  102.     /**
  103.      * Notifies all observers of the specified observable change
  104.      * which occurred.
  105.      * @param arg what is being notified
  106.      */
  107.     public synchronized void notifyObservers(Object arg) {
  108.     if (hasChanged()) {
  109.         if (obs != null) {
  110.         if (obs instanceof ObserverList) {
  111.             ((ObserverList) obs).notifyObservers(this, arg);
  112.         } else {
  113.             ((Observer) obs).update(this, arg);
  114.         }
  115.         }
  116.         clearChanged();
  117.     }
  118.     }
  119.  
  120.     /**
  121.      * Deletes observers from the observer list.
  122.      */
  123.     public synchronized void deleteObservers() {
  124.     obs = null;
  125.     }
  126.  
  127.     /**
  128.      * Sets a flag to note an observable change.
  129.      */
  130.     protected synchronized void setChanged() {
  131.     changed = true;
  132.     }
  133.  
  134.     /**
  135.      * Clears an observable change.
  136.      */
  137.     protected synchronized void clearChanged() {
  138.     changed = false;
  139.     }
  140.  
  141.     /**
  142.      * Returns a true boolean if an observable change has occurred.
  143.      */
  144.     public synchronized boolean hasChanged() {
  145.     return changed;
  146.     }
  147.  
  148.     /**
  149.      * Counts the number of observers.
  150.      */
  151.     public synchronized int countObservers() {
  152.     if (obs != null) {
  153.         if (obs instanceof ObserverList) {
  154.         return ((ObserverList)obs).size();
  155.         } else {
  156.         return 1;
  157.         }
  158.     }
  159.     return 0;
  160.     }
  161. }
  162.