KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > accessibility > AccessibleRelationSet


1 /*
2  * @(#)AccessibleRelationSet.java 1.13 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 AccessibleRelationSet determines a component's relation set. The
17  * relation set of a component is a set of AccessibleRelation objects that
18  * describe the component's relationships with other components.
19  *
20  * @see AccessibleRelation
21  *
22  * @version 1.13 05/05/04
23  * @author Lynn Monsanto
24  */

25 public class AccessibleRelationSet {
26
27     /**
28      * Each entry in the Vector represents an AccessibleRelation.
29      * @see #add
30      * @see #addAll
31      * @see #remove
32      * @see #contains
33      * @see #get
34      * @see #size
35      * @see #toArray
36      * @see #clear
37      */

38     protected Vector JavaDoc<AccessibleRelation JavaDoc> relations = null;
39
40     /**
41      * Creates a new empty relation set.
42      */

43     public AccessibleRelationSet() {
44         relations = null;
45     }
46
47     /**
48      * Creates a new relation with the initial set of relations contained in
49      * the array of relations passed in. Duplicate entries are ignored.
50      *
51      * @param relations an array of AccessibleRelation describing the
52      * relation set.
53      */

54     public AccessibleRelationSet(AccessibleRelation JavaDoc[] relations) {
55         if (relations.length != 0) {
56             this.relations = new Vector JavaDoc(relations.length);
57             for (int i = 0; i < relations.length; i++) {
58         add(relations[i]);
59             }
60         }
61     }
62
63     /**
64      * Adds a new relation to the current relation set. If the relation
65      * is already in the relation set, the target(s) of the specified
66      * relation is merged with the target(s) of the existing relation.
67      * Otherwise, the new relation is added to the relation set.
68      *
69      * @param relation the relation to add to the relation set
70      * @return true if relation is added to the relation set; false if the
71      * relation set is unchanged
72      */

73     public boolean add(AccessibleRelation JavaDoc relation) {
74         if (relations == null) {
75             relations = new Vector JavaDoc();
76         }
77
78     // Merge the relation targets if the key exists
79
AccessibleRelation JavaDoc existingRelation = get(relation.getKey());
80     if (existingRelation == null) {
81             relations.addElement(relation);
82             return true;
83         } else {
84         Object JavaDoc [] existingTarget = existingRelation.getTarget();
85         Object JavaDoc [] newTarget = relation.getTarget();
86         int mergedLength = existingTarget.length + newTarget.length;
87         Object JavaDoc [] mergedTarget = new Object JavaDoc[mergedLength];
88         for (int i = 0; i < existingTarget.length; i++) {
89         mergedTarget[i] = existingTarget[i];
90         }
91         for (int i = existingTarget.length, j = 0;
92          i < mergedLength;
93          i++, j++) {
94         mergedTarget[i] = newTarget[j];
95         }
96         existingRelation.setTarget(mergedTarget);
97         }
98     return true;
99     }
100
101     /**
102      * Adds all of the relations to the existing relation set. Duplicate
103      * entries are ignored.
104      *
105      * @param relations AccessibleRelation array describing the relation set.
106      */

107     public void addAll(AccessibleRelation JavaDoc[] relations) {
108         if (relations.length != 0) {
109             if (this.relations == null) {
110         this.relations = new Vector JavaDoc(relations.length);
111             }
112             for (int i = 0; i < relations.length; i++) {
113         add(relations[i]);
114             }
115         }
116     }
117
118     /**
119      * Removes a relation from the current relation set. If the relation
120      * is not in the set, the relation set will be unchanged and the
121      * return value will be false. If the relation is in the relation
122      * set, it will be removed from the set and the return value will be
123      * true.
124      *
125      * @param relation the relation to remove from the relation set
126      * @return true if the relation is in the relation set; false if the
127      * relation set is unchanged
128      */

129     public boolean remove(AccessibleRelation JavaDoc relation) {
130         if (relations == null) {
131             return false;
132         } else {
133             return relations.removeElement(relation);
134         }
135     }
136
137     /**
138      * Removes all the relations from the current relation set.
139      */

140     public void clear() {
141         if (relations != null) {
142             relations.removeAllElements();
143         }
144     }
145
146     /**
147      * Returns the number of relations in the relation set.
148      */

149     public int size() {
150     if (relations == null) {
151         return 0;
152     } else {
153         return relations.size();
154     }
155     }
156
157     /**
158      * Returns whether the relation set contains a relation
159      * that matches the specified key.
160      * @param key the AccessibleRelation key
161      * @return true if the relation is in the relation set; otherwise false
162      */

163     public boolean contains(String JavaDoc key) {
164     return get(key) != null;
165     }
166
167     /**
168      * Returns the relation that matches the specified key.
169      * @param key the AccessibleRelation key
170      * @return the relation, if one exists, that matches the specified key.
171      * Otherwise, null is returned.
172      */

173     public AccessibleRelation JavaDoc get(String JavaDoc key) {
174         if (relations == null) {
175             return null;
176         } else {
177         int len = relations.size();
178         for (int i = 0; i < len; i++) {
179         AccessibleRelation JavaDoc relation =
180             (AccessibleRelation JavaDoc)relations.elementAt(i);
181         if (relation != null && relation.getKey().equals(key)) {
182             return relation;
183         }
184         }
185             return null;
186         }
187     }
188
189     /**
190      * Returns the current relation set as an array of AccessibleRelation
191      * @return AccessibleRelation array contacting the current relation.
192      */

193     public AccessibleRelation JavaDoc[] toArray() {
194         if (relations == null) {
195             return new AccessibleRelation JavaDoc[0];
196         } else {
197             AccessibleRelation JavaDoc[] relationArray
198         = new AccessibleRelation JavaDoc[relations.size()];
199             for (int i = 0; i < relationArray.length; i++) {
200                 relationArray[i] = (AccessibleRelation JavaDoc) relations.elementAt(i);
201             }
202             return relationArray;
203         }
204     }
205
206     /**
207      * Creates a localized String representing all the relations in the set
208      * using the default locale.
209      *
210      * @return comma separated localized String
211      * @see AccessibleBundle#toDisplayString
212      */

213     public String JavaDoc toString() {
214         String JavaDoc ret = "";
215         if ((relations != null) && (relations.size() > 0)) {
216             ret = ((AccessibleRelation JavaDoc) (relations.elementAt(0))).toDisplayString();
217             for (int i = 1; i < relations.size(); i++) {
218                 ret = ret + ","
219                         + ((AccessibleRelation JavaDoc) (relations.elementAt(i))).
220                           toDisplayString();
221             }
222         }
223         return ret;
224     }
225 }
226
Popular Tags