1. /*
  2. * @(#)AccessibleBundle.java 1.16 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.accessibility;
  11. import java.util.Enumeration;
  12. import java.util.Hashtable;
  13. import java.util.Vector;
  14. import java.util.Locale;
  15. import java.util.MissingResourceException;
  16. import java.util.ResourceBundle;
  17. /**
  18. * <p>Base class used to maintain a strongly typed enumeration. This is
  19. * the superclass of {@link AccessibleState} and {@link AccessibleRole}.
  20. * <p>The toDisplayString method allows you to obtain the localized string
  21. * for a locale independent key from a predefined ResourceBundle for the
  22. * keys defined in this class. This localized string is intended to be
  23. * readable by humans.
  24. *
  25. * @see AccessibleRole
  26. * @see AccessibleState
  27. *
  28. * @version 1.12 10/05/99
  29. * @author Willie Walker
  30. * @author Peter Korn
  31. * @author Lynn Monsanto
  32. */
  33. public abstract class AccessibleBundle {
  34. private static Hashtable table = null;
  35. private final String defaultResourceBundleName
  36. = "javax.accessibility.resources.accessibility";
  37. /**
  38. * The locale independent name of the state. This is a programmatic
  39. * name that is not intended to be read by humans.
  40. * @see #toDisplayString
  41. */
  42. protected String key = null;
  43. /**
  44. * Obtains the key as a localized string.
  45. * If a localized string cannot be found for the key, the
  46. * locale independent key stored in the role will be returned.
  47. * This method is intended to be used only by subclasses so that they
  48. * can specify their own resource bundles which contain localized
  49. * strings for their keys.
  50. * @param resourceBundleName the name of the resource bundle to use for
  51. * lookup
  52. * @param locale the locale for which to obtain a localized string
  53. * @return a localized String for the key.
  54. */
  55. protected String toDisplayString(String resourceBundleName,
  56. Locale locale) {
  57. // loads the resource bundle if necessary
  58. loadResourceBundle(resourceBundleName, locale);
  59. // returns the localized string
  60. Object o = table.get(key);
  61. if (o != null && o instanceof String) {
  62. return (String)o;
  63. }
  64. return key;
  65. }
  66. /**
  67. * Obtains the key as a localized string.
  68. * If a localized string cannot be found for the key, the
  69. * locale independent key stored in the role will be returned.
  70. *
  71. * @param locale the locale for which to obtain a localized string
  72. * @return a localized String for the key.
  73. */
  74. public String toDisplayString(Locale locale) {
  75. return toDisplayString(defaultResourceBundleName, locale);
  76. }
  77. /**
  78. * Gets localized string describing the key using the default locale.
  79. * @return a localized String describing the key for the default locale
  80. */
  81. public String toDisplayString() {
  82. return toDisplayString(Locale.getDefault());
  83. }
  84. /**
  85. * Gets localized string describing the key using the default locale.
  86. * @return a localized String describing the key using the default locale
  87. * @see #toDisplayString
  88. */
  89. public String toString() {
  90. return toDisplayString();
  91. }
  92. /*
  93. * Loads the Accessibility resource bundle if necessary.
  94. */
  95. private void loadResourceBundle(String resourceBundleName,
  96. Locale locale) {
  97. if (table == null) {
  98. try {
  99. table = new Hashtable();
  100. ResourceBundle bundle
  101. = ResourceBundle.getBundle(resourceBundleName,
  102. locale);
  103. Enumeration iter = bundle.getKeys();
  104. while(iter.hasMoreElements()) {
  105. String key = (String)iter.nextElement();
  106. table.put(key, bundle.getObject(key));
  107. }
  108. } catch (MissingResourceException e) {
  109. // Just return so toDisplayString() returns the
  110. // non-localized key.
  111. return;
  112. }
  113. }
  114. }
  115. }