1. /*
  2. * @(#)AccessibleStateSet.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. * Class AccessibleStateSet determines a components state set. The state set
  14. * of a component is a set of AccessibleState objects and descriptions the
  15. * current overall state of the object, such as whether it is enabled,
  16. * has focus, etc.
  17. *
  18. * @see AccessibleState
  19. *
  20. * @version 1.10 11/29/01 23:09:29
  21. * @author Willie Walker
  22. */
  23. public class AccessibleStateSet {
  24. /**
  25. * Each entry in the Vector represents an AccessibleState.
  26. * @see #add
  27. * @see #addAll
  28. * @see #remove
  29. * @see #contains
  30. * @see #toArray
  31. * @see #clear
  32. */
  33. protected Vector states = null;
  34. /**
  35. * Create a new empty state set.
  36. */
  37. public AccessibleStateSet() {
  38. states = null;
  39. }
  40. /**
  41. * Create a new state with the initial set of states contained in
  42. * the array of states passed in. Duplicate entries are ignored.
  43. * @param state an array of AccessibleState describing the state set.
  44. */
  45. public AccessibleStateSet(AccessibleState[] states) {
  46. if (states.length != 0) {
  47. this.states = new Vector(states.length);
  48. for (int i = 0; i < states.length; i++) {
  49. if (!this.states.contains(states[i])) {
  50. this.states.addElement(states[i]);
  51. }
  52. }
  53. }
  54. }
  55. /**
  56. * Add a new state to the current state set if it is not already
  57. * present. If the state is already in the state set, the state
  58. * set is unchanged and the return value is false. Otherwise,
  59. * the state is added to the state set and the return value is
  60. * true.
  61. * @param state the state to add to the state set
  62. * @return true if state is added to the state set; false if the state set
  63. * is unchanged
  64. */
  65. public boolean add(AccessibleState state) {
  66. // [[[ PENDING: WDW - the implementation of this does not need
  67. // to always use a vector of states. It could be improved by
  68. // caching the states as a bit set.]]]
  69. if (states == null) {
  70. states = new Vector();
  71. }
  72. if (!states.contains(state)) {
  73. states.addElement(state);
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Add all of the states to the existing state set. Duplicate entries
  81. * are ignored.
  82. * @param state AccessibleState array describing the state set.
  83. */
  84. public void addAll(AccessibleState[] states) {
  85. if (states.length != 0) {
  86. if (this.states == null) {
  87. this.states = new Vector(states.length);
  88. }
  89. for (int i = 0; i < states.length; i++) {
  90. if (!this.states.contains(states[i])) {
  91. this.states.addElement(states[i]);
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Remove a state from the current state set. If the state is not
  98. * in the set, the state set will be unchanged and the return value
  99. * will be false. If the state is in the state set, it will be removed
  100. * from the set and the return value will be true.
  101. *
  102. * @param state the state to remove from the state set
  103. * @return true is the state is in the state set; false if the state set
  104. * will be unchanged
  105. */
  106. public boolean remove(AccessibleState state) {
  107. if (states == null) {
  108. return false;
  109. } else {
  110. return states.removeElement(state);
  111. }
  112. }
  113. /**
  114. * Remove all the states from the current state set.
  115. */
  116. public void clear() {
  117. if (states != null) {
  118. states.removeAllElements();
  119. }
  120. }
  121. /**
  122. * Check if the current state is in the state set.
  123. * @param state the state
  124. * @return true if the state is in the state set; otherwise false
  125. */
  126. public boolean contains(AccessibleState state) {
  127. if (states == null) {
  128. return false;
  129. } else {
  130. return states.contains(state);
  131. }
  132. }
  133. /**
  134. * Returns the current state set as an array of AccessibleState
  135. * @return AccessibleState array conatining the current state.
  136. */
  137. public AccessibleState[] toArray() {
  138. if (states == null) {
  139. return new AccessibleState[0];
  140. } else {
  141. AccessibleState[] stateArray = new AccessibleState[states.size()];
  142. for (int i = 0; i < stateArray.length; i++) {
  143. stateArray[i] = (AccessibleState) states.elementAt(i);
  144. }
  145. return stateArray;
  146. }
  147. }
  148. /**
  149. * Create a localized String representing all the states in the set
  150. * using the default locale.
  151. *
  152. * @return comma separated localized String
  153. * @see AccessibleBundle#toDisplayString
  154. */
  155. public String toString() {
  156. String ret = null;
  157. if ((states != null) && (states.size() > 0)) {
  158. ret = ((AccessibleState) (states.elementAt(0))).toDisplayString();
  159. for (int i = 1; i < states.size(); i++) {
  160. ret = ret + ","
  161. + ((AccessibleState) (states.elementAt(i))).
  162. toDisplayString();
  163. }
  164. }
  165. return ret;
  166. }
  167. }