home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / ListResourceBundle.java < prev    next >
Text File  |  1997-05-20  |  7KB  |  170 lines

  1. /*
  2.  * @(#)ListResourceBundle.java    1.6 97/01/29
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.util;
  32. import java.util.Hashtable;
  33.  
  34. /**
  35.  * <code>ListResourceBundle</code> is a abstract subclass of
  36.  * <code>ResourceBundle</code> that manages resources for a locale
  37.  * in a convenient and easy to use list. See <code>ResourceBundle</code> for
  38.  * more information about resource bundles in general.
  39.  *
  40.  * <P>
  41.  * Subclasses must override <code>getContents</code> and provide an array,
  42.  * where each item in the array is a pair of objects.
  43.  * The first element of each pair is a <code>String</code> key, and the second
  44.  * is the value associated with that key.
  45.  *
  46.  * <p>
  47.  * In the following example, the keys are of the form "s1"... The actual
  48.  * keys are entirely up to your choice, so long as they are the same as
  49.  * the keys you use in your program to retrieve the objects from the bundle.
  50.  * Keys are case-sensitive. <code>MyResource</code> is the default version
  51.  * of the bundle family, and <code>MyResource_fr</code> is the french version:
  52.  * <blockquote>
  53.  * <pre>
  54.  * //====================
  55.  * class MyResource extends ListResourceBundle {
  56.  *     public Object[][] getContents() {
  57.  *         return contents;
  58.  *     }
  59.  *     static final Object[][] contents = {
  60.  *     // LOCALIZE THIS
  61.  *         {"s1", "3"},        // starting value in choice field
  62.  *         {"s2", "MyDisk"},    // starting value in string field
  63.  *         {"s3", "3 Mar 96"},    // starting value in date field
  64.  *         {"s4", "The disk '{1}' contained {0} on {2}."},    // initial pattern
  65.  *         {"s5", "0"},        // first choice number
  66.  *         {"s6", "no files"},    // first choice value
  67.  *         {"s7", "1"},        // second choice number
  68.  *         {"s8", "one file"},    // second choice value
  69.  *         {"s9", "2"},        // third choice number
  70.  *         {"s10", "{0}|3 files"},    // third choice value
  71.  *         {"s11", "format threw an exception: {0}"},    // generic exception message
  72.  *         {"s12", "ERROR"},    // what to show in field in case of error
  73.  *         {"s14", "Result"},    // label for formatted stuff
  74.  *         {"s13", "Dialog"},    // standard font
  75.  *         {"s15", "Pattern"},    // label for standard pattern
  76.  *         {"s16", new Dimension(1,5)}    // real object, not just string
  77.  *     // END OF MATERIAL TO LOCALIZE
  78.  *     };
  79.  * }
  80.  * //====================
  81.  * class MyResource_fr  extends ListResourceBundle {
  82.  *     public Object[][] getContents() {
  83.  *         return contents;
  84.         }
  85.  *     static final Object[][] contents = {
  86.  *     // LOCALIZE THIS
  87.  *         {"s1", "3"},        // starting value in choice field
  88.  *         {"s2", "MonDisk"},    // starting value in string field
  89.  *         {"s3", "3 Mar 96"},    // starting value in date field
  90.  *         {"s4", "Le disk '{1}' a {0} a {2}."},    // initial pattern
  91.  *         {"s5", "0"},        // first choice number
  92.  *         {"s6", "pas de files"},    // first choice value
  93.  *         {"s7", "1"},        // second choice number
  94.  *         {"s8", "une file"},    // second choice value
  95.  *         {"s9", "2"},        // third choice number
  96.  *         {"s10", "{0}|3 files"},    // third choice value
  97.  *         {"s11", "Le format a jete une exception: {0}"},    // generic exception message
  98.  *         {"s12", "ERROR"},    // what to show in field in case of error
  99.  *         {"s14", "Resulte"},    // label for formatted stuff
  100.  *         {"s13", "Dialogue"},    // standard font
  101.  *         {"s15", "Pattern"},    // label for standard pattern
  102.  *         {"s16", new Dimension(1,3)}    // real object, not just string
  103.  *     // END OF MATERIAL TO LOCALIZE
  104.  *     };
  105.  * }
  106.  * </pre>
  107.  * </blockquote>
  108.  * @see ResourceBundle
  109.  * @see PropertyResourceBundle
  110.  */
  111. public abstract class ListResourceBundle extends ResourceBundle {
  112.     /**
  113.      * Override of ResourceBundle, same semantics
  114.      */
  115.     public final Object handleGetObject(String key) {
  116.         // lazily load the lookup hashtable.
  117.         if (lookup == null) {
  118.             loadLookup();
  119.         }
  120.         return lookup.get(key); // this class ignores locales
  121.     }
  122.  
  123.     /**
  124.      * Implementation of ResourceBundle.getKeys.
  125.      */
  126.     public Enumeration getKeys() {
  127.         // lazily load the lookup hashtable.
  128.         if (lookup == null) {
  129.             loadLookup();
  130.         }
  131.     Enumeration result = null;
  132.     if (parent != null) {
  133.         Hashtable temp = new Hashtable();
  134.         for (Enumeration parentKeys = parent.getKeys() ;
  135.          parentKeys.hasMoreElements() ; /* nothing */) {
  136.         temp.put(parentKeys.nextElement(), this);
  137.         }
  138.         for (Enumeration thisKeys = lookup.keys();
  139.          thisKeys.hasMoreElements() ; /* nothing */) {
  140.         temp.put(thisKeys.nextElement(), this);
  141.         }
  142.         result = temp.keys();
  143.     } else {
  144.         result = lookup.keys();
  145.     }
  146.         return result;
  147.     }
  148.  
  149.     /**
  150.      * See class description.
  151.      */
  152.     abstract protected Object[][] getContents();
  153.  
  154.     // ==================privates====================
  155.  
  156.     /**
  157.      * We lazily load the lookup hashtable.  This function does the
  158.      * loading.
  159.      */
  160.     private void loadLookup() {
  161.         Object[][] contents = getContents();
  162.         lookup = new Hashtable(contents.length);
  163.         for (int i = 0; i < contents.length; ++i) {
  164.             lookup.put(contents[i][0],contents[i][1]);
  165.         }        
  166.     }
  167.     
  168.     private Hashtable lookup = null;
  169. }
  170.