1 | /* AccessibleRelationSet.java -- the combined relations of an accessible object
|
---|
2 | Copyright (C) 2002 Free Software Foundation
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package javax.accessibility;
|
---|
39 |
|
---|
40 | import java.util.Vector;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Describes all relations of an accessible object. For example, an object
|
---|
44 | * by labeled by one object and control another.
|
---|
45 | *
|
---|
46 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
47 | * @see AccessibleRelation
|
---|
48 | * @since 1.2
|
---|
49 | * @status updated to 1.4
|
---|
50 | */
|
---|
51 | public class AccessibleRelationSet
|
---|
52 | {
|
---|
53 | /**
|
---|
54 | * The list of relations, should be instances of AccessibleRelation. Don't
|
---|
55 | * set this to null.
|
---|
56 | *
|
---|
57 | * @see #add(AccessibleRelation)
|
---|
58 | * @see #addAll(AccessibleRelation[])
|
---|
59 | * @see #remove(AccessibleRelation)
|
---|
60 | * @see #contains(String)
|
---|
61 | * @see #get(String)
|
---|
62 | * @see #size()
|
---|
63 | * @see #toArray()
|
---|
64 | * @see #clear()
|
---|
65 | */
|
---|
66 | protected Vector relations = new Vector();
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Create an empty relation set.
|
---|
70 | */
|
---|
71 | public AccessibleRelationSet()
|
---|
72 | {
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Create a relation set initialized with the given relations, duplicates are
|
---|
77 | * ignored.
|
---|
78 | *
|
---|
79 | * @param relations the relations to insert
|
---|
80 | * @throws NullPointerException if relations is null
|
---|
81 | */
|
---|
82 | public AccessibleRelationSet(AccessibleRelation[] relations)
|
---|
83 | {
|
---|
84 | addAll(relations);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Add a new relation to the current set. If the relation is already in
|
---|
89 | * the set, the targets are merged with the existing relation, possibly
|
---|
90 | * resulting in an object being in the target list more than once. Do not
|
---|
91 | * add a relation with a null key, as it will cause problems later.
|
---|
92 | *
|
---|
93 | * @param relation the relation to add
|
---|
94 | * @return true if the set was modified, which is always the case
|
---|
95 | * @throws NullPointerException if relation is null
|
---|
96 | */
|
---|
97 | public boolean add(AccessibleRelation relation)
|
---|
98 | {
|
---|
99 | AccessibleRelation old = get(relation.key);
|
---|
100 | if (old == null)
|
---|
101 | return relations.add(relation);
|
---|
102 | if (old.targets.length == 0)
|
---|
103 | old.targets = relation.targets;
|
---|
104 | else if (relation.targets.length != 0)
|
---|
105 | {
|
---|
106 | Object[] t = new Object[old.targets.length + relation.targets.length];
|
---|
107 | System.arraycopy(old.targets, 0, t, 0, old.targets.length);
|
---|
108 | System.arraycopy(relation.targets, 0, t, old.targets.length,
|
---|
109 | relation.targets.length);
|
---|
110 | old.targets = t;
|
---|
111 | }
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Add all of the relations to the current set. Duplicates are ignored.
|
---|
117 | *
|
---|
118 | * @param array the array of relations to add
|
---|
119 | * @throws NullPointerException if array is null or has null entries
|
---|
120 | */
|
---|
121 | public void addAll(AccessibleRelation[] array)
|
---|
122 | {
|
---|
123 | int i = array.length;
|
---|
124 | while (--i >= 0)
|
---|
125 | add(array[i]);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Remove a relation from the set. If a relation was removed, return true.
|
---|
130 | * Note that this uses AccessibleRelation.equals, which defaults to ==, so a
|
---|
131 | * relation with the same key may still exist in the set afterwords.
|
---|
132 | *
|
---|
133 | * @param relation the state to remove
|
---|
134 | * @return true if the set changed
|
---|
135 | */
|
---|
136 | public boolean remove(AccessibleRelation relation)
|
---|
137 | {
|
---|
138 | return relations.remove(relation);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Clear all relations in the set.
|
---|
143 | */
|
---|
144 | public void clear()
|
---|
145 | {
|
---|
146 | relations.clear();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Return the number of relations in the set.
|
---|
151 | *
|
---|
152 | * @return the set size
|
---|
153 | */
|
---|
154 | public int size()
|
---|
155 | {
|
---|
156 | return relations.size();
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Check if the relation key is in the set.
|
---|
161 | *
|
---|
162 | * @param relation the relation to locate
|
---|
163 | * @return true if it is in the set
|
---|
164 | */
|
---|
165 | public boolean contains(String key)
|
---|
166 | {
|
---|
167 | int i = relations.size();
|
---|
168 | while (--i >= 0)
|
---|
169 | if (((AccessibleRelation) relations.get(i)).key.equals(key))
|
---|
170 | return true;
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Get the relation that matches the key.
|
---|
176 | *
|
---|
177 | * @param relation the relation to locate
|
---|
178 | * @return the relation in the set, or null
|
---|
179 | */
|
---|
180 | public AccessibleRelation get(String key)
|
---|
181 | {
|
---|
182 | int i = relations.size();
|
---|
183 | while (--i >= 0)
|
---|
184 | {
|
---|
185 | AccessibleRelation r = (AccessibleRelation) relations.get(i);
|
---|
186 | if (r.key.equals(key))
|
---|
187 | return r;
|
---|
188 | }
|
---|
189 | return null;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Return the relation set as an array.
|
---|
194 | *
|
---|
195 | * @return an array of the current relations
|
---|
196 | */
|
---|
197 | public AccessibleRelation[] toArray()
|
---|
198 | {
|
---|
199 | AccessibleRelation[] result = new AccessibleRelation[relations.size()];
|
---|
200 | relations.toArray(result);
|
---|
201 | return result;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Return a localized, comma-separated string representing all relations
|
---|
206 | * in the set. This is in arbitrary order.
|
---|
207 | *
|
---|
208 | * @return the string representation
|
---|
209 | * @see AccessibleBundle#toDisplayString(String, Locale)
|
---|
210 | */
|
---|
211 | public String toString()
|
---|
212 | {
|
---|
213 | int i = relations.size();
|
---|
214 | if (i == 0)
|
---|
215 | return "";
|
---|
216 | // Pre-allocate an average of 10 chars per state.
|
---|
217 | StringBuffer b = new StringBuffer(i * 10);
|
---|
218 | while (--i >= 0)
|
---|
219 | b.append(relations.get(i)).append(',');
|
---|
220 | return b.substring(0, b.length() - 1);
|
---|
221 | }
|
---|
222 | } // class AccessibleRelationSet
|
---|