1. /*
  2. * @(#)AccessibleBundle.java 1.10 01/11/29
  3. *
  4. * Copyright 2002 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.Vector;
  9. import java.util.Locale;
  10. import java.util.MissingResourceException;
  11. import java.util.ResourceBundle;
  12. /**
  13. * <p>Base class used to maintain a strongly typed enumeration. This is
  14. * the superclass of {@link AccessibleState} and {@link AccessibleRole}.
  15. * <p>The toDisplayString method allows you to obtain the localized string
  16. * for a locale independent key from a predefined ResourceBundle for the
  17. * keys defined in this class. This localized string is intended to be
  18. * readable by humans.
  19. *
  20. * @see AccessibleRole
  21. * @see AccessibleState
  22. *
  23. * @version 1.10 11/29/01 23:09:22
  24. * @author Willie Walker
  25. * @author Peter Korn
  26. */
  27. public abstract class AccessibleBundle {
  28. /**
  29. * The locale independent name of the state. This is a programmatic
  30. * name that is not intended to be read by humans.
  31. * @see #toDisplayString
  32. */
  33. protected String key = null;
  34. /**
  35. * Obtain the key as a localized string.
  36. * If a localized string cannot be found for the key, the
  37. * locale independent key stored in the role will be returned.
  38. * This method is intended to be used only by subclasses so that they
  39. * can specify their own resource bundles which contain localized
  40. * strings for their keys.
  41. * @param resourceBundleName the name of the resource bundle to use for
  42. * lookup
  43. * @param locale the locale for which to obtain a localized string
  44. * @return a localized String for the key.
  45. */
  46. protected String toDisplayString(String resourceBundleName,
  47. Locale locale) {
  48. // [[[FIXME: WDW - obtaining resource bundles can be
  49. // expensive, especially when obtaining them from ASCII
  50. // properties files. A time performace improvement can be
  51. // made here if we cache the resource bundles by locale.
  52. // We probably should also see if ResourceBundle itself
  53. // caches these for us. If it does, it would be nice.]]]
  54. ResourceBundle resources;
  55. String displayString = null;
  56. try {
  57. resources = ResourceBundle.getBundle(resourceBundleName,
  58. locale);
  59. displayString = resources.getString(key);
  60. } catch (MissingResourceException mre) {
  61. System.err.println(mre
  62. + ": " + resourceBundleName + " not found");
  63. }
  64. if (displayString != null) {
  65. return displayString;
  66. } else {
  67. return key;
  68. }
  69. }
  70. /**
  71. * Obtain the key as a localized string.
  72. * If a localized string cannot be found for the key, the
  73. * locale independent key stored in the role will be returned.
  74. *
  75. * @param locale the locale for which to obtain a localized string
  76. * @return a localized String for the key.
  77. */
  78. public String toDisplayString(Locale locale) {
  79. return toDisplayString(
  80. "javax.accessibility.AccessibleResourceBundle",
  81. locale);
  82. }
  83. /**
  84. * Get localized string describing the key using the default locale.
  85. * @return a localized String describing the key for the default locale
  86. */
  87. public String toDisplayString() {
  88. return toDisplayString(Locale.getDefault());
  89. }
  90. /**
  91. * Get localized string describing the key using the default locale.
  92. * @return a localized String describing the key using the default locale
  93. * @see #toDisplayString
  94. */
  95. public String toString() {
  96. return toDisplayString();
  97. }
  98. }