home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / LocaleChangeManager.java < prev    next >
Text File  |  1998-05-08  |  4KB  |  110 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.intl.beans;
  21.  
  22. import java.awt.*;
  23. import java.util.*;
  24.  
  25. import borland.samples.intl.beans.event.*;
  26.  
  27. /**
  28.  * LocaleChangeManager is a non-visual java bean which 
  29.  * notifies registered observers when a locale change occurs.
  30.  * LocaleChangeManager itself must be registered with a
  31.  * LocaleChangeEvent source, in this case LocaleChooser.
  32.  * Using a LocaleChangeManager rather than having locale-aware components
  33.  * register themselves directly with the user-interface for selecting
  34.  * locales has two advantages:
  35.  *<ol>
  36.  *<li>Switching to a different locale chooser only requires changing
  37.  *    one hookup, the LocaleChangeManager to the locale chooser component.
  38.  *<li>Locale-aware components don't need to have (global) access to
  39.  *    the locale chooser component.
  40.  *</ol>
  41.  *
  42.  */
  43. public class LocaleChangeManager implements LocaleChangeListener {
  44.   // holds the single instance of the LocaleChangeManager
  45.   private static LocaleChangeManager localeChangeManager = null;
  46.   // holds registered event listeners
  47.   Vector localeChangeListeners = new Vector();
  48.   private static Locale locale = Locale.getDefault();
  49.  
  50.   protected LocaleChangeManager() {
  51.   }
  52.  
  53.   /**
  54.    * Returns the single instance of the locale change manager.
  55.    *
  56.    * @return instance of locale change manager
  57.    */
  58.   public static LocaleChangeManager getLocaleChangeManager() {
  59.     if (localeChangeManager == null) {
  60.       localeChangeManager = new LocaleChangeManager();
  61.     }
  62.     return localeChangeManager;
  63.   }
  64.  
  65.   /**
  66.    * Returns the current locale according to the locale change manager
  67.    * @return locale change manager's current locale
  68.    */
  69.   public static Locale getLocale() {
  70.     return new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant()); 
  71.   }
  72.  
  73.   /**
  74.    * Registers a LocaleChangeListener to be notified on a locale change event.
  75.    *
  76.    * @param listener LocaleChangeListener to be added to notification list
  77.    */
  78.   public synchronized void addLocaleChangeListener(LocaleChangeListener listener) {
  79.     localeChangeListeners.addElement(listener);
  80.   }
  81.  
  82.   /**
  83.    * Removes a LocaleChangeListener to be notified on a locale change event.
  84.    *
  85.    * @param listener LocaleChangeListener to be removed
  86.    */
  87.   public synchronized void removeLocaleChangeListener(LocaleChangeListener listener) {
  88.     localeChangeListeners.removeElement(listener);
  89.   }
  90.  
  91.   /**
  92.    * Required for implementation of the LocaleChangeListener interface.
  93.    * Notifies all registered listeners of a locale change
  94.    *
  95.    * @param e LocaleChangeEvent describing new locale
  96.    */
  97.   public void localeChanged(LocaleChangeEvent e) {
  98.     Vector listeners;
  99.  
  100.     locale = e.getLocale();
  101.  
  102.     synchronized (this) {
  103.       listeners = (Vector) localeChangeListeners.clone();
  104.     }
  105.     for (int i = 0; i < listeners.size(); i++) {
  106.       ((LocaleChangeListener) listeners.elementAt(i)).localeChanged(e);
  107.     }
  108.   }
  109. }
  110.