Source for javax.swing.text.SimpleAttributeSet

   1: /* SimpleAttributeSet.java --
   2:    Copyright (C) 2004, 2005 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: public class SimpleAttributeSet
  46:   implements MutableAttributeSet, Serializable, Cloneable
  47: {
  48:   /** The serialization UID (compatible with JDK1.5). */
  49:   private static final long serialVersionUID = 8267656273837665219L;
  50: 
  51:   public static final AttributeSet EMPTY = new SimpleAttributeSet();
  52: 
  53:   Hashtable tab;
  54: 
  55:   public SimpleAttributeSet()
  56:   {
  57:     this(null);
  58:   }
  59:   
  60:   public SimpleAttributeSet(AttributeSet a)
  61:   {
  62:     tab = new Hashtable();
  63:     if (a != null)
  64:       addAttributes(a);
  65:   }
  66: 
  67:   public void addAttribute(Object name, Object value)
  68:   {
  69:     tab.put(name, value);
  70:   }
  71: 
  72:   public void addAttributes(AttributeSet attributes)
  73:   {
  74:     Enumeration e = attributes.getAttributeNames();
  75:     while (e.hasMoreElements())
  76:       {
  77:         Object name = e.nextElement();
  78:         Object val = attributes.getAttribute(name);
  79:         tab.put(name, val);
  80:       }
  81:   }
  82: 
  83:   public Object clone()
  84:   {
  85:     SimpleAttributeSet s = new SimpleAttributeSet();
  86:     s.tab = (Hashtable) tab.clone();
  87:     return s;
  88:   }
  89: 
  90:   /**
  91:    * Returns true if the given name and value represent an attribute
  92:    * found either in this AttributeSet or in its resolve parent hierarchy.
  93:    * @param name the key for the attribute
  94:    * @param value the value for the attribute
  95:    * @return true if the attribute is found here or in this set's resolve
  96:    * parent hierarchy
  97:    */
  98:   public boolean containsAttribute(Object name, Object value)
  99:   {
 100:     return (tab.containsKey(name) && tab.get(name).equals(value)) || 
 101:       (getResolveParent() != null && getResolveParent().
 102:        containsAttribute(name, value));
 103:   }
 104:   
 105:   /**
 106:    * Returns true if the given name and value are found in this AttributeSet.
 107:    * Does not check the resolve parent.
 108:    * @param name the key for the attribute
 109:    * @param value the value for the attribute
 110:    * @return true if the attribute is found in this AttributeSet
 111:    */
 112:   boolean containsAttributeLocally(Object name, Object value)
 113:   {
 114:     return tab.containsKey(name) 
 115:       && tab.get(name).equals(value);
 116:   }
 117: 
 118:   public boolean containsAttributes(AttributeSet attributes)
 119:   {
 120:     Enumeration e = attributes.getAttributeNames();
 121:     while (e.hasMoreElements())
 122:       {
 123:         Object name = e.nextElement();
 124:         Object val = attributes.getAttribute(name);
 125:         if (! containsAttribute(name, val))
 126:           return false;        
 127:       }
 128:     return true;
 129:   }
 130: 
 131:   public AttributeSet copyAttributes()
 132:   {
 133:     return (AttributeSet) clone();
 134:   }
 135: 
 136:   public boolean equals(Object obj)
 137:   {
 138:     return 
 139:       (obj instanceof AttributeSet)
 140:       && this.isEqual((AttributeSet) obj);
 141:   }
 142: 
 143:   public Object getAttribute(Object name)
 144:   {
 145:     Object val = tab.get(name);
 146:     if (val != null) 
 147:       return val;
 148: 
 149:     Object p = getResolveParent();
 150:     if (p != null && p instanceof AttributeSet)
 151:       return (((AttributeSet)p).getAttribute(name));
 152: 
 153:     return null;
 154:   }
 155: 
 156:   public int getAttributeCount()
 157:   {
 158:     return tab.size();
 159:   }
 160: 
 161:   public Enumeration getAttributeNames()
 162:   {
 163:     return tab.keys();
 164:   }
 165: 
 166:   public AttributeSet getResolveParent()
 167:   {
 168:     return (AttributeSet) tab.get(ResolveAttribute);
 169:   }
 170: 
 171:   public int hashCode()
 172:   {
 173:     return tab.hashCode();
 174:   }
 175: 
 176:   public boolean isDefined(Object attrName)
 177:   {
 178:     return tab.containsKey(attrName);
 179:   }
 180: 
 181:   public boolean isEmpty()
 182:   {
 183:     return tab.isEmpty();    
 184:   }
 185: 
 186:   /**
 187:    * Returns true if the given set has the same number of attributes
 188:    * as this set and <code>containsAttributes(attr)</code> returns
 189:    * true.
 190:    */
 191:   public boolean isEqual(AttributeSet attr)
 192:   {
 193:     return getAttributeCount() == attr.getAttributeCount()
 194:       && this.containsAttributes(attr);
 195:   }
 196:     
 197:   public void removeAttribute(Object name)
 198:   {
 199:     tab.remove(name);
 200:   }
 201: 
 202:   /**
 203:    * Removes attributes from this set if they are found in the 
 204:    * given set.  Only attributes whose key AND value are removed.
 205:    * Removes attributes only from this set, not from the resolving parent.
 206:    */
 207:   public void removeAttributes(AttributeSet attributes)
 208:   {
 209:     Enumeration e = attributes.getAttributeNames();
 210:     while (e.hasMoreElements())
 211:       {
 212:         Object name = e.nextElement();
 213:         Object val = attributes.getAttribute(name);
 214:         if (containsAttributeLocally(name, val))
 215:           removeAttribute(name);     
 216:       }
 217:   }
 218: 
 219:   public void removeAttributes(Enumeration names)
 220:   {
 221:     while (names.hasMoreElements())
 222:       {
 223:         removeAttribute(names.nextElement());
 224:       }    
 225:   }
 226: 
 227:   public void setResolveParent(AttributeSet parent)
 228:   {
 229:     addAttribute(ResolveAttribute, parent);
 230:   }
 231:     
 232:   public String toString()
 233:   {
 234:     return tab.toString();
 235:   }    
 236: }