src/share/classes/javax/swing/text/SimpleAttributeSet.java

Print this page




 155      * specified attribute name/value pair.
 156      *
 157      * @param name the name
 158      * @param value the value
 159      * @return true if the name/value pair is in the list
 160      */
 161     public boolean containsAttribute(Object name, Object value) {
 162         return value.equals(getAttribute(name));
 163     }
 164 
 165     /**
 166      * Checks whether the attribute list contains all the
 167      * specified name/value pairs.
 168      *
 169      * @param attributes the attribute list
 170      * @return true if the list contains all the name/value pairs
 171      */
 172     public boolean containsAttributes(AttributeSet attributes) {
 173         boolean result = true;
 174 
 175         Enumeration names = attributes.getAttributeNames();
 176         while (result && names.hasMoreElements()) {
 177             Object name = names.nextElement();
 178             result = attributes.getAttribute(name).equals(getAttribute(name));
 179         }
 180 
 181         return result;
 182     }
 183 
 184     /**
 185      * Adds an attribute to the list.
 186      *
 187      * @param name the attribute name
 188      * @param value the attribute value
 189      */
 190     public void addAttribute(Object name, Object value) {
 191         table.put(name, value);
 192     }
 193 
 194     /**
 195      * Adds a set of attributes to the list.
 196      *
 197      * @param attributes the set of attributes to add
 198      */
 199     public void addAttributes(AttributeSet attributes) {
 200         Enumeration names = attributes.getAttributeNames();
 201         while (names.hasMoreElements()) {
 202             Object name = names.nextElement();
 203             addAttribute(name, attributes.getAttribute(name));
 204         }
 205     }
 206 
 207     /**
 208      * Removes an attribute from the list.
 209      *
 210      * @param name the attribute name
 211      */
 212     public void removeAttribute(Object name) {
 213         table.remove(name);
 214     }
 215 
 216     /**
 217      * Removes a set of attributes from the list.
 218      *
 219      * @param names the set of names to remove
 220      */
 221     public void removeAttributes(Enumeration<?> names) {
 222         while (names.hasMoreElements())
 223             removeAttribute(names.nextElement());
 224     }
 225 
 226     /**
 227      * Removes a set of attributes from the list.
 228      *
 229      * @param attributes the set of attributes to remove
 230      */
 231     public void removeAttributes(AttributeSet attributes) {
 232         if (attributes == this) {
 233             table.clear();
 234         }
 235         else {
 236             Enumeration names = attributes.getAttributeNames();
 237             while (names.hasMoreElements()) {
 238                 Object name = names.nextElement();
 239                 Object value = attributes.getAttribute(name);
 240                 if (value.equals(getAttribute(name)))
 241                     removeAttribute(name);
 242             }
 243         }
 244     }
 245 
 246     /**
 247      * Gets the resolving parent.  This is the set
 248      * of attributes to resolve through if an attribute
 249      * isn't defined locally.  This is null if there
 250      * are no other sets of attributes to resolve
 251      * through.
 252      *
 253      * @return the parent
 254      */
 255     public AttributeSet getResolveParent() {
 256         return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
 257     }
 258 
 259     /**
 260      * Sets the resolving parent.
 261      *
 262      * @param parent the parent
 263      */
 264     public void setResolveParent(AttributeSet parent) {
 265         addAttribute(StyleConstants.ResolveAttribute, parent);
 266     }
 267 
 268     // --- Object methods ---------------------------------
 269 
 270     /**
 271      * Clones a set of attributes.
 272      *
 273      * @return the new set of attributes
 274      */

 275     public Object clone() {
 276         SimpleAttributeSet attr;
 277         try {
 278             attr = (SimpleAttributeSet) super.clone();
 279             attr.table = (LinkedHashMap) table.clone();
 280         } catch (CloneNotSupportedException cnse) {
 281             attr = null;
 282         }
 283         return attr;
 284     }
 285 
 286     /**
 287      * Returns a hashcode for this set of attributes.
 288      * @return     a hashcode value for this set of attributes.
 289      */
 290     public int hashCode() {
 291         return table.hashCode();
 292     }
 293 
 294     /**


 300      *            <code>false</code> otherwise
 301      */
 302     public boolean equals(Object obj) {
 303         if (this == obj) {
 304             return true;
 305         }
 306         if (obj instanceof AttributeSet) {
 307             AttributeSet attrs = (AttributeSet) obj;
 308             return isEqual(attrs);
 309         }
 310         return false;
 311     }
 312 
 313     /**
 314      * Converts the attribute set to a String.
 315      *
 316      * @return the string
 317      */
 318     public String toString() {
 319         String s = "";
 320         Enumeration names = getAttributeNames();
 321         while (names.hasMoreElements()) {
 322             Object key = names.nextElement();
 323             Object value = getAttribute(key);
 324             if (value instanceof AttributeSet) {
 325                 // don't go recursive
 326                 s = s + key + "=**AttributeSet** ";
 327             } else {
 328                 s = s + key + "=" + value + " ";
 329             }
 330         }
 331         return s;
 332     }
 333 
 334     private void writeObject(java.io.ObjectOutputStream s) throws IOException {
 335         s.defaultWriteObject();
 336         StyleContext.writeAttributeSet(s, this);
 337     }
 338 
 339     private void readObject(ObjectInputStream s)
 340       throws ClassNotFoundException, IOException {


 347      * An AttributeSet that is always empty.
 348      */
 349     static class EmptyAttributeSet implements AttributeSet, Serializable {
 350         static final long serialVersionUID = -8714803568785904228L;
 351 
 352         public int getAttributeCount() {
 353             return 0;
 354         }
 355         public boolean isDefined(Object attrName) {
 356             return false;
 357         }
 358         public boolean isEqual(AttributeSet attr) {
 359             return (attr.getAttributeCount() == 0);
 360         }
 361         public AttributeSet copyAttributes() {
 362             return this;
 363         }
 364         public Object getAttribute(Object key) {
 365             return null;
 366         }
 367         public Enumeration getAttributeNames() {
 368             return Collections.emptyEnumeration();
 369         }
 370         public boolean containsAttribute(Object name, Object value) {
 371             return false;
 372         }
 373         public boolean containsAttributes(AttributeSet attributes) {
 374             return (attributes.getAttributeCount() == 0);
 375         }
 376         public AttributeSet getResolveParent() {
 377             return null;
 378         }
 379         public boolean equals(Object obj) {
 380             if (this == obj) {
 381                 return true;
 382             }
 383             return ((obj instanceof AttributeSet) &&
 384                     (((AttributeSet)obj).getAttributeCount() == 0));
 385         }
 386         public int hashCode() {
 387             return 0;


 155      * specified attribute name/value pair.
 156      *
 157      * @param name the name
 158      * @param value the value
 159      * @return true if the name/value pair is in the list
 160      */
 161     public boolean containsAttribute(Object name, Object value) {
 162         return value.equals(getAttribute(name));
 163     }
 164 
 165     /**
 166      * Checks whether the attribute list contains all the
 167      * specified name/value pairs.
 168      *
 169      * @param attributes the attribute list
 170      * @return true if the list contains all the name/value pairs
 171      */
 172     public boolean containsAttributes(AttributeSet attributes) {
 173         boolean result = true;
 174 
 175         Enumeration<?> names = attributes.getAttributeNames();
 176         while (result && names.hasMoreElements()) {
 177             Object name = names.nextElement();
 178             result = attributes.getAttribute(name).equals(getAttribute(name));
 179         }
 180 
 181         return result;
 182     }
 183 
 184     /**
 185      * Adds an attribute to the list.
 186      *
 187      * @param name the attribute name
 188      * @param value the attribute value
 189      */
 190     public void addAttribute(Object name, Object value) {
 191         table.put(name, value);
 192     }
 193 
 194     /**
 195      * Adds a set of attributes to the list.
 196      *
 197      * @param attributes the set of attributes to add
 198      */
 199     public void addAttributes(AttributeSet attributes) {
 200         Enumeration<?> names = attributes.getAttributeNames();
 201         while (names.hasMoreElements()) {
 202             Object name = names.nextElement();
 203             addAttribute(name, attributes.getAttribute(name));
 204         }
 205     }
 206 
 207     /**
 208      * Removes an attribute from the list.
 209      *
 210      * @param name the attribute name
 211      */
 212     public void removeAttribute(Object name) {
 213         table.remove(name);
 214     }
 215 
 216     /**
 217      * Removes a set of attributes from the list.
 218      *
 219      * @param names the set of names to remove
 220      */
 221     public void removeAttributes(Enumeration<?> names) {
 222         while (names.hasMoreElements())
 223             removeAttribute(names.nextElement());
 224     }
 225 
 226     /**
 227      * Removes a set of attributes from the list.
 228      *
 229      * @param attributes the set of attributes to remove
 230      */
 231     public void removeAttributes(AttributeSet attributes) {
 232         if (attributes == this) {
 233             table.clear();
 234         }
 235         else {
 236             Enumeration<?> names = attributes.getAttributeNames();
 237             while (names.hasMoreElements()) {
 238                 Object name = names.nextElement();
 239                 Object value = attributes.getAttribute(name);
 240                 if (value.equals(getAttribute(name)))
 241                     removeAttribute(name);
 242             }
 243         }
 244     }
 245 
 246     /**
 247      * Gets the resolving parent.  This is the set
 248      * of attributes to resolve through if an attribute
 249      * isn't defined locally.  This is null if there
 250      * are no other sets of attributes to resolve
 251      * through.
 252      *
 253      * @return the parent
 254      */
 255     public AttributeSet getResolveParent() {
 256         return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
 257     }
 258 
 259     /**
 260      * Sets the resolving parent.
 261      *
 262      * @param parent the parent
 263      */
 264     public void setResolveParent(AttributeSet parent) {
 265         addAttribute(StyleConstants.ResolveAttribute, parent);
 266     }
 267 
 268     // --- Object methods ---------------------------------
 269 
 270     /**
 271      * Clones a set of attributes.
 272      *
 273      * @return the new set of attributes
 274      */
 275     @SuppressWarnings("unchecked") // Cast of result of clone
 276     public Object clone() {
 277         SimpleAttributeSet attr;
 278         try {
 279             attr = (SimpleAttributeSet) super.clone();
 280             attr.table = (LinkedHashMap) table.clone();
 281         } catch (CloneNotSupportedException cnse) {
 282             attr = null;
 283         }
 284         return attr;
 285     }
 286 
 287     /**
 288      * Returns a hashcode for this set of attributes.
 289      * @return     a hashcode value for this set of attributes.
 290      */
 291     public int hashCode() {
 292         return table.hashCode();
 293     }
 294 
 295     /**


 301      *            <code>false</code> otherwise
 302      */
 303     public boolean equals(Object obj) {
 304         if (this == obj) {
 305             return true;
 306         }
 307         if (obj instanceof AttributeSet) {
 308             AttributeSet attrs = (AttributeSet) obj;
 309             return isEqual(attrs);
 310         }
 311         return false;
 312     }
 313 
 314     /**
 315      * Converts the attribute set to a String.
 316      *
 317      * @return the string
 318      */
 319     public String toString() {
 320         String s = "";
 321         Enumeration<?> names = getAttributeNames();
 322         while (names.hasMoreElements()) {
 323             Object key = names.nextElement();
 324             Object value = getAttribute(key);
 325             if (value instanceof AttributeSet) {
 326                 // don't go recursive
 327                 s = s + key + "=**AttributeSet** ";
 328             } else {
 329                 s = s + key + "=" + value + " ";
 330             }
 331         }
 332         return s;
 333     }
 334 
 335     private void writeObject(java.io.ObjectOutputStream s) throws IOException {
 336         s.defaultWriteObject();
 337         StyleContext.writeAttributeSet(s, this);
 338     }
 339 
 340     private void readObject(ObjectInputStream s)
 341       throws ClassNotFoundException, IOException {


 348      * An AttributeSet that is always empty.
 349      */
 350     static class EmptyAttributeSet implements AttributeSet, Serializable {
 351         static final long serialVersionUID = -8714803568785904228L;
 352 
 353         public int getAttributeCount() {
 354             return 0;
 355         }
 356         public boolean isDefined(Object attrName) {
 357             return false;
 358         }
 359         public boolean isEqual(AttributeSet attr) {
 360             return (attr.getAttributeCount() == 0);
 361         }
 362         public AttributeSet copyAttributes() {
 363             return this;
 364         }
 365         public Object getAttribute(Object key) {
 366             return null;
 367         }
 368         public Enumeration<?> getAttributeNames() {
 369             return Collections.emptyEnumeration();
 370         }
 371         public boolean containsAttribute(Object name, Object value) {
 372             return false;
 373         }
 374         public boolean containsAttributes(AttributeSet attributes) {
 375             return (attributes.getAttributeCount() == 0);
 376         }
 377         public AttributeSet getResolveParent() {
 378             return null;
 379         }
 380         public boolean equals(Object obj) {
 381             if (this == obj) {
 382                 return true;
 383             }
 384             return ((obj instanceof AttributeSet) &&
 385                     (((AttributeSet)obj).getAttributeCount() == 0));
 386         }
 387         public int hashCode() {
 388             return 0;