Source for javax.swing.text.SimpleAttributeSet

   1: /* SimpleAttributeSet.java --
   2:    Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
   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: 
  39: package javax.swing.text;
  40: 
  41: import java.io.Serializable;
  42: import java.util.Enumeration;
  43: import java.util.Hashtable;
  44: 
  45: /**
  46:  * A set of attributes.
  47:  */
  48: public class SimpleAttributeSet
  49:   implements MutableAttributeSet, Serializable, Cloneable
  50: {
  51:   /** The serialization UID (compatible with JDK1.5). */
  52:   private static final long serialVersionUID = 8267656273837665219L;
  53: 
  54:   /** An empty attribute set. */
  55:   public static final AttributeSet EMPTY = new SimpleAttributeSet();
  56: 
  57:   /** Storage for the attributes. */
  58:   Hashtable tab;
  59: 
  60:   /**
  61:    * Creates a new attribute set that is initially empty.
  62:    */
  63:   public SimpleAttributeSet()
  64:   {
  65:     tab = new Hashtable();
  66:   }
  67:   
  68:   /**
  69:    * Creates a new <code>SimpleAttributeSet</code> with the same attributes
  70:    * and resolve parent as the specified set.
  71:    * 
  72:    * @param a  the attributes (<code>null</code> not permitted).
  73:    * 
  74:    * @throws NullPointerException if <code>a</code> is <code>null</code>.
  75:    */
  76:   public SimpleAttributeSet(AttributeSet a)
  77:   {
  78:     tab = new Hashtable();
  79:     addAttributes(a);
  80:   }
  81: 
  82:   /**
  83:    * Adds an attribute with the given <code>name</code> and <code>value</code>
  84:    * to the set.  If the set already contains an attribute with the given
  85:    * <code>name</code>, the attribute value is updated.
  86:    * 
  87:    * @param name  the attribute name (<code>null</code> not permitted).
  88:    * @param value  the value (<code>null</code> not permitted).
  89:    * 
  90:    * @throws NullPointerException if either argument is <code>null</code>.
  91:    */
  92:   public void addAttribute(Object name, Object value)
  93:   {
  94:     tab.put(name, value);
  95:   }
  96: 
  97:   /**
  98:    * Adds all the attributes from <code>attributes</code> to this set.
  99:    * 
 100:    * @param attributes  the set of attributes to add (<code>null</code> not
 101:    *                    permitted).
 102:    *                    
 103:    * @throws NullPointerException if <code>attributes</code> is 
 104:    *         <code>null</code>.
 105:    */
 106:   public void addAttributes(AttributeSet attributes)
 107:   {
 108:     Enumeration e = attributes.getAttributeNames();
 109:     while (e.hasMoreElements())
 110:       {
 111:         Object name = e.nextElement();
 112:         Object val = attributes.getAttribute(name);
 113:         tab.put(name, val);
 114:       }
 115:   }
 116: 
 117:   /**
 118:    * Returns a clone of the attribute set.
 119:    * 
 120:    * @return A clone of the attribute set.
 121:    */
 122:   public Object clone()
 123:   {
 124:     SimpleAttributeSet s = new SimpleAttributeSet();
 125:     s.tab = (Hashtable) tab.clone();
 126:     return s;
 127:   }
 128: 
 129:   /**
 130:    * Returns true if the given name and value represent an attribute
 131:    * found either in this AttributeSet or in its resolve parent hierarchy.
 132:    * @param name the key for the attribute
 133:    * @param value the value for the attribute
 134:    * @return true if the attribute is found here or in this set's resolve
 135:    * parent hierarchy
 136:    */
 137:   public boolean containsAttribute(Object name, Object value)
 138:   {
 139:     if (value == null)
 140:       throw new NullPointerException("Null 'value' argument.");
 141:     if (tab.containsKey(name))
 142:       return tab.get(name).equals(value);
 143:     else
 144:       {
 145:         AttributeSet p = getResolveParent();
 146:         if (p != null)
 147:           return p.containsAttribute(name, value);
 148:         else
 149:           return false;
 150:       }
 151:   }
 152:   
 153:   /**
 154:    * Returns true if the given name and value are found in this AttributeSet.
 155:    * Does not check the resolve parent.
 156:    * @param name the key for the attribute
 157:    * @param value the value for the attribute
 158:    * @return true if the attribute is found in this AttributeSet
 159:    */
 160:   boolean containsAttributeLocally(Object name, Object value)
 161:   {
 162:     return tab.containsKey(name) 
 163:       && tab.get(name).equals(value);
 164:   }
 165: 
 166:   /**
 167:    * Returns <code>true</code> of this <code>AttributeSet</code> contains all
 168:    * of the specified <code>attributes</code>.
 169:    *
 170:    * @param attributes the requested attributes
 171:    *
 172:    * @return <code>true</code> of this <code>AttributeSet</code> contains all
 173:    *         of the specified <code>attributes</code>
 174:    */
 175:   public boolean containsAttributes(AttributeSet attributes)
 176:   {
 177:     Enumeration e = attributes.getAttributeNames();
 178:     while (e.hasMoreElements())
 179:       {
 180:         Object name = e.nextElement();
 181:         Object val = attributes.getAttribute(name);
 182:         if (! containsAttribute(name, val))
 183:           return false;        
 184:       }
 185:     return true;
 186:   }
 187: 
 188:   /**
 189:    * Creates and returns a copy of this <code>AttributeSet</code>.
 190:    *
 191:    * @return a copy of this <code>AttributeSet</code>
 192:    */
 193:   public AttributeSet copyAttributes()
 194:   {
 195:     return (AttributeSet) clone();
 196:   }
 197: 
 198:   /**
 199:    * Checks this set for equality with an arbitrary object.
 200:    * 
 201:    * @param obj  the object (<code>null</code> permitted).
 202:    * 
 203:    * @return <code>true</code> if this set is equal to <code>obj</code>, and
 204:    *         <code>false</code> otherwise. 
 205:    */
 206:   public boolean equals(Object obj)
 207:   {
 208:     return 
 209:       (obj instanceof AttributeSet)
 210:       && this.isEqual((AttributeSet) obj);
 211:   }
 212: 
 213:   /**
 214:    * Returns the value of the specified attribute, or <code>null</code> if 
 215:    * there is no attribute with that name.  If the attribute is not defined
 216:    * directly in this set, the parent hierarchy (if there is one) will be
 217:    * used.
 218:    * 
 219:    * @param name  the attribute (<code>null</code> not permitted).
 220:    * 
 221:    * @throws NullPointerException if <code>name</code> is <code>null</code>.
 222:    */
 223:   public Object getAttribute(Object name)
 224:   {
 225:     Object val = tab.get(name);
 226:     if (val != null) 
 227:       return val;
 228: 
 229:     AttributeSet p = getResolveParent();
 230:     if (p != null)
 231:       return p.getAttribute(name);
 232: 
 233:     return null;
 234:   }
 235: 
 236:   /**
 237:    * Returns the number of attributes stored in this set, plus 1 if a parent
 238:    * has been specified (the reference to the parent is stored as a special 
 239:    * attribute).  The attributes stored in the parent do NOT contribute
 240:    * to the count.
 241:    * 
 242:    * @return The attribute count.
 243:    */
 244:   public int getAttributeCount()
 245:   {
 246:     return tab.size();
 247:   }
 248: 
 249:   /**
 250:    * Returns an enumeration of the attribute names.
 251:    * 
 252:    * @return An enumeration of the attribute names.
 253:    */
 254:   public Enumeration getAttributeNames()
 255:   {
 256:     return tab.keys();
 257:   }
 258: 
 259:   /**
 260:    * Returns the resolving parent.
 261:    * 
 262:    * @return The resolving parent (possibly <code>null</code>).
 263:    * 
 264:    * @see #setResolveParent(AttributeSet)
 265:    */
 266:   public AttributeSet getResolveParent()
 267:   {
 268:     return (AttributeSet) tab.get(ResolveAttribute);
 269:   }
 270: 
 271:   /**
 272:    * Returns a hash code for this instance.
 273:    * 
 274:    * @return A hash code.
 275:    */
 276:   public int hashCode()
 277:   {
 278:     return tab.hashCode();
 279:   }
 280: 
 281:   /**
 282:    * Returns <code>true</code> if the given attribute is defined in this set,
 283:    * and <code>false</code> otherwise.  The parent attribute set is not
 284:    * checked.
 285:    * 
 286:    * @param attrName  the attribute name (<code>null</code> not permitted).
 287:    */
 288:   public boolean isDefined(Object attrName)
 289:   {
 290:     return tab.containsKey(attrName);
 291:   }
 292: 
 293:   /**
 294:    * Returns <code>true</code> if the set contains no attributes, and 
 295:    * <code>false</code> otherwise.  Note that the resolving parent is 
 296:    * stored as an attribute, so this method will return <code>false</code> if 
 297:    * a resolving parent is set.
 298:    * 
 299:    * @return <code>true</code> if the set contains no attributes, and 
 300:    * <code>false</code> otherwise.
 301:    */
 302:   public boolean isEmpty()
 303:   {
 304:     return tab.isEmpty();    
 305:   }
 306: 
 307:   /**
 308:    * Returns true if the given set has the same number of attributes
 309:    * as this set and <code>containsAttributes(attr)</code> returns
 310:    * <code>true</code>.
 311:    * 
 312:    * @param attr  the attribute set (<code>null</code> not permitted).
 313:    * 
 314:    * @return A boolean.
 315:    * 
 316:    * @throws NullPointerException if <code>attr</code> is <code>null</code>.
 317:    */
 318:   public boolean isEqual(AttributeSet attr)
 319:   {
 320:     return getAttributeCount() == attr.getAttributeCount()
 321:       && this.containsAttributes(attr);
 322:   }
 323:     
 324:   /**
 325:    * Removes the attribute with the specified <code>name</code>, if this 
 326:    * attribute is defined.  This method will only remove an attribute from
 327:    * this set, not from the resolving parent.
 328:    * 
 329:    * @param name  the attribute name (<code>null</code> not permitted).
 330:    * 
 331:    * @throws NullPointerException if <code>name</code> is <code>null</code>.
 332:    */
 333:   public void removeAttribute(Object name)
 334:   {
 335:     tab.remove(name);
 336:   }
 337: 
 338:   /**
 339:    * Removes attributes from this set if they are found in the 
 340:    * given set.  Only attributes whose key AND value are removed.
 341:    * Removes attributes only from this set, not from the resolving parent.  
 342:    * Since the resolving parent is stored as an attribute, if 
 343:    * <code>attributes</code> has the same resolving parent as this set, the
 344:    * parent will be removed from this set.
 345:    * 
 346:    * @param attributes  the attributes (<code>null</code> not permitted).
 347:    */
 348:   public void removeAttributes(AttributeSet attributes)
 349:   {
 350:     Enumeration e = attributes.getAttributeNames();
 351:     while (e.hasMoreElements())
 352:       {
 353:         Object name = e.nextElement();
 354:         Object val = attributes.getAttribute(name);
 355:         if (containsAttributeLocally(name, val))
 356:           removeAttribute(name);     
 357:       }
 358:   }
 359: 
 360:   /**
 361:    * Removes the attributes listed in <code>names</code>.
 362:    * 
 363:    * @param names  the attribute names (<code>null</code> not permitted).
 364:    * 
 365:    * @throws NullPointerException if <code>names</code> is <code>null</code> 
 366:    *         or contains any <code>null</code> values.
 367:    */
 368:   public void removeAttributes(Enumeration names)
 369:   {
 370:     while (names.hasMoreElements())
 371:       {
 372:         removeAttribute(names.nextElement());
 373:       }    
 374:   }
 375: 
 376:   /**
 377:    * Sets the reolving parent for this set.  When looking up an attribute, if
 378:    * it is not found in this set, then the resolving parent is also used for
 379:    * the lookup.
 380:    * <p>
 381:    * Note that the parent is stored as an attribute, and will contribute 1 to 
 382:    * the count returned by {@link #getAttributeCount()}. 
 383:    * 
 384:    * @param parent  the parent attribute set (<code>null</code> not permitted).
 385:    * 
 386:    * @throws NullPointerException if <code>parent</code> is <code>null</code>.
 387:    * 
 388:    * @see #setResolveParent(AttributeSet)
 389:    */
 390:   public void setResolveParent(AttributeSet parent)
 391:   {
 392:     addAttribute(ResolveAttribute, parent);
 393:   }
 394:   
 395:   /**
 396:    * Returns a string representation of this instance, typically used for
 397:    * debugging purposes.
 398:    * 
 399:    * @return A string representation of this instance.
 400:    */
 401:   public String toString()
 402:   {
 403:     return tab.toString();
 404:   }    
 405: }