FileDocCategorySizeDatePackage
SimpleAttributeSet.javaAPI DocJava SE 5 API10197Fri Aug 26 14:58:16 BST 2005javax.swing.text

SimpleAttributeSet

public class SimpleAttributeSet extends Object implements MutableAttributeSet, Serializable, Cloneable
A straightforward implementation of MutableAttributeSet using a hash table.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.41 05/05/04
author
Tim Prinzing

Fields Summary
public static final AttributeSet
EMPTY
An empty attribute set.
private transient Hashtable
table
private static Enumeration
emptyEnumeration
Constructors Summary
public SimpleAttributeSet()
Creates a new attribute set.


              
      
    
public SimpleAttributeSet(AttributeSet source)
Creates a new attribute set based on a supplied set of attributes.

param
source the set of attributes

        addAttributes(source);
    
private SimpleAttributeSet(Hashtable table)

        this.table = table;
    
Methods Summary
public voidaddAttribute(java.lang.Object name, java.lang.Object value)
Adds an attribute to the list.

param
name the attribute name
param
value the attribute value

        table.put(name, value);
    
public voidaddAttributes(javax.swing.text.AttributeSet attributes)
Adds a set of attributes to the list.

param
attributes the set of attributes to add

        Enumeration names = attributes.getAttributeNames();
        while (names.hasMoreElements()) {
            Object name = names.nextElement();
            addAttribute(name, attributes.getAttribute(name));
        }
    
public java.lang.Objectclone()
Clones a set of attributes.

return
the new set of attributes

	SimpleAttributeSet attr;
	try {
	    attr = (SimpleAttributeSet) super.clone();
	    attr.table = (Hashtable) table.clone();
	} catch (CloneNotSupportedException cnse) {
	    attr = null;
	}
        return attr;
    
public booleancontainsAttribute(java.lang.Object name, java.lang.Object value)
Checks whether the attribute list contains a specified attribute name/value pair.

param
name the name
param
value the value
return
true if the name/value pair is in the list

        return value.equals(getAttribute(name));
    
public booleancontainsAttributes(javax.swing.text.AttributeSet attributes)
Checks whether the attribute list contains all the specified name/value pairs.

param
attributes the attribute list
return
true if the list contains all the name/value pairs

        boolean result = true;

        Enumeration names = attributes.getAttributeNames();
        while (result && names.hasMoreElements()) {
            Object name = names.nextElement();
            result = attributes.getAttribute(name).equals(getAttribute(name));
        }

        return result;
    
public javax.swing.text.AttributeSetcopyAttributes()
Makes a copy of the attributes.

return
the copy

	return (AttributeSet) clone();
    
public booleanequals(java.lang.Object obj)
Compares this object to the specified object. The result is true if the object is an equivalent set of attributes.

param
obj the object to compare this attribute set with
return
true if the objects are equal; false otherwise

	if (this == obj) {
	    return true;
	}
	if (obj instanceof AttributeSet) {
	    AttributeSet attrs = (AttributeSet) obj;
	    return isEqual(attrs);
	}
	return false;
    
public java.lang.ObjectgetAttribute(java.lang.Object name)
Gets the value of an attribute.

param
name the attribute name
return
the value

        Object value = table.get(name);
	if (value == null) {
	    AttributeSet parent = getResolveParent();
	    if (parent != null) {
		value = parent.getAttribute(name);
	    }
	}
	return value;
    
public intgetAttributeCount()
Gets a count of the number of attributes.

return
the count

        return table.size();
    
public java.util.EnumerationgetAttributeNames()
Gets the names of the attributes in the set.

return
the names as an Enumeration

        return table.keys();
    
private static java.util.EnumerationgetEmptyEnumeration()

        if (emptyEnumeration == null) {
            emptyEnumeration = new Enumeration() {
                public boolean hasMoreElements() {
                    return false;
                }
                public Object nextElement() {
                    throw new NoSuchElementException("No more elements");
                }
            };
        }
        return emptyEnumeration;
    
public javax.swing.text.AttributeSetgetResolveParent()
Gets the resolving parent. This is the set of attributes to resolve through if an attribute isn't defined locally. This is null if there are no other sets of attributes to resolve through.

return
the parent

	return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
    
public inthashCode()
Returns a hashcode for this set of attributes.

return
a hashcode value for this set of attributes.

	return table.hashCode();
    
public booleanisDefined(java.lang.Object attrName)
Tells whether a given attribute is defined.

param
attrName the attribute name
return
true if the attribute is defined

	return table.containsKey(attrName);
    
public booleanisEmpty()
Checks whether the set of attributes is empty.

return
true if the set is empty else false

        return table.isEmpty();
    
public booleanisEqual(javax.swing.text.AttributeSet attr)
Compares two attribute sets.

param
attr the second attribute set
return
true if the sets are equal, false otherwise

	return ((getAttributeCount() == attr.getAttributeCount()) &&
		containsAttributes(attr));
    
private voidreadObject(java.io.ObjectInputStream s)

        s.defaultReadObject();
	table = new Hashtable(3);
	StyleContext.readAttributeSet(s, this);
    
public voidremoveAttribute(java.lang.Object name)
Removes an attribute from the list.

param
name the attribute name

        table.remove(name);
    
public voidremoveAttributes(java.util.Enumeration names)
Removes a set of attributes from the list.

param
names the set of names to remove

        while (names.hasMoreElements())
            removeAttribute(names.nextElement());
    
public voidremoveAttributes(javax.swing.text.AttributeSet attributes)
Removes a set of attributes from the list.

param
attributes the set of attributes to remove

	if (attributes == this) {
	    table.clear();
	}
	else {
	    Enumeration names = attributes.getAttributeNames();
	    while (names.hasMoreElements()) {
		Object name = names.nextElement();
		Object value = attributes.getAttribute(name);
		if (value.equals(getAttribute(name)))
		    removeAttribute(name);
	    }
	}
    
public voidsetResolveParent(javax.swing.text.AttributeSet parent)
Sets the resolving parent.

param
parent the parent

	addAttribute(StyleConstants.ResolveAttribute, parent);
    
public java.lang.StringtoString()
Converts the attribute set to a String.

return
the string

	String s = "";
        Enumeration names = getAttributeNames();
        while (names.hasMoreElements()) {
            Object key = names.nextElement();
            Object value = getAttribute(key);
	    if (value instanceof AttributeSet) {
		// don't go recursive
		s = s + key + "=**AttributeSet** ";
	    } else {
		s = s + key + "=" + value + " ";
	    }
	}
	return s;
    
private voidwriteObject(java.io.ObjectOutputStream s)

        s.defaultWriteObject();
	StyleContext.writeAttributeSet(s, this);