0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Collections;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * A straightforward implementation of MutableAttributeSet using a
0N/A * hash table.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @author Tim Prinzing
0N/A */
0N/Apublic class SimpleAttributeSet implements MutableAttributeSet, Serializable, Cloneable
0N/A{
0N/A private static final long serialVersionUID = -6631553454711782652L;
0N/A
0N/A /**
0N/A * An empty attribute set.
0N/A */
0N/A public static final AttributeSet EMPTY = new EmptyAttributeSet();
0N/A
611N/A private transient Hashtable<Object, Object> table = new Hashtable<Object, Object>(3);
0N/A
0N/A /**
0N/A * Creates a new attribute set.
0N/A */
0N/A public SimpleAttributeSet() {
0N/A }
0N/A
0N/A /**
0N/A * Creates a new attribute set based on a supplied set of attributes.
0N/A *
0N/A * @param source the set of attributes
0N/A */
0N/A public SimpleAttributeSet(AttributeSet source) {
0N/A addAttributes(source);
0N/A }
0N/A
611N/A private SimpleAttributeSet(Hashtable<Object, Object> table) {
0N/A this.table = table;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the set of attributes is empty.
0N/A *
0N/A * @return true if the set is empty else false
0N/A */
0N/A public boolean isEmpty()
0N/A {
0N/A return table.isEmpty();
0N/A }
0N/A
0N/A /**
0N/A * Gets a count of the number of attributes.
0N/A *
0N/A * @return the count
0N/A */
0N/A public int getAttributeCount() {
0N/A return table.size();
0N/A }
0N/A
0N/A /**
0N/A * Tells whether a given attribute is defined.
0N/A *
0N/A * @param attrName the attribute name
0N/A * @return true if the attribute is defined
0N/A */
0N/A public boolean isDefined(Object attrName) {
0N/A return table.containsKey(attrName);
0N/A }
0N/A
0N/A /**
0N/A * Compares two attribute sets.
0N/A *
0N/A * @param attr the second attribute set
0N/A * @return true if the sets are equal, false otherwise
0N/A */
0N/A public boolean isEqual(AttributeSet attr) {
0N/A return ((getAttributeCount() == attr.getAttributeCount()) &&
0N/A containsAttributes(attr));
0N/A }
0N/A
0N/A /**
0N/A * Makes a copy of the attributes.
0N/A *
0N/A * @return the copy
0N/A */
0N/A public AttributeSet copyAttributes() {
0N/A return (AttributeSet) clone();
0N/A }
0N/A
0N/A /**
0N/A * Gets the names of the attributes in the set.
0N/A *
0N/A * @return the names as an <code>Enumeration</code>
0N/A */
0N/A public Enumeration<?> getAttributeNames() {
0N/A return table.keys();
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of an attribute.
0N/A *
0N/A * @param name the attribute name
0N/A * @return the value
0N/A */
0N/A public Object getAttribute(Object name) {
0N/A Object value = table.get(name);
0N/A if (value == null) {
0N/A AttributeSet parent = getResolveParent();
0N/A if (parent != null) {
0N/A value = parent.getAttribute(name);
0N/A }
0N/A }
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the attribute list contains a
0N/A * specified attribute name/value pair.
0N/A *
0N/A * @param name the name
0N/A * @param value the value
0N/A * @return true if the name/value pair is in the list
0N/A */
0N/A public boolean containsAttribute(Object name, Object value) {
0N/A return value.equals(getAttribute(name));
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the attribute list contains all the
0N/A * specified name/value pairs.
0N/A *
0N/A * @param attributes the attribute list
0N/A * @return true if the list contains all the name/value pairs
0N/A */
0N/A public boolean containsAttributes(AttributeSet attributes) {
0N/A boolean result = true;
0N/A
0N/A Enumeration names = attributes.getAttributeNames();
0N/A while (result && names.hasMoreElements()) {
0N/A Object name = names.nextElement();
0N/A result = attributes.getAttribute(name).equals(getAttribute(name));
0N/A }
0N/A
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Adds an attribute to the list.
0N/A *
0N/A * @param name the attribute name
0N/A * @param value the attribute value
0N/A */
0N/A public void addAttribute(Object name, Object value) {
0N/A table.put(name, value);
0N/A }
0N/A
0N/A /**
0N/A * Adds a set of attributes to the list.
0N/A *
0N/A * @param attributes the set of attributes to add
0N/A */
0N/A public void addAttributes(AttributeSet attributes) {
0N/A Enumeration names = attributes.getAttributeNames();
0N/A while (names.hasMoreElements()) {
0N/A Object name = names.nextElement();
0N/A addAttribute(name, attributes.getAttribute(name));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes an attribute from the list.
0N/A *
0N/A * @param name the attribute name
0N/A */
0N/A public void removeAttribute(Object name) {
0N/A table.remove(name);
0N/A }
0N/A
0N/A /**
0N/A * Removes a set of attributes from the list.
0N/A *
0N/A * @param names the set of names to remove
0N/A */
0N/A public void removeAttributes(Enumeration<?> names) {
0N/A while (names.hasMoreElements())
0N/A removeAttribute(names.nextElement());
0N/A }
0N/A
0N/A /**
0N/A * Removes a set of attributes from the list.
0N/A *
0N/A * @param attributes the set of attributes to remove
0N/A */
0N/A public void removeAttributes(AttributeSet attributes) {
0N/A if (attributes == this) {
0N/A table.clear();
0N/A }
0N/A else {
0N/A Enumeration names = attributes.getAttributeNames();
0N/A while (names.hasMoreElements()) {
0N/A Object name = names.nextElement();
0N/A Object value = attributes.getAttribute(name);
0N/A if (value.equals(getAttribute(name)))
0N/A removeAttribute(name);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the resolving parent. This is the set
0N/A * of attributes to resolve through if an attribute
0N/A * isn't defined locally. This is null if there
0N/A * are no other sets of attributes to resolve
0N/A * through.
0N/A *
0N/A * @return the parent
0N/A */
0N/A public AttributeSet getResolveParent() {
0N/A return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
0N/A }
0N/A
0N/A /**
0N/A * Sets the resolving parent.
0N/A *
0N/A * @param parent the parent
0N/A */
0N/A public void setResolveParent(AttributeSet parent) {
0N/A addAttribute(StyleConstants.ResolveAttribute, parent);
0N/A }
0N/A
0N/A // --- Object methods ---------------------------------
0N/A
0N/A /**
0N/A * Clones a set of attributes.
0N/A *
0N/A * @return the new set of attributes
0N/A */
0N/A public Object clone() {
0N/A SimpleAttributeSet attr;
0N/A try {
0N/A attr = (SimpleAttributeSet) super.clone();
0N/A attr.table = (Hashtable) table.clone();
0N/A } catch (CloneNotSupportedException cnse) {
0N/A attr = null;
0N/A }
0N/A return attr;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this set of attributes.
0N/A * @return a hashcode value for this set of attributes.
0N/A */
0N/A public int hashCode() {
0N/A return table.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Compares this object to the specified object.
0N/A * The result is <code>true</code> if the object is an equivalent
0N/A * set of attributes.
0N/A * @param obj the object to compare this attribute set with
0N/A * @return <code>true</code> if the objects are equal;
0N/A * <code>false</code> otherwise
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) {
0N/A return true;
0N/A }
0N/A if (obj instanceof AttributeSet) {
0N/A AttributeSet attrs = (AttributeSet) obj;
0N/A return isEqual(attrs);
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Converts the attribute set to a String.
0N/A *
0N/A * @return the string
0N/A */
0N/A public String toString() {
0N/A String s = "";
0N/A Enumeration names = getAttributeNames();
0N/A while (names.hasMoreElements()) {
0N/A Object key = names.nextElement();
0N/A Object value = getAttribute(key);
0N/A if (value instanceof AttributeSet) {
0N/A // don't go recursive
0N/A s = s + key + "=**AttributeSet** ";
0N/A } else {
0N/A s = s + key + "=" + value + " ";
0N/A }
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A private void writeObject(java.io.ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A StyleContext.writeAttributeSet(s, this);
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException {
0N/A s.defaultReadObject();
611N/A table = new Hashtable<Object, Object>(3);
0N/A StyleContext.readAttributeSet(s, this);
0N/A }
0N/A
0N/A /**
0N/A * An AttributeSet that is always empty.
0N/A */
0N/A static class EmptyAttributeSet implements AttributeSet, Serializable {
0N/A static final long serialVersionUID = -8714803568785904228L;
0N/A
0N/A public int getAttributeCount() {
0N/A return 0;
0N/A }
0N/A public boolean isDefined(Object attrName) {
0N/A return false;
0N/A }
0N/A public boolean isEqual(AttributeSet attr) {
0N/A return (attr.getAttributeCount() == 0);
0N/A }
0N/A public AttributeSet copyAttributes() {
0N/A return this;
0N/A }
0N/A public Object getAttribute(Object key) {
0N/A return null;
0N/A }
0N/A public Enumeration getAttributeNames() {
0N/A return Collections.emptyEnumeration();
0N/A }
0N/A public boolean containsAttribute(Object name, Object value) {
0N/A return false;
0N/A }
0N/A public boolean containsAttributes(AttributeSet attributes) {
0N/A return (attributes.getAttributeCount() == 0);
0N/A }
0N/A public AttributeSet getResolveParent() {
0N/A return null;
0N/A }
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) {
0N/A return true;
0N/A }
0N/A return ((obj instanceof AttributeSet) &&
0N/A (((AttributeSet)obj).getAttributeCount() == 0));
0N/A }
0N/A public int hashCode() {
0N/A return 0;
0N/A }
0N/A }
0N/A}