Source for javax.accessibility.AccessibleStateSet

   1: /* AccessibleStateSet.java -- the combined state of an accessible object
   2:    Copyright (C) 2002, 2005 Free Software Foundation
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.accessibility;
  39: 
  40: import gnu.java.lang.CPStringBuilder;
  41: 
  42: import java.util.Locale;
  43: import java.util.Vector;
  44: 
  45: /**
  46:  * Describes all elements of an accessible object's state. For example, an
  47:  * object may be enabled and have focus.
  48:  *
  49:  * @author Eric Blake (ebb9@email.byu.edu)
  50:  * @see AccessibleState
  51:  * @since 1.2
  52:  * @status updated to 1.4
  53:  */
  54: public class AccessibleStateSet
  55: {
  56:   /**
  57:    * The list of states, should be instances of AccessibleState. Don't set
  58:    * this to null.
  59:    *
  60:    * @see #add(AccessibleState)
  61:    * @see #addAll(AccessibleState[])
  62:    * @see #remove(AccessibleState)
  63:    * @see #contains(AccessibleState)
  64:    * @see #toArray()
  65:    * @see #clear()
  66:    */
  67:   protected Vector<AccessibleState> states = new Vector<AccessibleState>();
  68: 
  69:   /**
  70:    * Create an empty state set.
  71:    */
  72:   public AccessibleStateSet()
  73:   {
  74:   }
  75: 
  76:   /**
  77:    * Create a state set initialized with the given states, duplicates are
  78:    * ignored.
  79:    *
  80:    * @param states the states to insert
  81:    * @throws NullPointerException if states is null
  82:    */
  83:   public AccessibleStateSet(AccessibleState[] states)
  84:   {
  85:     addAll(states);
  86:   }
  87: 
  88:   /**
  89:    * Add a new state to the current set. Return true if the state was added,
  90:    * as duplicates are ignored. Entering a null state will cause problems
  91:    * later, so don't do it.
  92:    *
  93:    * @param state the state to add
  94:    * @return true if the state was added
  95:    */
  96:   public boolean add(AccessibleState state)
  97:   {
  98:     return states.contains(state) ? false : states.add(state);
  99:   }
 100: 
 101:   /**
 102:    * Add all of the states to the current set. Duplicates are ignored.
 103:    * Entering a null state will cause problems later, so don't do it.
 104:    *
 105:    * @param array the array of states to add
 106:    * @throws NullPointerException if array is null
 107:    */
 108:   public void addAll(AccessibleState[] array)
 109:   {
 110:     int i = array.length;
 111:     while (--i >= 0)
 112:       add(array[i]);
 113:   }
 114: 
 115:   /**
 116:    * Remove a state from the set. If a state was removed, return true.
 117:    *
 118:    * @param state the state to remove
 119:    * @return true if the set changed
 120:    */
 121:   public boolean remove(AccessibleState state)
 122:   {
 123:     return states.remove(state);
 124:   }
 125: 
 126:   /**
 127:    * Clear all states in the set.
 128:    */
 129:   public void clear()
 130:   {
 131:     states.clear();
 132:   }
 133: 
 134:   /**
 135:    * Check if the current state is in the set.
 136:    *
 137:    * @param state the state to locate
 138:    * @return true if it is in the set
 139:    */
 140:   public boolean contains(AccessibleState state)
 141:   {
 142:     return states.contains(state);
 143:   }
 144: 
 145:   /**
 146:    * Return the state set as an array.
 147:    *
 148:    * @return an array of the current states
 149:    */
 150:   public AccessibleState[] toArray()
 151:   {
 152:     AccessibleState[] result = new AccessibleState[states.size()];
 153:     states.toArray(result);
 154:     return result;
 155:   }
 156: 
 157:   /**
 158:    * Return a localized, comma-separated string representing all states
 159:    * in the set. This is in arbitrary order.
 160:    *
 161:    * @return the string representation
 162:    * @see AccessibleBundle#toDisplayString(String, Locale)
 163:    */
 164:   public String toString()
 165:   {
 166:     int i = states.size();
 167:     if (i == 0)
 168:       return "";
 169:     // Pre-allocate an average of 10 chars per state.
 170:     CPStringBuilder b = new CPStringBuilder(i * 10);
 171:     while (--i >= 0)
 172:       b.append(states.get(i)).append(',');
 173:     return b.substring(0, b.length() - 1);
 174:   }
 175: } // class AccessibleStateSet