Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/accessibility/AccessibleRelationSet.java
41153 views
1
/*
2
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.accessibility;
27
28
import java.util.Vector;
29
30
/**
31
* Class {@code AccessibleRelationSet} determines a component's relation set.
32
* The relation set of a component is a set of {@code AccessibleRelation}
33
* objects that describe the component's relationships with other components.
34
*
35
* @author Lynn Monsanto
36
* @see AccessibleRelation
37
* @since 1.3
38
*/
39
public class AccessibleRelationSet {
40
41
/**
42
* Each entry in the {@code Vector} represents an
43
* {@code AccessibleRelation}.
44
*
45
* @see #add
46
* @see #addAll
47
* @see #remove
48
* @see #contains
49
* @see #get
50
* @see #size
51
* @see #toArray
52
* @see #clear
53
*/
54
protected Vector<AccessibleRelation> relations = null;
55
56
/**
57
* Creates a new empty relation set.
58
*/
59
public AccessibleRelationSet() {
60
relations = null;
61
}
62
63
/**
64
* Creates a new relation with the initial set of relations contained in the
65
* array of relations passed in. Duplicate entries are ignored.
66
*
67
* @param relations an array of {@code AccessibleRelation} describing the
68
* relation set
69
*/
70
public AccessibleRelationSet(AccessibleRelation[] relations) {
71
if (relations.length != 0) {
72
this.relations = new Vector<>(relations.length);
73
for (int i = 0; i < relations.length; i++) {
74
add(relations[i]);
75
}
76
}
77
}
78
79
/**
80
* Adds a new relation to the current relation set. If the relation is
81
* already in the relation set, the target(s) of the specified relation is
82
* merged with the target(s) of the existing relation. Otherwise, the new
83
* relation is added to the relation set.
84
*
85
* @param relation the relation to add to the relation set
86
* @return {@code true} if relation is added to the relation set;
87
* {@code false} if the relation set is unchanged
88
*/
89
public boolean add(AccessibleRelation relation) {
90
if (relations == null) {
91
relations = new Vector<>();
92
}
93
94
// Merge the relation targets if the key exists
95
AccessibleRelation existingRelation = get(relation.getKey());
96
if (existingRelation == null) {
97
relations.addElement(relation);
98
return true;
99
} else {
100
Object [] existingTarget = existingRelation.getTarget();
101
Object [] newTarget = relation.getTarget();
102
int mergedLength = existingTarget.length + newTarget.length;
103
Object [] mergedTarget = new Object[mergedLength];
104
for (int i = 0; i < existingTarget.length; i++) {
105
mergedTarget[i] = existingTarget[i];
106
}
107
for (int i = existingTarget.length, j = 0;
108
i < mergedLength;
109
i++, j++) {
110
mergedTarget[i] = newTarget[j];
111
}
112
existingRelation.setTarget(mergedTarget);
113
}
114
return true;
115
}
116
117
/**
118
* Adds all of the relations to the existing relation set. Duplicate entries
119
* are ignored.
120
*
121
* @param relations {@code AccessibleRelation} array describing the
122
* relation set
123
*/
124
public void addAll(AccessibleRelation[] relations) {
125
if (relations.length != 0) {
126
if (this.relations == null) {
127
this.relations = new Vector<>(relations.length);
128
}
129
for (int i = 0; i < relations.length; i++) {
130
add(relations[i]);
131
}
132
}
133
}
134
135
/**
136
* Removes a relation from the current relation set. If the relation is not
137
* in the set, the relation set will be unchanged and the return value will
138
* be {@code false}. If the relation is in the relation set, it will be
139
* removed from the set and the return value will be {@code true}.
140
*
141
* @param relation the relation to remove from the relation set
142
* @return {@code true} if the relation is in the relation set;
143
* {@code false} if the relation set is unchanged
144
*/
145
public boolean remove(AccessibleRelation relation) {
146
if (relations == null) {
147
return false;
148
} else {
149
return relations.removeElement(relation);
150
}
151
}
152
153
/**
154
* Removes all the relations from the current relation set.
155
*/
156
public void clear() {
157
if (relations != null) {
158
relations.removeAllElements();
159
}
160
}
161
162
/**
163
* Returns the number of relations in the relation set.
164
*
165
* @return 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 that matches the
177
* specified key.
178
*
179
* @param key the {@code AccessibleRelation} key
180
* @return {@code true} if the relation is in the relation set; otherwise
181
* {@code false}
182
*/
183
public boolean contains(String key) {
184
return get(key) != null;
185
}
186
187
/**
188
* Returns the relation that matches the specified key.
189
*
190
* @param key the {@code AccessibleRelation} key
191
* @return the relation, if one exists, that matches the specified key.
192
* Otherwise, {@code null} is returned.
193
*/
194
public AccessibleRelation get(String key) {
195
if (relations == null) {
196
return null;
197
} else {
198
int len = relations.size();
199
for (int i = 0; i < len; i++) {
200
AccessibleRelation relation = relations.elementAt(i);
201
if (relation != null && relation.getKey().equals(key)) {
202
return relation;
203
}
204
}
205
return null;
206
}
207
}
208
209
/**
210
* Returns the current relation set as an array of
211
* {@code AccessibleRelation}.
212
*
213
* @return {@code AccessibleRelation} array contacting the current relation
214
*/
215
public AccessibleRelation[] toArray() {
216
if (relations == null) {
217
return new AccessibleRelation[0];
218
} else {
219
AccessibleRelation[] relationArray
220
= new AccessibleRelation[relations.size()];
221
for (int i = 0; i < relationArray.length; i++) {
222
relationArray[i] = relations.elementAt(i);
223
}
224
return relationArray;
225
}
226
}
227
228
/**
229
* Creates a localized string representing all the relations in the set
230
* using the default locale.
231
*
232
* @return comma separated localized string
233
* @see AccessibleBundle#toDisplayString
234
*/
235
public String toString() {
236
String ret = "";
237
if ((relations != null) && (relations.size() > 0)) {
238
ret = (relations.elementAt(0)).toDisplayString();
239
for (int i = 1; i < relations.size(); i++) {
240
ret = ret + ","
241
+ (relations.elementAt(i)).toDisplayString();
242
}
243
}
244
return ret;
245
}
246
}
247
248