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