Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/accessibility/AccessibleStateSet.java
38829 views
/*1* Copyright (c) 1997, 2004, 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;28import java.util.Locale;29import java.util.MissingResourceException;30import java.util.ResourceBundle;3132/**33* Class AccessibleStateSet determines a component's state set. The state set34* of a component is a set of AccessibleState objects and descriptions. E.G., The35* current overall state of the object, such as whether it is enabled,36* has focus, etc.37*38* @see AccessibleState39*40* @author Willie Walker41*/42public class AccessibleStateSet {4344/**45* Each entry in the Vector represents an AccessibleState.46* @see #add47* @see #addAll48* @see #remove49* @see #contains50* @see #toArray51* @see #clear52*/53protected Vector<AccessibleState> states = null;5455/**56* Creates a new empty state set.57*/58public AccessibleStateSet() {59states = null;60}6162/**63* Creates a new state with the initial set of states contained in64* the array of states passed in. Duplicate entries are ignored.65*66* @param states an array of AccessibleState describing the state set.67*/68public AccessibleStateSet(AccessibleState[] states) {69if (states.length != 0) {70this.states = new Vector(states.length);71for (int i = 0; i < states.length; i++) {72if (!this.states.contains(states[i])) {73this.states.addElement(states[i]);74}75}76}77}7879/**80* Adds a new state to the current state set if it is not already81* present. If the state is already in the state set, the state82* set is unchanged and the return value is false. Otherwise,83* the state is added to the state set and the return value is84* true.85* @param state the state to add to the state set86* @return true if state is added to the state set; false if the state set87* is unchanged88*/89public boolean add(AccessibleState state) {90// [[[ PENDING: WDW - the implementation of this does not need91// to always use a vector of states. It could be improved by92// caching the states as a bit set.]]]93if (states == null) {94states = new Vector();95}9697if (!states.contains(state)) {98states.addElement(state);99return true;100} else {101return false;102}103}104105/**106* Adds all of the states to the existing state set. Duplicate entries107* are ignored.108* @param states AccessibleState array describing the state set.109*/110public void addAll(AccessibleState[] states) {111if (states.length != 0) {112if (this.states == null) {113this.states = new Vector(states.length);114}115for (int i = 0; i < states.length; i++) {116if (!this.states.contains(states[i])) {117this.states.addElement(states[i]);118}119}120}121}122123/**124* Removes a state from the current state set. If the state is not125* in the set, the state set will be unchanged and the return value126* will be false. If the state is in the state set, it will be removed127* from the set and the return value will be true.128*129* @param state the state to remove from the state set130* @return true if the state is in the state set; false if the state set131* will be unchanged132*/133public boolean remove(AccessibleState state) {134if (states == null) {135return false;136} else {137return states.removeElement(state);138}139}140141/**142* Removes all the states from the current state set.143*/144public void clear() {145if (states != null) {146states.removeAllElements();147}148}149150/**151* Checks if the current state is in the state set.152* @param state the state153* @return true if the state is in the state set; otherwise false154*/155public boolean contains(AccessibleState state) {156if (states == null) {157return false;158} else {159return states.contains(state);160}161}162163/**164* Returns the current state set as an array of AccessibleState165* @return AccessibleState array containing the current state.166*/167public AccessibleState[] toArray() {168if (states == null) {169return new AccessibleState[0];170} else {171AccessibleState[] stateArray = new AccessibleState[states.size()];172for (int i = 0; i < stateArray.length; i++) {173stateArray[i] = (AccessibleState) states.elementAt(i);174}175return stateArray;176}177}178179/**180* Creates a localized String representing all the states in the set181* using the default locale.182*183* @return comma separated localized String184* @see AccessibleBundle#toDisplayString185*/186public String toString() {187String ret = null;188if ((states != null) && (states.size() > 0)) {189ret = ((AccessibleState) (states.elementAt(0))).toDisplayString();190for (int i = 1; i < states.size(); i++) {191ret = ret + ","192+ ((AccessibleState) (states.elementAt(i))).193toDisplayString();194}195}196return ret;197}198}199200201