001    /* AccessibleStateSet.java -- the combined state of an accessible object
002       Copyright (C) 2002, 2005 Free Software Foundation
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.accessibility;
039    
040    import java.util.Locale;
041    import java.util.Vector;
042    
043    /**
044     * Describes all elements of an accessible object's state. For example, an
045     * object may be enabled and have focus.
046     *
047     * @author Eric Blake (ebb9@email.byu.edu)
048     * @see AccessibleState
049     * @since 1.2
050     * @status updated to 1.4
051     */
052    public class AccessibleStateSet
053    {
054      /**
055       * The list of states, should be instances of AccessibleState. Don't set
056       * this to null.
057       *
058       * @see #add(AccessibleState)
059       * @see #addAll(AccessibleState[])
060       * @see #remove(AccessibleState)
061       * @see #contains(AccessibleState)
062       * @see #toArray()
063       * @see #clear()
064       */
065      protected Vector<AccessibleState> states = new Vector<AccessibleState>();
066    
067      /**
068       * Create an empty state set.
069       */
070      public AccessibleStateSet()
071      {
072      }
073    
074      /**
075       * Create a state set initialized with the given states, duplicates are
076       * ignored.
077       *
078       * @param states the states to insert
079       * @throws NullPointerException if states is null
080       */
081      public AccessibleStateSet(AccessibleState[] states)
082      {
083        addAll(states);
084      }
085    
086      /**
087       * Add a new state to the current set. Return true if the state was added,
088       * as duplicates are ignored. Entering a null state will cause problems
089       * later, so don't do it.
090       *
091       * @param state the state to add
092       * @return true if the state was added
093       */
094      public boolean add(AccessibleState state)
095      {
096        return states.contains(state) ? false : states.add(state);
097      }
098    
099      /**
100       * Add all of the states to the current set. Duplicates are ignored.
101       * Entering a null state will cause problems later, so don't do it.
102       *
103       * @param array the array of states to add
104       * @throws NullPointerException if array is null
105       */
106      public void addAll(AccessibleState[] array)
107      {
108        int i = array.length;
109        while (--i >= 0)
110          add(array[i]);
111      }
112    
113      /**
114       * Remove a state from the set. If a state was removed, return true.
115       *
116       * @param state the state to remove
117       * @return true if the set changed
118       */
119      public boolean remove(AccessibleState state)
120      {
121        return states.remove(state);
122      }
123    
124      /**
125       * Clear all states in the set.
126       */
127      public void clear()
128      {
129        states.clear();
130      }
131    
132      /**
133       * Check if the current state is in the set.
134       *
135       * @param state the state to locate
136       * @return true if it is in the set
137       */
138      public boolean contains(AccessibleState state)
139      {
140        return states.contains(state);
141      }
142    
143      /**
144       * Return the state set as an array.
145       *
146       * @return an array of the current states
147       */
148      public AccessibleState[] toArray()
149      {
150        AccessibleState[] result = new AccessibleState[states.size()];
151        states.toArray(result);
152        return result;
153      }
154    
155      /**
156       * Return a localized, comma-separated string representing all states
157       * in the set. This is in arbitrary order.
158       *
159       * @return the string representation
160       * @see AccessibleBundle#toDisplayString(String, Locale)
161       */
162      public String toString()
163      {
164        int i = states.size();
165        if (i == 0)
166          return "";
167        // Pre-allocate an average of 10 chars per state.
168        StringBuffer b = new StringBuffer(i * 10);
169        while (--i >= 0)
170          b.append(states.get(i)).append(',');
171        return b.substring(0, b.length() - 1);
172      }
173    } // class AccessibleStateSet