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. package javax.accessibility;
  8. import java.util.Vector;
  9. import java.util.Locale;
  10. import java.util.MissingResourceException;
  11. import java.util.ResourceBundle;
  12. /**
  13. * Class AccessibleRelationSet determines a component's relation set. The
  14. * relation set of a component is a set of AccessibleRelation objects that
  15. * describe the component's relationships with other components.
  16. *
  17. * @see AccessibleRelation
  18. *
  19. * @version 1.13 05/05/04
  20. * @author Lynn Monsanto
  21. */
  22. public class AccessibleRelationSet {
  23. /**
  24. * Each entry in the Vector represents an AccessibleRelation.
  25. * @see #add
  26. * @see #addAll
  27. * @see #remove
  28. * @see #contains
  29. * @see #get
  30. * @see #size
  31. * @see #toArray
  32. * @see #clear
  33. */
  34. protected Vector<AccessibleRelation> relations = null;
  35. /**
  36. * Creates a new empty relation set.
  37. */
  38. public AccessibleRelationSet() {
  39. relations = null;
  40. }
  41. /**
  42. * Creates a new relation with the initial set of relations contained in
  43. * the array of relations passed in. Duplicate entries are ignored.
  44. *
  45. * @param relations an array of AccessibleRelation describing the
  46. * relation set.
  47. */
  48. public AccessibleRelationSet(AccessibleRelation[] relations) {
  49. if (relations.length != 0) {
  50. this.relations = new Vector(relations.length);
  51. for (int i = 0; i < relations.length; i++) {
  52. add(relations[i]);
  53. }
  54. }
  55. }
  56. /**
  57. * Adds a new relation to the current relation set. If the relation
  58. * is already in the relation set, the target(s) of the specified
  59. * relation is merged with the target(s) of the existing relation.
  60. * Otherwise, the new relation is added to the relation set.
  61. *
  62. * @param relation the relation to add to the relation set
  63. * @return true if relation is added to the relation set; false if the
  64. * relation set is unchanged
  65. */
  66. public boolean add(AccessibleRelation relation) {
  67. if (relations == null) {
  68. relations = new Vector();
  69. }
  70. // Merge the relation targets if the key exists
  71. AccessibleRelation existingRelation = get(relation.getKey());
  72. if (existingRelation == null) {
  73. relations.addElement(relation);
  74. return true;
  75. } else {
  76. Object [] existingTarget = existingRelation.getTarget();
  77. Object [] newTarget = relation.getTarget();
  78. int mergedLength = existingTarget.length + newTarget.length;
  79. Object [] mergedTarget = new Object[mergedLength];
  80. for (int i = 0; i < existingTarget.length; i++) {
  81. mergedTarget[i] = existingTarget[i];
  82. }
  83. for (int i = existingTarget.length, j = 0;
  84. i < mergedLength;
  85. i++, j++) {
  86. mergedTarget[i] = newTarget[j];
  87. }
  88. existingRelation.setTarget(mergedTarget);
  89. }
  90. return true;
  91. }
  92. /**
  93. * Adds all of the relations to the existing relation set. Duplicate
  94. * entries are ignored.
  95. *
  96. * @param relations AccessibleRelation array describing the relation set.
  97. */
  98. public void addAll(AccessibleRelation[] relations) {
  99. if (relations.length != 0) {
  100. if (this.relations == null) {
  101. this.relations = new Vector(relations.length);
  102. }
  103. for (int i = 0; i < relations.length; i++) {
  104. add(relations[i]);
  105. }
  106. }
  107. }
  108. /**
  109. * Removes a relation from the current relation set. If the relation
  110. * is not in the set, the relation set will be unchanged and the
  111. * return value will be false. If the relation is in the relation
  112. * set, it will be removed from the set and the return value will be
  113. * true.
  114. *
  115. * @param relation the relation to remove from the relation set
  116. * @return true if the relation is in the relation set; false if the
  117. * relation set is unchanged
  118. */
  119. public boolean remove(AccessibleRelation relation) {
  120. if (relations == null) {
  121. return false;
  122. } else {
  123. return relations.removeElement(relation);
  124. }
  125. }
  126. /**
  127. * Removes all the relations from the current relation set.
  128. */
  129. public void clear() {
  130. if (relations != null) {
  131. relations.removeAllElements();
  132. }
  133. }
  134. /**
  135. * Returns the number of relations in the relation set.
  136. */
  137. public int size() {
  138. if (relations == null) {
  139. return 0;
  140. } else {
  141. return relations.size();
  142. }
  143. }
  144. /**
  145. * Returns whether the relation set contains a relation
  146. * that matches the specified key.
  147. * @param key the AccessibleRelation key
  148. * @return true if the relation is in the relation set; otherwise false
  149. */
  150. public boolean contains(String key) {
  151. return get(key) != null;
  152. }
  153. /**
  154. * Returns the relation that matches the specified key.
  155. * @param key the AccessibleRelation key
  156. * @return the relation, if one exists, that matches the specified key.
  157. * Otherwise, null is returned.
  158. */
  159. public AccessibleRelation get(String key) {
  160. if (relations == null) {
  161. return null;
  162. } else {
  163. int len = relations.size();
  164. for (int i = 0; i < len; i++) {
  165. AccessibleRelation relation =
  166. (AccessibleRelation)relations.elementAt(i);
  167. if (relation != null && relation.getKey().equals(key)) {
  168. return relation;
  169. }
  170. }
  171. return null;
  172. }
  173. }
  174. /**
  175. * Returns the current relation set as an array of AccessibleRelation
  176. * @return AccessibleRelation array contacting the current relation.
  177. */
  178. public AccessibleRelation[] toArray() {
  179. if (relations == null) {
  180. return new AccessibleRelation[0];
  181. } else {
  182. AccessibleRelation[] relationArray
  183. = new AccessibleRelation[relations.size()];
  184. for (int i = 0; i < relationArray.length; i++) {
  185. relationArray[i] = (AccessibleRelation) relations.elementAt(i);
  186. }
  187. return relationArray;
  188. }
  189. }
  190. /**
  191. * Creates a localized String representing all the relations in the set
  192. * using the default locale.
  193. *
  194. * @return comma separated localized String
  195. * @see AccessibleBundle#toDisplayString
  196. */
  197. public String toString() {
  198. String ret = "";
  199. if ((relations != null) && (relations.size() > 0)) {
  200. ret = ((AccessibleRelation) (relations.elementAt(0))).toDisplayString();
  201. for (int i = 1; i < relations.size(); i++) {
  202. ret = ret + ","
  203. + ((AccessibleRelation) (relations.elementAt(i))).
  204. toDisplayString();
  205. }
  206. }
  207. return ret;
  208. }
  209. }