1 /*
2 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.accessibility;
27
28 import java.util.Vector;
29 import java.util.Locale;
30 import java.util.MissingResourceException;
31 import java.util.ResourceBundle;
32
33 /**
34 * Class AccessibleRelationSet determines a component's relation set. The
35 * relation set of a component is a set of AccessibleRelation objects that
36 * describe the component's relationships with other components.
37 *
38 * @see AccessibleRelation
39 *
40 * @author Lynn Monsanto
41 * @since 1.3
42 */
43 public class AccessibleRelationSet {
44
45 /**
46 * Each entry in the Vector represents an AccessibleRelation.
47 * @see #add
48 * @see #addAll
49 * @see #remove
50 * @see #contains
51 * @see #get
52 * @see #size
53 * @see #toArray
54 * @see #clear
55 */
56 protected Vector<AccessibleRelation> relations = null;
57
58 /**
59 * Creates a new empty relation set.
60 */
61 public AccessibleRelationSet() {
62 relations = null;
63 }
64
65 /**
66 * Creates a new relation with the initial set of relations contained in
67 * the array of relations passed in. Duplicate entries are ignored.
68 *
69 * @param relations an array of AccessibleRelation describing the
70 * relation set.
71 */
72 public AccessibleRelationSet(AccessibleRelation[] relations) {
73 if (relations.length != 0) {
74 this.relations = new Vector(relations.length);
75 for (int i = 0; i < relations.length; i++) {
76 add(relations[i]);
77 }
78 }
79 }
80
81 /**
82 * Adds a new relation to the current relation set. If the relation
83 * is already in the relation set, the target(s) of the specified
84 * relation is merged with the target(s) of the existing relation.
85 * Otherwise, the new relation is added to the relation set.
86 *
87 * @param relation the relation to add to the relation set
88 * @return true if relation is added to the relation set; false if the
89 * relation set is unchanged
90 */
91 public boolean add(AccessibleRelation relation) {
92 if (relations == null) {
93 relations = new Vector();
94 }
95
96 // Merge the relation targets if the key exists
97 AccessibleRelation existingRelation = get(relation.getKey());
98 if (existingRelation == null) {
99 relations.addElement(relation);
100 return true;
101 } else {
102 Object [] existingTarget = existingRelation.getTarget();
103 Object [] newTarget = relation.getTarget();
104 int mergedLength = existingTarget.length + newTarget.length;
105 Object [] mergedTarget = new Object[mergedLength];
106 for (int i = 0; i < existingTarget.length; i++) {
107 mergedTarget[i] = existingTarget[i];
108 }
109 for (int i = existingTarget.length, j = 0;
110 i < mergedLength;
111 i++, j++) {
112 mergedTarget[i] = newTarget[j];
113 }
114 existingRelation.setTarget(mergedTarget);
115 }
116 return true;
117 }
118
119 /**
120 * Adds all of the relations to the existing relation set. Duplicate
121 * entries are ignored.
122 *
123 * @param relations AccessibleRelation array describing the relation set.
124 */
125 public void addAll(AccessibleRelation[] relations) {
126 if (relations.length != 0) {
127 if (this.relations == null) {
128 this.relations = new Vector(relations.length);
129 }
130 for (int i = 0; i < relations.length; i++) {
131 add(relations[i]);
132 }
133 }
134 }
135
136 /**
137 * Removes a relation from the current relation set. If the relation
138 * is not in the set, the relation set will be unchanged and the
139 * return value will be false. If the relation is in the relation
140 * set, it will be removed from the set and the return value will be
141 * true.
142 *
143 * @param relation the relation to remove from the relation set
144 * @return true if the relation is in the relation set; false if the
145 * relation set is unchanged
146 */
147 public boolean remove(AccessibleRelation relation) {
148 if (relations == null) {
149 return false;
150 } else {
151 return relations.removeElement(relation);
152 }
153 }
154
155 /**
156 * Removes all the relations from the current relation set.
157 */
158 public void clear() {
159 if (relations != null) {
160 relations.removeAllElements();
161 }
162 }
163
164 /**
165 * Returns the number of relations in the relation set.
166 */
167 public int size() {
168 if (relations == null) {
169 return 0;
170 } else {
171 return relations.size();
172 }
173 }
174
175 /**
176 * Returns whether the relation set contains a relation
177 * that matches the specified key.
178 * @param key the AccessibleRelation key
179 * @return true if the relation is in the relation set; otherwise false
180 */
181 public boolean contains(String key) {
182 return get(key) != null;
183 }
184
185 /**
186 * Returns the relation that matches the specified key.
187 * @param key the AccessibleRelation key
188 * @return the relation, if one exists, that matches the specified key.
189 * Otherwise, null is returned.
190 */
191 public AccessibleRelation get(String key) {
192 if (relations == null) {
193 return null;
194 } else {
195 int len = relations.size();
196 for (int i = 0; i < len; i++) {
197 AccessibleRelation relation =
198 (AccessibleRelation)relations.elementAt(i);
199 if (relation != null && relation.getKey().equals(key)) {
200 return relation;
201 }
202 }
203 return null;
204 }
205 }
206
207 /**
208 * Returns the current relation set as an array of AccessibleRelation
209 * @return AccessibleRelation array contacting the current relation.
210 */
211 public AccessibleRelation[] toArray() {
212 if (relations == null) {
213 return new AccessibleRelation[0];
214 } else {
215 AccessibleRelation[] relationArray
216 = new AccessibleRelation[relations.size()];
217 for (int i = 0; i < relationArray.length; i++) {
218 relationArray[i] = (AccessibleRelation) relations.elementAt(i);
219 }
220 return relationArray;
221 }
222 }
223
224 /**
225 * Creates a localized String representing all the relations in the set
226 * using the default locale.
227 *
228 * @return comma separated localized String
229 * @see AccessibleBundle#toDisplayString
230 */
231 public String toString() {
232 String ret = "";
233 if ((relations != null) && (relations.size() > 0)) {
234 ret = ((AccessibleRelation) (relations.elementAt(0))).toDisplayString();
235 for (int i = 1; i < relations.size(); i++) {
236 ret = ret + ","
237 + ((AccessibleRelation) (relations.elementAt(i))).
238 toDisplayString();
239 }
240 }
241 return ret;
242 }
243 }