jdk/src/share/classes/javax/swing/text/SimpleAttributeSet.java
author malenkov
Fri, 04 Apr 2014 20:18:53 +0400
changeset 24158 6afb40c4e9f8
parent 22574 7f8ce0c8c20a
child 25193 187a455af8f8
permissions -rw-r--r--
8039137: KSS: JTextComponent.isProcessInputMethodEventOverridden Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21258
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
21258
317e884625eb 8005391: Floating behavior of HTMLEditorKit parser
alexsch
parents: 20458
diff changeset
    33
import java.util.AbstractMap;
317e884625eb 8005391: Floating behavior of HTMLEditorKit parser
alexsch
parents: 20458
diff changeset
    34
import java.util.LinkedHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A straightforward implementation of MutableAttributeSet using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    45
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Tim Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21258
diff changeset
    51
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class SimpleAttributeSet implements MutableAttributeSet, Serializable, Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = -6631553454711782652L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * An empty attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final AttributeSet EMPTY = new EmptyAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
21258
317e884625eb 8005391: Floating behavior of HTMLEditorKit parser
alexsch
parents: 20458
diff changeset
    61
    private transient LinkedHashMap<Object, Object> table = new LinkedHashMap<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a new attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public SimpleAttributeSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a new attribute set based on a supplied set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param source the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public SimpleAttributeSet(AttributeSet source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        addAttributes(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Checks whether the set of attributes is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return true if the set is empty else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public boolean isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return table.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Gets a count of the number of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return the count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return table.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Tells whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param attrName the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public boolean isDefined(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return table.containsKey(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Compares two attribute sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param attr the second attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return true if the sets are equal, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return ((getAttributeCount() == attr.getAttributeCount()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                containsAttributes(attr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Makes a copy of the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return (AttributeSet) clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Gets the names of the attributes in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return the names as an <code>Enumeration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public Enumeration<?> getAttributeNames() {
21258
317e884625eb 8005391: Floating behavior of HTMLEditorKit parser
alexsch
parents: 20458
diff changeset
   133
        return Collections.enumeration(table.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param name the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Object getAttribute(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Object value = table.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            AttributeSet parent = getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                value = parent.getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Checks whether the attribute list contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * specified attribute name/value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param name the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param value the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return true if the name/value pair is in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return value.equals(getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Checks whether the attribute list contains all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * specified name/value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param attributes the attribute list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return true if the list contains all the name/value pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public boolean containsAttributes(AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        boolean result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Enumeration names = attributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        while (result && names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            result = attributes.getAttribute(name).equals(getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Adds an attribute to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param name the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public void addAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        table.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Adds a set of attributes to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param attributes the set of attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void addAttributes(AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Enumeration names = attributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            addAttribute(name, attributes.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Removes an attribute from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param name the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public void removeAttribute(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        table.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Removes a set of attributes from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param names the set of names to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void removeAttributes(Enumeration<?> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        while (names.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            removeAttribute(names.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Removes a set of attributes from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param attributes the set of attributes to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public void removeAttributes(AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (attributes == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            table.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            Enumeration names = attributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                Object value = attributes.getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (value.equals(getAttribute(name)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    removeAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Gets the resolving parent.  This is the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * of attributes to resolve through if an attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * isn't defined locally.  This is null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * are no other sets of attributes to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Sets the resolving parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param parent the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public void setResolveParent(AttributeSet parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        addAttribute(StyleConstants.ResolveAttribute, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // --- Object methods ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Clones a set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the new set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        SimpleAttributeSet attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            attr = (SimpleAttributeSet) super.clone();
21258
317e884625eb 8005391: Floating behavior of HTMLEditorKit parser
alexsch
parents: 20458
diff changeset
   279
            attr.table = (LinkedHashMap) table.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (CloneNotSupportedException cnse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            attr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns a hashcode for this set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @return     a hashcode value for this set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return table.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Compares this object to the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * The result is <code>true</code> if the object is an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param     obj   the object to compare this attribute set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *            <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (obj instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            AttributeSet attrs = (AttributeSet) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return isEqual(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Converts the attribute set to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        String s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        Enumeration names = getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            Object key = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            Object value = getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (value instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // don't go recursive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                s = s + key + "=**AttributeSet** ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                s = s + key + "=" + value + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private void writeObject(java.io.ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        StyleContext.writeAttributeSet(s, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
      throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        s.defaultReadObject();
21258
317e884625eb 8005391: Floating behavior of HTMLEditorKit parser
alexsch
parents: 20458
diff changeset
   342
        table = new LinkedHashMap<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        StyleContext.readAttributeSet(s, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * An AttributeSet that is always empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static class EmptyAttributeSet implements AttributeSet, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        static final long serialVersionUID = -8714803568785904228L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public boolean isDefined(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return (attr.getAttributeCount() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public Enumeration getAttributeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return Collections.emptyEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        public boolean containsAttributes(AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return (attributes.getAttributeCount() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return ((obj instanceof AttributeSet) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    (((AttributeSet)obj).getAttributeCount() == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
}