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