1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.accessibility;
  27 
  28 import java.util.Vector;
  29 import java.util.Locale;
  30 import java.util.MissingResourceException;
  31 import java.util.ResourceBundle;
  32 
  33 /**
  34  * Class AccessibleStateSet determines a component's state set.  The state set
  35  * of a component is a set of AccessibleState objects and descriptions. E.G., The
  36  * current overall state of the object, such as whether it is enabled,
  37  * has focus, etc.
  38  *
  39  * @see AccessibleState
  40  *
  41  * @author      Willie Walker
  42  */
  43 public class AccessibleStateSet {
  44 
  45     /**
  46      * Each entry in the Vector represents an AccessibleState.
  47      * @see #add
  48      * @see #addAll
  49      * @see #remove
  50      * @see #contains
  51      * @see #toArray
  52      * @see #clear
  53      */
  54     protected Vector<AccessibleState> states = null;
  55 
  56     /**
  57      * Creates a new empty state set.
  58      */
  59     public AccessibleStateSet() {
  60         states = null;
  61     }
  62 
  63     /**
  64      * Creates a new state with the initial set of states contained in
  65      * the array of states passed in.  Duplicate entries are ignored.
  66      *
  67      * @param states an array of AccessibleState describing the state set.
  68      */
  69     public AccessibleStateSet(AccessibleState[] states) {
  70         if (states.length != 0) {
  71             this.states = new Vector(states.length);
  72             for (int i = 0; i < states.length; i++) {
  73                 if (!this.states.contains(states[i])) {
  74                     this.states.addElement(states[i]);
  75                 }
  76             }
  77         }
  78     }
  79 
  80     /**
  81      * Adds a new state to the current state set if it is not already
  82      * present.  If the state is already in the state set, the state
  83      * set is unchanged and the return value is false.  Otherwise,
  84      * the state is added to the state set and the return value is
  85      * true.
  86      * @param state the state to add to the state set
  87      * @return true if state is added to the state set; false if the state set
  88      * is unchanged
  89      */
  90     public boolean add(AccessibleState state) {
  91         // [[[ PENDING:  WDW - the implementation of this does not need
  92         // to always use a vector of states.  It could be improved by
  93         // caching the states as a bit set.]]]
  94         if (states == null) {
  95             states = new Vector();
  96         }
  97 
  98         if (!states.contains(state)) {
  99             states.addElement(state);
 100             return true;
 101         } else {
 102             return false;
 103         }
 104     }
 105 
 106     /**
 107      * Adds all of the states to the existing state set.  Duplicate entries
 108      * are ignored.
 109      * @param states  AccessibleState array describing the state set.
 110      */
 111     public void addAll(AccessibleState[] states) {
 112         if (states.length != 0) {
 113             if (this.states == null) {
 114                 this.states = new Vector(states.length);
 115             }
 116             for (int i = 0; i < states.length; i++) {
 117                 if (!this.states.contains(states[i])) {
 118                     this.states.addElement(states[i]);
 119                 }
 120             }
 121         }
 122     }
 123 
 124     /**
 125      * Removes a state from the current state set.  If the state is not
 126      * in the set, the state set will be unchanged and the return value
 127      * will be false.  If the state is in the state set, it will be removed
 128      * from the set and the return value will be true.
 129      *
 130      * @param state the state to remove from the state set
 131      * @return true if the state is in the state set; false if the state set
 132      * will be unchanged
 133      */
 134     public boolean remove(AccessibleState state) {
 135         if (states == null) {
 136             return false;
 137         } else {
 138             return states.removeElement(state);
 139         }
 140     }
 141 
 142     /**
 143      * Removes all the states from the current state set.
 144      */
 145     public void clear() {
 146         if (states != null) {
 147             states.removeAllElements();
 148         }
 149     }
 150 
 151     /**
 152      * Checks if the current state is in the state set.
 153      * @param state the state
 154      * @return true if the state is in the state set; otherwise false
 155      */
 156     public boolean contains(AccessibleState state) {
 157         if (states == null) {
 158             return false;
 159         } else {
 160             return states.contains(state);
 161         }
 162     }
 163 
 164     /**
 165      * Returns the current state set as an array of AccessibleState
 166      * @return AccessibleState array containing the current state.
 167      */
 168     public AccessibleState[] toArray() {
 169         if (states == null) {
 170             return new AccessibleState[0];
 171         } else {
 172             AccessibleState[] stateArray = new AccessibleState[states.size()];
 173             for (int i = 0; i < stateArray.length; i++) {
 174                 stateArray[i] = states.elementAt(i);
 175             }
 176             return stateArray;
 177         }
 178     }
 179 
 180     /**
 181      * Creates a localized String representing all the states in the set
 182      * using the default locale.
 183      *
 184      * @return comma separated localized String
 185      * @see AccessibleBundle#toDisplayString
 186      */
 187     public String toString() {
 188         String ret = null;
 189         if ((states != null) && (states.size() > 0)) {
 190             ret = states.elementAt(0).toDisplayString();
 191             for (int i = 1; i < states.size(); i++) {
 192                 ret = ret + ","
 193                         + states.elementAt(i).toDisplayString();
 194             }
 195         }
 196         return ret;
 197     }
 198 }