KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > accessibility > AccessibleStateSet


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
8 package javax.accessibility;
9
10 import java.util.Vector JavaDoc;
11 import java.util.Locale JavaDoc;
12 import java.util.MissingResourceException JavaDoc;
13 import java.util.ResourceBundle JavaDoc;
14
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     /**
29      * Each entry in the Vector represents an AccessibleState.
30      * @see #add
31      * @see #addAll
32      * @see #remove
33      * @see #contains
34      * @see #toArray
35      * @see #clear
36      */

37     protected Vector JavaDoc<AccessibleState JavaDoc> states = null;
38
39     /**
40      * Creates a new empty state set.
41      */

42     public AccessibleStateSet() {
43         states = null;
44     }
45
46     /**
47      * Creates a new state with the initial set of states contained in
48      * the array of states passed in. Duplicate entries are ignored.
49      *
50      * @param states an array of AccessibleState describing the state set.
51      */

52     public AccessibleStateSet(AccessibleState JavaDoc[] states) {
53         if (states.length != 0) {
54             this.states = new Vector JavaDoc(states.length);
55             for (int i = 0; i < states.length; i++) {
56                 if (!this.states.contains(states[i])) {
57                     this.states.addElement(states[i]);
58                 }
59             }
60         }
61     }
62
63     /**
64      * Adds a new state to the current state set if it is not already
65      * present. If the state is already in the state set, the state
66      * set is unchanged and the return value is false. Otherwise,
67      * the state is added to the state set and the return value is
68      * true.
69      * @param state the state to add to the state set
70      * @return true if state is added to the state set; false if the state set
71      * is unchanged
72      */

73     public boolean add(AccessibleState JavaDoc state) {
74         // [[[ PENDING: WDW - the implementation of this does not need
75
// to always use a vector of states. It could be improved by
76
// caching the states as a bit set.]]]
77
if (states == null) {
78             states = new Vector JavaDoc();
79         }
80
81         if (!states.contains(state)) {
82             states.addElement(state);
83             return true;
84         } else {
85             return false;
86         }
87     }
88
89     /**
90      * Adds all of the states to the existing state set. Duplicate entries
91      * are ignored.
92      * @param states AccessibleState array describing the state set.
93      */

94     public void addAll(AccessibleState JavaDoc[] states) {
95         if (states.length != 0) {
96             if (this.states == null) {
97         this.states = new Vector JavaDoc(states.length);
98             }
99             for (int i = 0; i < states.length; i++) {
100                 if (!this.states.contains(states[i])) {
101                     this.states.addElement(states[i]);
102                 }
103             }
104         }
105     }
106
107     /**
108      * Removes a state from the current state set. If the state is not
109      * in the set, the state set will be unchanged and the return value
110      * will be false. If the state is in the state set, it will be removed
111      * from the set and the return value will be true.
112      *
113      * @param state the state to remove from the state set
114      * @return true if the state is in the state set; false if the state set
115      * will be unchanged
116      */

117     public boolean remove(AccessibleState JavaDoc state) {
118         if (states == null) {
119             return false;
120         } else {
121             return states.removeElement(state);
122         }
123     }
124
125     /**
126      * Removes all the states from the current state set.
127      */

128     public void clear() {
129         if (states != null) {
130             states.removeAllElements();
131         }
132     }
133
134     /**
135      * Checks if the current state is in the state set.
136      * @param state the state
137      * @return true if the state is in the state set; otherwise false
138      */

139     public boolean contains(AccessibleState JavaDoc state) {
140         if (states == null) {
141             return false;
142         } else {
143             return states.contains(state);
144         }
145     }
146
147     /**
148      * Returns the current state set as an array of AccessibleState
149      * @return AccessibleState array containing the current state.
150      */

151     public AccessibleState JavaDoc[] toArray() {
152         if (states == null) {
153             return new AccessibleState JavaDoc[0];
154         } else {
155             AccessibleState JavaDoc[] stateArray = new AccessibleState JavaDoc[states.size()];
156             for (int i = 0; i < stateArray.length; i++) {
157                 stateArray[i] = (AccessibleState JavaDoc) states.elementAt(i);
158             }
159             return stateArray;
160         }
161     }
162
163     /**
164      * Creates a localized String representing all the states in the set
165      * using the default locale.
166      *
167      * @return comma separated localized String
168      * @see AccessibleBundle#toDisplayString
169      */

170     public String JavaDoc toString() {
171         String JavaDoc ret = null;
172         if ((states != null) && (states.size() > 0)) {
173             ret = ((AccessibleState JavaDoc) (states.elementAt(0))).toDisplayString();
174             for (int i = 1; i < states.size(); i++) {
175                 ret = ret + ","
176                         + ((AccessibleState JavaDoc) (states.elementAt(i))).
177                           toDisplayString();
178             }
179         }
180         return ret;
181     }
182 }
183
Popular Tags