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