Path: blob/master/src/java.desktop/share/classes/javax/accessibility/AccessibleRelationSet.java
41153 views
/*1* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.accessibility;2627import java.util.Vector;2829/**30* Class {@code AccessibleRelationSet} determines a component's relation set.31* The relation set of a component is a set of {@code AccessibleRelation}32* objects that describe the component's relationships with other components.33*34* @author Lynn Monsanto35* @see AccessibleRelation36* @since 1.337*/38public class AccessibleRelationSet {3940/**41* Each entry in the {@code Vector} represents an42* {@code AccessibleRelation}.43*44* @see #add45* @see #addAll46* @see #remove47* @see #contains48* @see #get49* @see #size50* @see #toArray51* @see #clear52*/53protected Vector<AccessibleRelation> relations = null;5455/**56* Creates a new empty relation set.57*/58public AccessibleRelationSet() {59relations = null;60}6162/**63* Creates a new relation with the initial set of relations contained in the64* array of relations passed in. Duplicate entries are ignored.65*66* @param relations an array of {@code AccessibleRelation} describing the67* relation set68*/69public AccessibleRelationSet(AccessibleRelation[] relations) {70if (relations.length != 0) {71this.relations = new Vector<>(relations.length);72for (int i = 0; i < relations.length; i++) {73add(relations[i]);74}75}76}7778/**79* Adds a new relation to the current relation set. If the relation is80* already in the relation set, the target(s) of the specified relation is81* merged with the target(s) of the existing relation. Otherwise, the new82* relation is added to the relation set.83*84* @param relation the relation to add to the relation set85* @return {@code true} if relation is added to the relation set;86* {@code false} if the relation set is unchanged87*/88public boolean add(AccessibleRelation relation) {89if (relations == null) {90relations = new Vector<>();91}9293// Merge the relation targets if the key exists94AccessibleRelation existingRelation = get(relation.getKey());95if (existingRelation == null) {96relations.addElement(relation);97return true;98} else {99Object [] existingTarget = existingRelation.getTarget();100Object [] newTarget = relation.getTarget();101int mergedLength = existingTarget.length + newTarget.length;102Object [] mergedTarget = new Object[mergedLength];103for (int i = 0; i < existingTarget.length; i++) {104mergedTarget[i] = existingTarget[i];105}106for (int i = existingTarget.length, j = 0;107i < mergedLength;108i++, j++) {109mergedTarget[i] = newTarget[j];110}111existingRelation.setTarget(mergedTarget);112}113return true;114}115116/**117* Adds all of the relations to the existing relation set. Duplicate entries118* are ignored.119*120* @param relations {@code AccessibleRelation} array describing the121* relation set122*/123public void addAll(AccessibleRelation[] relations) {124if (relations.length != 0) {125if (this.relations == null) {126this.relations = new Vector<>(relations.length);127}128for (int i = 0; i < relations.length; i++) {129add(relations[i]);130}131}132}133134/**135* Removes a relation from the current relation set. If the relation is not136* in the set, the relation set will be unchanged and the return value will137* be {@code false}. If the relation is in the relation set, it will be138* removed from the set and the return value will be {@code true}.139*140* @param relation the relation to remove from the relation set141* @return {@code true} if the relation is in the relation set;142* {@code false} if the relation set is unchanged143*/144public boolean remove(AccessibleRelation relation) {145if (relations == null) {146return false;147} else {148return relations.removeElement(relation);149}150}151152/**153* Removes all the relations from the current relation set.154*/155public void clear() {156if (relations != null) {157relations.removeAllElements();158}159}160161/**162* Returns the number of relations in the relation set.163*164* @return the number of relations in the relation set165*/166public int size() {167if (relations == null) {168return 0;169} else {170return relations.size();171}172}173174/**175* Returns whether the relation set contains a relation that matches the176* specified key.177*178* @param key the {@code AccessibleRelation} key179* @return {@code true} if the relation is in the relation set; otherwise180* {@code false}181*/182public boolean contains(String key) {183return get(key) != null;184}185186/**187* Returns the relation that matches the specified key.188*189* @param key the {@code AccessibleRelation} key190* @return the relation, if one exists, that matches the specified key.191* Otherwise, {@code null} is returned.192*/193public AccessibleRelation get(String key) {194if (relations == null) {195return null;196} else {197int len = relations.size();198for (int i = 0; i < len; i++) {199AccessibleRelation relation = relations.elementAt(i);200if (relation != null && relation.getKey().equals(key)) {201return relation;202}203}204return null;205}206}207208/**209* Returns the current relation set as an array of210* {@code AccessibleRelation}.211*212* @return {@code AccessibleRelation} array contacting the current relation213*/214public AccessibleRelation[] toArray() {215if (relations == null) {216return new AccessibleRelation[0];217} else {218AccessibleRelation[] relationArray219= new AccessibleRelation[relations.size()];220for (int i = 0; i < relationArray.length; i++) {221relationArray[i] = relations.elementAt(i);222}223return relationArray;224}225}226227/**228* Creates a localized string representing all the relations in the set229* using the default locale.230*231* @return comma separated localized string232* @see AccessibleBundle#toDisplayString233*/234public String toString() {235String ret = "";236if ((relations != null) && (relations.size() > 0)) {237ret = (relations.elementAt(0)).toDisplayString();238for (int i = 1; i < relations.size(); i++) {239ret = ret + ","240+ (relations.elementAt(i)).toDisplayString();241}242}243return ret;244}245}246247248