001 /*
002 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.accessibility;
027
028 import java.util.Vector;
029 import java.util.Locale;
030 import java.util.MissingResourceException;
031 import java.util.ResourceBundle;
032
033 /**
034 * Class AccessibleStateSet determines a component's state set. The state set
035 * of a component is a set of AccessibleState objects and descriptions. E.G., The
036 * current overall state of the object, such as whether it is enabled,
037 * has focus, etc.
038 *
039 * @see AccessibleState
040 *
041 * @version 1.10 10/12/99 15:05:34
042 * @author Willie Walker
043 */
044 public class AccessibleStateSet {
045
046 /**
047 * Each entry in the Vector represents an AccessibleState.
048 * @see #add
049 * @see #addAll
050 * @see #remove
051 * @see #contains
052 * @see #toArray
053 * @see #clear
054 */
055 protected Vector<AccessibleState> states = null;
056
057 /**
058 * Creates a new empty state set.
059 */
060 public AccessibleStateSet() {
061 states = null;
062 }
063
064 /**
065 * Creates a new state with the initial set of states contained in
066 * the array of states passed in. Duplicate entries are ignored.
067 *
068 * @param states an array of AccessibleState describing the state set.
069 */
070 public AccessibleStateSet(AccessibleState[] states) {
071 if (states.length != 0) {
072 this .states = new Vector(states.length);
073 for (int i = 0; i < states.length; i++) {
074 if (!this .states.contains(states[i])) {
075 this .states.addElement(states[i]);
076 }
077 }
078 }
079 }
080
081 /**
082 * Adds a new state to the current state set if it is not already
083 * present. If the state is already in the state set, the state
084 * set is unchanged and the return value is false. Otherwise,
085 * the state is added to the state set and the return value is
086 * true.
087 * @param state the state to add to the state set
088 * @return true if state is added to the state set; false if the state set
089 * is unchanged
090 */
091 public boolean add(AccessibleState state) {
092 // [[[ PENDING: WDW - the implementation of this does not need
093 // to always use a vector of states. It could be improved by
094 // caching the states as a bit set.]]]
095 if (states == null) {
096 states = new Vector();
097 }
098
099 if (!states.contains(state)) {
100 states.addElement(state);
101 return true;
102 } else {
103 return false;
104 }
105 }
106
107 /**
108 * Adds all of the states to the existing state set. Duplicate entries
109 * are ignored.
110 * @param states AccessibleState array describing the state set.
111 */
112 public void addAll(AccessibleState[] states) {
113 if (states.length != 0) {
114 if (this .states == null) {
115 this .states = new Vector(states.length);
116 }
117 for (int i = 0; i < states.length; i++) {
118 if (!this .states.contains(states[i])) {
119 this .states.addElement(states[i]);
120 }
121 }
122 }
123 }
124
125 /**
126 * Removes a state from the current state set. If the state is not
127 * in the set, the state set will be unchanged and the return value
128 * will be false. If the state is in the state set, it will be removed
129 * from the set and the return value will be true.
130 *
131 * @param state the state to remove from the state set
132 * @return true if the state is in the state set; false if the state set
133 * will be unchanged
134 */
135 public boolean remove(AccessibleState state) {
136 if (states == null) {
137 return false;
138 } else {
139 return states.removeElement(state);
140 }
141 }
142
143 /**
144 * Removes all the states from the current state set.
145 */
146 public void clear() {
147 if (states != null) {
148 states.removeAllElements();
149 }
150 }
151
152 /**
153 * Checks if the current state is in the state set.
154 * @param state the state
155 * @return true if the state is in the state set; otherwise false
156 */
157 public boolean contains(AccessibleState state) {
158 if (states == null) {
159 return false;
160 } else {
161 return states.contains(state);
162 }
163 }
164
165 /**
166 * Returns the current state set as an array of AccessibleState
167 * @return AccessibleState array containing the current state.
168 */
169 public AccessibleState[] toArray() {
170 if (states == null) {
171 return new AccessibleState[0];
172 } else {
173 AccessibleState[] stateArray = new AccessibleState[states
174 .size()];
175 for (int i = 0; i < stateArray.length; i++) {
176 stateArray[i] = (AccessibleState) states.elementAt(i);
177 }
178 return stateArray;
179 }
180 }
181
182 /**
183 * Creates a localized String representing all the states in the set
184 * using the default locale.
185 *
186 * @return comma separated localized String
187 * @see AccessibleBundle#toDisplayString
188 */
189 public String toString() {
190 String ret = null;
191 if ((states != null) && (states.size() > 0)) {
192 ret = ((AccessibleState) (states.elementAt(0)))
193 .toDisplayString();
194 for (int i = 1; i < states.size(); i++) {
195 ret = ret
196 + ","
197 + ((AccessibleState) (states.elementAt(i)))
198 .toDisplayString();
199 }
200 }
201 return ret;
202 }
203 }
|