| 1 | /* AccessibleStateSet.java -- the combined state of an accessible object
|
|---|
| 2 | Copyright (C) 2002 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 java.util.Vector;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Describes all elements of an accessible object's state. For example, an
|
|---|
| 44 | * object may be enabled and have focus.
|
|---|
| 45 | *
|
|---|
| 46 | * @author Eric Blake <[email protected]>
|
|---|
| 47 | * @see AccessibleState
|
|---|
| 48 | * @since 1.2
|
|---|
| 49 | * @status updated to 1.4
|
|---|
| 50 | */
|
|---|
| 51 | public class AccessibleStateSet
|
|---|
| 52 | {
|
|---|
| 53 | /**
|
|---|
| 54 | * The list of states, should be instances of AccessibleState. Don't set
|
|---|
| 55 | * this to null.
|
|---|
| 56 | *
|
|---|
| 57 | * @see #add(AccessibleState)
|
|---|
| 58 | * @see #addAll(AccessibleState[])
|
|---|
| 59 | * @see #remove(AccessibleState)
|
|---|
| 60 | * @see #contains(AccessibleState)
|
|---|
| 61 | * @see #toArray()
|
|---|
| 62 | * @see #clear()
|
|---|
| 63 | */
|
|---|
| 64 | protected Vector states = new Vector();
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Create an empty state set.
|
|---|
| 68 | */
|
|---|
| 69 | public AccessibleStateSet()
|
|---|
| 70 | {
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Create a state set initialized with the given states, duplicates are
|
|---|
| 75 | * ignored.
|
|---|
| 76 | *
|
|---|
| 77 | * @param states the states to insert
|
|---|
| 78 | * @throws NullPointerException if states is null
|
|---|
| 79 | */
|
|---|
| 80 | public AccessibleStateSet(AccessibleState[] states)
|
|---|
| 81 | {
|
|---|
| 82 | addAll(states);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Add a new state to the current set. Return true if the state was added,
|
|---|
| 87 | * as duplicates are ignored. Entering a null state will cause problems
|
|---|
| 88 | * later, so don't do it.
|
|---|
| 89 | *
|
|---|
| 90 | * @param state the state to add
|
|---|
| 91 | * @return true if the state was added
|
|---|
| 92 | */
|
|---|
| 93 | public boolean add(AccessibleState state)
|
|---|
| 94 | {
|
|---|
| 95 | return states.contains(state) ? false : states.add(state);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Add all of the states to the current set. Duplicates are ignored.
|
|---|
| 100 | * Entering a null state will cause problems later, so don't do it.
|
|---|
| 101 | *
|
|---|
| 102 | * @param array the array of states to add
|
|---|
| 103 | * @throws NullPointerException if array is null
|
|---|
| 104 | */
|
|---|
| 105 | public void addAll(AccessibleState[] array)
|
|---|
| 106 | {
|
|---|
| 107 | int i = array.length;
|
|---|
| 108 | while (--i >= 0)
|
|---|
| 109 | add(array[i]);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Remove a state from the set. If a state was removed, return true.
|
|---|
| 114 | *
|
|---|
| 115 | * @param state the state to remove
|
|---|
| 116 | * @return true if the set changed
|
|---|
| 117 | */
|
|---|
| 118 | public boolean remove(AccessibleState state)
|
|---|
| 119 | {
|
|---|
| 120 | return states.remove(state);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * Clear all states in the set.
|
|---|
| 125 | */
|
|---|
| 126 | public void clear()
|
|---|
| 127 | {
|
|---|
| 128 | states.clear();
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Check if the current state is in the set.
|
|---|
| 133 | *
|
|---|
| 134 | * @param state the state to locate
|
|---|
| 135 | * @return true if it is in the set
|
|---|
| 136 | */
|
|---|
| 137 | public boolean contains(AccessibleState state)
|
|---|
| 138 | {
|
|---|
| 139 | return states.contains(state);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Return the state set as an array.
|
|---|
| 144 | *
|
|---|
| 145 | * @return an array of the current states
|
|---|
| 146 | */
|
|---|
| 147 | public AccessibleState[] toArray()
|
|---|
| 148 | {
|
|---|
| 149 | AccessibleState[] result = new AccessibleState[states.size()];
|
|---|
| 150 | states.toArray(result);
|
|---|
| 151 | return result;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Return a localized, comma-separated string representing all states
|
|---|
| 156 | * in the set. This is in arbitrary order.
|
|---|
| 157 | *
|
|---|
| 158 | * @return the string representation
|
|---|
| 159 | * @see AccessibleBundle#toDisplayString(String, Locale)
|
|---|
| 160 | */
|
|---|
| 161 | public String toString()
|
|---|
| 162 | {
|
|---|
| 163 | int i = states.size();
|
|---|
| 164 | if (i == 0)
|
|---|
| 165 | return "";
|
|---|
| 166 | // Pre-allocate an average of 10 chars per state.
|
|---|
| 167 | StringBuffer b = new StringBuffer(i * 10);
|
|---|
| 168 | while (--i >= 0)
|
|---|
| 169 | b.append(states.get(i)).append(',');
|
|---|
| 170 | return b.substring(0, b.length() - 1);
|
|---|
| 171 | }
|
|---|
| 172 | } // class AccessibleStateSet
|
|---|