Path: blob/master/src/java.desktop/share/classes/javax/accessibility/AccessibleStateSet.java
41153 views
/*1* Copyright (c) 1997, 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 AccessibleStateSet} determines a component's state set. The31* state set of a component is a set of {@code AccessibleState} objects and32* descriptions. E.G., The current overall state of the object, such as whether33* it is enabled, has focus, etc.34*35* @author Willie Walker36* @see AccessibleState37*/38public class AccessibleStateSet {3940/**41* Each entry in the {@code Vector} represents an {@code AccessibleState}.42*43* @see #add44* @see #addAll45* @see #remove46* @see #contains47* @see #toArray48* @see #clear49*/50protected Vector<AccessibleState> states = null;5152/**53* Creates a new empty state set.54*/55public AccessibleStateSet() {56states = null;57}5859/**60* Creates a new state with the initial set of states contained in the array61* of states passed in. Duplicate entries are ignored.62*63* @param states an array of {@code AccessibleState} describing the state64* set65*/66public AccessibleStateSet(AccessibleState[] states) {67if (states.length != 0) {68this.states = new Vector<>(states.length);69for (int i = 0; i < states.length; i++) {70if (!this.states.contains(states[i])) {71this.states.addElement(states[i]);72}73}74}75}7677/**78* Adds a new state to the current state set if it is not already present.79* If the state is already in the state set, the state set is unchanged and80* the return value is {@code false}. Otherwise, the state is added to the81* state set and the return value is {@code true}.82*83* @param state the state to add to the state set84* @return {@code true} if state is added to the state set; {@code false} if85* the state set is unchanged86*/87public boolean add(AccessibleState state) {88// [[[ PENDING: WDW - the implementation of this does not need89// to always use a vector of states. It could be improved by90// caching the states as a bit set.]]]91if (states == null) {92states = new Vector<>();93}9495if (!states.contains(state)) {96states.addElement(state);97return true;98} else {99return false;100}101}102103/**104* Adds all of the states to the existing state set. Duplicate entries are105* ignored.106*107* @param states {@code AccessibleState} array describing the state set108*/109public void addAll(AccessibleState[] states) {110if (states.length != 0) {111if (this.states == null) {112this.states = new Vector<>(states.length);113}114for (int i = 0; i < states.length; i++) {115if (!this.states.contains(states[i])) {116this.states.addElement(states[i]);117}118}119}120}121122/**123* Removes a state from the current state set. If the state is not in the124* set, the state set will be unchanged and the return value will be125* {@code false}. If the state is in the state set, it will be removed from126* the set and the return value will be {@code true}.127*128* @param state the state to remove from the state set129* @return {@code true} if the state is in the state set; {@code false} if130* the state set will be unchanged131*/132public boolean remove(AccessibleState state) {133if (states == null) {134return false;135} else {136return states.removeElement(state);137}138}139140/**141* Removes all the states from the current state set.142*/143public void clear() {144if (states != null) {145states.removeAllElements();146}147}148149/**150* Checks if the current state is in the state set.151*152* @param state the state153* @return {@code true} if the state is in the state set; otherwise154* {@code false}155*/156public boolean contains(AccessibleState state) {157if (states == null) {158return false;159} else {160return states.contains(state);161}162}163164/**165* Returns the current state set as an array of {@code AccessibleState}.166*167* @return {@code AccessibleState} array containing the current state168*/169public AccessibleState[] toArray() {170if (states == null) {171return new AccessibleState[0];172} else {173AccessibleState[] stateArray = new AccessibleState[states.size()];174for (int i = 0; i < stateArray.length; i++) {175stateArray[i] = states.elementAt(i);176}177return stateArray;178}179}180181/**182* Creates a localized string representing all the states in the set using183* the default locale.184*185* @return comma separated localized string186* @see AccessibleBundle#toDisplayString187*/188public String toString() {189String ret = null;190if ((states != null) && (states.size() > 0)) {191ret = states.elementAt(0).toDisplayString();192for (int i = 1; i < states.size(); i++) {193ret = ret + ","194+ states.elementAt(i).toDisplayString();195}196}197return ret;198}199}200201202