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