Source for javax.accessibility.AccessibleRelationSet

   1: /* AccessibleRelationSet.java -- the combined relations 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 java.util.Locale;
  41: import java.util.Vector;
  42: 
  43: /**
  44:  * Describes all relations of an accessible object. For example, an object
  45:  * by labeled by one object and control another.
  46:  *
  47:  * @author Eric Blake (ebb9@email.byu.edu)
  48:  * @see AccessibleRelation
  49:  * @since 1.2
  50:  * @status updated to 1.4
  51:  */
  52: public class AccessibleRelationSet
  53: {
  54:   /**
  55:    * The list of relations, should be instances of AccessibleRelation. Don't
  56:    * set this to null.
  57:    *
  58:    * @see #add(AccessibleRelation)
  59:    * @see #addAll(AccessibleRelation[])
  60:    * @see #remove(AccessibleRelation)
  61:    * @see #contains(String)
  62:    * @see #get(String)
  63:    * @see #size()
  64:    * @see #toArray()
  65:    * @see #clear()
  66:    */
  67:   protected Vector relations = new Vector();
  68: 
  69:   /**
  70:    * Create an empty relation set.
  71:    */
  72:   public AccessibleRelationSet()
  73:   {
  74:   }
  75: 
  76:   /**
  77:    * Create a relation set initialized with the given relations, duplicates are
  78:    * ignored.
  79:    *
  80:    * @param relations the relations to insert
  81:    * @throws NullPointerException if relations is null
  82:    */
  83:   public AccessibleRelationSet(AccessibleRelation[] relations)
  84:   {
  85:     addAll(relations);
  86:   }
  87: 
  88:   /**
  89:    * Add a new relation to the current set. If the relation is already in
  90:    * the set, the targets are merged with the existing relation, possibly
  91:    * resulting in an object being in the target list more than once. Do not
  92:    * add a relation with a null key, as it will cause problems later.
  93:    *
  94:    * @param relation the relation to add
  95:    * @return true if the set was modified, which is always the case
  96:    * @throws NullPointerException if relation is null
  97:    */
  98:   public boolean add(AccessibleRelation relation)
  99:   {
 100:     AccessibleRelation old = get(relation.key);
 101:     if (old == null)
 102:       return relations.add(relation);
 103:     if (old.targets.length == 0)
 104:       old.targets = relation.targets;
 105:     else if (relation.targets.length != 0)
 106:       {
 107:         Object[] t = new Object[old.targets.length + relation.targets.length];
 108:         System.arraycopy(old.targets, 0, t, 0, old.targets.length);
 109:         System.arraycopy(relation.targets, 0, t, old.targets.length,
 110:                          relation.targets.length);
 111:         old.targets = t;
 112:       }
 113:     return true;
 114:   }
 115: 
 116:   /**
 117:    * Add all of the relations to the current set. Duplicates are ignored.
 118:    *
 119:    * @param array the array of relations to add
 120:    * @throws NullPointerException if array is null or has null entries
 121:    */
 122:   public void addAll(AccessibleRelation[] array)
 123:   {
 124:     int i = array.length;
 125:     while (--i >= 0)
 126:       add(array[i]);
 127:   }
 128: 
 129:   /**
 130:    * Remove a relation from the set. If a relation was removed, return true.
 131:    * Note that this uses AccessibleRelation.equals, which defaults to ==, so a
 132:    * relation with the same key may still exist in the set afterwords.
 133:    *
 134:    * @param relation the state to remove
 135:    * @return true if the set changed
 136:    */
 137:   public boolean remove(AccessibleRelation relation)
 138:   {
 139:     return relations.remove(relation);
 140:   }
 141: 
 142:   /**
 143:    * Clear all relations in the set.
 144:    */
 145:   public void clear()
 146:   {
 147:     relations.clear();
 148:   }
 149: 
 150:   /**
 151:    * Return the number of relations in the set.
 152:    *
 153:    * @return the set size
 154:    */
 155:   public int size()
 156:   {
 157:     return relations.size();
 158:   }
 159: 
 160:   /**
 161:    * Check if the relation key is in the set.
 162:    *
 163:    * @param key the relation to locate
 164:    * @return true if it is in the set
 165:    */
 166:   public boolean contains(String key)
 167:   {
 168:     int i = relations.size();
 169:     while (--i >= 0)
 170:       if (((AccessibleRelation) relations.get(i)).key.equals(key))
 171:         return true;
 172:     return false;
 173:   }
 174: 
 175:   /**
 176:    * Get the relation that matches the key.
 177:    *
 178:    * @param key the relation to locate
 179:    * @return the relation in the set, or null
 180:    */
 181:   public AccessibleRelation get(String key)
 182:   {
 183:     int i = relations.size();
 184:     while (--i >= 0)
 185:       {
 186:         AccessibleRelation r = (AccessibleRelation) relations.get(i);
 187:         if (r.key.equals(key))
 188:           return r;
 189:       }
 190:     return null;
 191:   }
 192: 
 193:   /**
 194:    * Return the relation set as an array.
 195:    *
 196:    * @return an array of the current relations
 197:    */
 198:   public AccessibleRelation[] toArray()
 199:   {
 200:     AccessibleRelation[] result = new AccessibleRelation[relations.size()];
 201:     relations.toArray(result);
 202:     return result;
 203:   }
 204: 
 205:   /**
 206:    * Return a localized, comma-separated string representing all relations
 207:    * in the set. This is in arbitrary order.
 208:    *
 209:    * @return the string representation
 210:    * @see AccessibleBundle#toDisplayString(String, Locale)
 211:    */
 212:   public String toString()
 213:   {
 214:     int i = relations.size();
 215:     if (i == 0)
 216:       return "";
 217:     // Pre-allocate an average of 10 chars per state.
 218:     StringBuffer b = new StringBuffer(i * 10);
 219:     while (--i >= 0)
 220:       b.append(relations.get(i)).append(',');
 221:     return b.substring(0, b.length() - 1);
 222:   }
 223: } // class AccessibleRelationSet