jdk/src/share/classes/javax/swing/text/StyleContext.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1287 a04aca99c77a
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.ChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.EventListenerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.ChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.font.FontManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A pool of styles and their associated resources.  This class determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the lifetime of a group of resources by being a container that holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * caches for various resources such as font and color that get reused
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by the various style definitions.  This can be shared by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * documents if desired to maximize the sharing of related resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class also provides efficient support for small sets of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and compresses them by sharing across uses and taking advantage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * their immutable nature.  Since many styles are replicated, the potential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * for sharing is significant, and copies can be extremely cheap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Larger sets reduce the possibility of sharing, and therefore revert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * automatically to a less space-efficient implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class StyleContext implements Serializable, AbstractDocument.AttributeContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Returns default AttributeContext shared by all documents that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * don't bother to define/supply their own context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final StyleContext getDefaultStyleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (defaultContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            defaultContext = new StyleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return defaultContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static StyleContext defaultContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates a new StyleContext object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public StyleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        styles = new NamedStyle(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        addStyle(DEFAULT_STYLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Adds a new style into the style hierarchy.  Style attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * resolve from bottom up so an attribute specified in a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * will override an attribute specified in the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param nm   the name of the style (must be unique within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *   collection of named styles in the document).  The name may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *   be null if the style is unnamed, but the caller is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *   for managing the reference returned as an unnamed style can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *   be fetched by name.  An unnamed style may be useful for things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *   like character attribute overrides such as found in a style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *   run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param parent the parent style.  This may be null if unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *   attributes need not be resolved in some other style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return the created style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public Style addStyle(String nm, Style parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Style style = new NamedStyle(nm, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (nm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // add a named style, a class of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            styles.addAttribute(nm, style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Removes a named style previously added to the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param nm  the name of the style to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void removeStyle(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        styles.removeAttribute(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Fetches a named style previously added to the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param nm  the name of the style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return the style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Style getStyle(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (Style) styles.getAttribute(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Fetches the names of the styles defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the list of names as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public Enumeration<?> getStyleNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return styles.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Adds a listener to track when styles are added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * or removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param l the change listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        styles.addChangeListener(l);
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
     * Removes a listener that was tracking styles being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * added or removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param l the change listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        styles.removeChangeListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * to this StyleContext with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public ChangeListener[] getChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return ((NamedStyle)styles).getChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Gets the font from an attribute set.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * implemented to try and fetch a cached font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * for the given AttributeSet, and if that fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * the font features are resolved and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * font is fetched from the low-level font cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param attr the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public Font getFont(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // PENDING(prinz) add cache behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int style = Font.PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (StyleConstants.isBold(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            style |= Font.BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (StyleConstants.isItalic(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            style |= Font.ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        String family = StyleConstants.getFontFamily(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int size = StyleConstants.getFontSize(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * if either superscript or subscript is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * is set, we need to reduce the font size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * by 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (StyleConstants.isSuperscript(attr) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            StyleConstants.isSubscript(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            size -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return getFont(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Takes a set of attributes and turn it into a foreground color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * specification.  This might be used to specify things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * like brighter, more hue, etc.  By default it simply returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the value specified by the StyleConstants.Foreground attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param attr the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public Color getForeground(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return StyleConstants.getForeground(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Takes a set of attributes and turn it into a background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * specification.  This might be used to specify things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * like brighter, more hue, etc.  By default it simply returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * the value specified by the StyleConstants.Background attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param attr the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public Color getBackground(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return StyleConstants.getBackground(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Gets a new font.  This returns a Font from a cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * if a cached font exists.  If not, a Font is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * the cache.  This is basically a low-level cache for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * 1.1 font features.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param family the font family (such as "Monospaced")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param style the style of the font (such as Font.PLAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param size the point size >= 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return the new font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public Font getFont(String family, int style, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        fontSearch.setValue(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Font f = (Font) fontTable.get(fontSearch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // haven't seen this one yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            Style defaultStyle =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (defaultStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                Font defaultFont =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    (Font) defaultStyle.getAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if (defaultFont != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                      && defaultFont.getFamily().equalsIgnoreCase(family)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    f = defaultFont.deriveFont(style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                f = new Font(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (! FontManager.fontSupportsDefaultEncoding(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                f = FontManager.getCompositeFontUIResource(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            FontKey key = new FontKey(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            fontTable.put(key, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns font metrics for a font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param f the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return the metrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public FontMetrics getFontMetrics(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // The Toolkit implementations cache, so we just forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // to the default toolkit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return Toolkit.getDefaultToolkit().getFontMetrics(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // --- AttributeContext methods --------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Adds an attribute to the given set, and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * the new representative set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public synchronized AttributeSet addAttribute(AttributeSet old, Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if ((old.getAttributeCount() + 1) <= getCompressionThreshold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // build a search key and find/create an immutable and unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            // set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            search.removeAttributes(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            search.addAttributes(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            search.addAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            reclaim(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return getImmutableUniqueSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        MutableAttributeSet ma = getMutableAttributeSet(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        ma.addAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return ma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Adds a set of attributes to the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param attr the attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public synchronized AttributeSet addAttributes(AttributeSet old, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if ((old.getAttributeCount() + attr.getAttributeCount()) <= getCompressionThreshold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // build a search key and find/create an immutable and unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            search.removeAttributes(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            search.addAttributes(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            search.addAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            reclaim(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return getImmutableUniqueSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        MutableAttributeSet ma = getMutableAttributeSet(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        ma.addAttributes(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return ma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Removes an attribute from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param old the old set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @see MutableAttributeSet#removeAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public synchronized AttributeSet removeAttribute(AttributeSet old, Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if ((old.getAttributeCount() - 1) <= getCompressionThreshold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // build a search key and find/create an immutable and unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            search.removeAttributes(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            search.addAttributes(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            search.removeAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            reclaim(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return getImmutableUniqueSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        MutableAttributeSet ma = getMutableAttributeSet(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        ma.removeAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return ma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param names the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public synchronized AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (old.getAttributeCount() <= getCompressionThreshold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // build a search key and find/create an immutable and unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            search.removeAttributes(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            search.addAttributes(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            search.removeAttributes(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            reclaim(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return getImmutableUniqueSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        MutableAttributeSet ma = getMutableAttributeSet(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        ma.removeAttributes(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return ma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param attrs the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public synchronized AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (old.getAttributeCount() <= getCompressionThreshold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // build a search key and find/create an immutable and unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            // set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            search.removeAttributes(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            search.addAttributes(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            search.removeAttributes(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            reclaim(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return getImmutableUniqueSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        MutableAttributeSet ma = getMutableAttributeSet(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        ma.removeAttributes(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return ma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Fetches an empty AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @return the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public AttributeSet getEmptySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return SimpleAttributeSet.EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Returns a set no longer needed by the MutableAttributeSet implmentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * This is useful for operation under 1.1 where there are no weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * references.  This would typically be called by the finalize method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * of the MutableAttributeSet implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * This method is thread safe, although most Swing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * are not. Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param a the set to reclaim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public void reclaim(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            attributesPool.size(); // force WeakHashMap to expunge stale entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // if current thread is not event dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // do not bother with expunging stale entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    // --- local methods -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Returns the maximum number of key/value pairs to try and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * compress into unique/immutable sets.  Any sets above this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * limit will use hashtables and be a MutableAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return the threshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    protected int getCompressionThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return THRESHOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Create a compact set of attributes that might be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * This is a hook for subclasses that want to alter the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * behavior of SmallAttributeSet.  This can be reimplemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * to return an AttributeSet that provides some sort of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * attribute conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param a The set of attributes to be represented in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *  the compact form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return new SmallAttributeSet(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Create a large set of attributes that should trade off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * space for time.  This set will not be shared.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * a hook for subclasses that want to alter the behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * of the larger attribute storage format (which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * SimpleAttributeSet by default).   This can be reimplemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * to return a MutableAttributeSet that provides some sort of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * attribute conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param a The set of attributes to be represented in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *  the larger form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    protected MutableAttributeSet createLargeAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return new SimpleAttributeSet(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Clean the unused immutable sets out of the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    synchronized void removeUnusedSets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        attributesPool.size(); // force WeakHashMap to expunge stale entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Search for an existing attribute set using the current search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * parameters.  If a matching set is found, return it.  If a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * is not found, we create a new set and add it to the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    AttributeSet getImmutableUniqueSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        // PENDING(prinz) should consider finding a alternative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // generating extra garbage on search key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        SmallAttributeSet key = createSmallAttributeSet(search);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        WeakReference reference = (WeakReference)attributesPool.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        SmallAttributeSet a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (reference == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            || (a = (SmallAttributeSet)reference.get()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            a = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            attributesPool.put(a, new WeakReference(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Creates a mutable attribute set to hand out because the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * needs are too big to try and use a shared version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    MutableAttributeSet getMutableAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (a instanceof MutableAttributeSet &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            a != SimpleAttributeSet.EMPTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return (MutableAttributeSet) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return createLargeAttributeSet(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Converts a StyleContext to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        removeUnusedSets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        String s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        Iterator iterator = attributesPool.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            SmallAttributeSet set = (SmallAttributeSet)iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            s = s + set + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Context-specific handling of writing out attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public void writeAttributes(ObjectOutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                  AttributeSet a) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        writeAttributeSet(out, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Context-specific handling of reading in attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public void readAttributes(ObjectInputStream in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                               MutableAttributeSet a) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        readAttributeSet(in, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Writes a set of attributes to the given object stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * for the purpose of serialization.  This will take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * special care to deal with static attribute keys that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * have been registered wit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <code>registerStaticAttributeKey</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Any attribute key not regsitered as a static key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * will be serialized directly.  All values are expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * to be serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @param a the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public static void writeAttributeSet(ObjectOutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                         AttributeSet a) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        int n = a.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        out.writeInt(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        Enumeration keys = a.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (key instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                out.writeObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                Object ioFmt = freezeKeyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                if (ioFmt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    throw new NotSerializableException(key.getClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                 getName() + " is not serializable as a key in an AttributeSet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                out.writeObject(ioFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            Object value = a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Object ioFmt = freezeKeyMap.get(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (value instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                out.writeObject((ioFmt != null) ? ioFmt : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (ioFmt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    throw new NotSerializableException(value.getClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                 getName() + " is not serializable as a value in an AttributeSet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                out.writeObject(ioFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Reads a set of attributes from the given object input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * stream that have been previously written out with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <code>writeAttributeSet</code>.  This will try to restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * keys that were static objects to the static objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * the current virtual machine considering only those keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * that have been registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <code>registerStaticAttributeKey</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * The attributes retrieved from the stream will be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * into the given mutable set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param in the object stream to read the attribute data from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param a  the attribute set to place the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *   definitions in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @exception ClassNotFoundException passed upward if encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *  when reading the object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @exception IOException passed upward if encountered when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *  reading the object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public static void readAttributeSet(ObjectInputStream in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        MutableAttributeSet a) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        int n = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            Object key = in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            Object value = in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (thawKeyMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                Object staticKey = thawKeyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (staticKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    key = staticKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                Object staticValue = thawKeyMap.get(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                if (staticValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    value = staticValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            a.addAttribute(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Registers an object as a static object that is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * used as a key in attribute sets.  This allows the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * to be treated specially for serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * For operation under a 1.1 virtual machine, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * uses the value returned by <code>toString</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * concatenated to the classname.  The value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * by toString should not have the class reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * in it (ie it should be reimplemented from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * definition in Object) in order to be the same when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * recomputed later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param key the non-null object key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public static void registerStaticAttributeKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        String ioFmt = key.getClass().getName() + "." + key.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (freezeKeyMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            freezeKeyMap = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            thawKeyMap = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        freezeKeyMap.put(key, ioFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        thawKeyMap.put(ioFmt, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Returns the object previously registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * <code>registerStaticAttributeKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public static Object getStaticAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (thawKeyMap == null || key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return thawKeyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Returns the String that <code>key</code> will be registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @see #getStaticAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @see #registerStaticAttributeKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public static Object getStaticAttributeKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return key.getClass().getName() + "." + key.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // clean out unused sets before saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        removeUnusedSets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
      throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        fontSearch = new FontKey(null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        fontTable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        search = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        attributesPool = Collections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            synchronizedMap(new WeakHashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    // --- variables ---------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * The name given to the default logical style attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * to paragraphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public static final String DEFAULT_STYLE = "default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    private static Hashtable freezeKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private static Hashtable thawKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private Style styles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    private transient FontKey fontSearch = new FontKey(null, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    private transient Hashtable fontTable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    private transient Map attributesPool = Collections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        synchronizedMap(new WeakHashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    private transient MutableAttributeSet search = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Number of immutable sets that are not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * being used.  This helps indicate when the sets need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * to be cleaned out of the hashtable they are stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    private int unusedSets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * The threshold for no longer sharing the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * in an immutable table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    static final int THRESHOLD = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * This class holds a small number of attributes in an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * The storage format is key, value, key, value, etc.  The size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * of the set is the length of the array divided by two.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * default, this is the class that will be used to store attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * when held in the compact sharable form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public class SmallAttributeSet implements AttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        public SmallAttributeSet(Object[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            this.attributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            updateResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        public SmallAttributeSet(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            int n = attrs.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            Object[] tbl = new Object[2 * n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            Enumeration names = attrs.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                tbl[i] = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                tbl[i+1] = attrs.getAttribute(tbl[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            attributes = tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            updateResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        private void updateResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            resolveParent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            for (int i = 0; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                if (tbl[i] == StyleConstants.ResolveAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    resolveParent = (AttributeSet)tbl[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        Object getLocalAttribute(Object nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (nm == StyleConstants.ResolveAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                return resolveParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            for (int i = 0; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                if (nm.equals(tbl[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    return tbl[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        // --- Object methods -------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         * Returns a string showing the key/value pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            String s = "{";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            for (int i = 0; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                if (tbl[i+1] instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    // don't recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    s = s + tbl[i] + "=" + "AttributeSet" + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    s = s + tbl[i] + "=" + tbl[i+1] + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            s = s + "}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         * Returns a hashcode for this set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         * @return     a hashcode value for this set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            int code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            for (int i = 1; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                code ^= tbl[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * Compares this object to the specifed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         * The result is <code>true</code> if the object is an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         * set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * @param     obj   the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (obj instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                AttributeSet attrs = (AttributeSet) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                return ((getAttributeCount() == attrs.getAttributeCount()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        containsAttributes(attrs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * Clones a set of attributes.  Since the set is immutable, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         * clone is basically the same set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
         * @return the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        //  --- AttributeSet methods ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         * Gets the number of attributes that are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         * @return the number of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         * @see AttributeSet#getAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return attributes.length / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         * @param key the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        public boolean isDefined(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            Object[] a = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            int n = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            for (int i = 0; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                if (key.equals(a[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * Checks whether two attribute sets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         * @param attr the attribute set to check against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * @return true if the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         * @see AttributeSet#isEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (attr instanceof SmallAttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                return attr == this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            return ((getAttributeCount() == attr.getAttributeCount()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    containsAttributes(attr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         * Copies a set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * @see AttributeSet#copyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         * @param key the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        public Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            Object value = getLocalAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                AttributeSet parent = getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                if (parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    value = parent.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * Gets the names of all attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         * @return the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         * @see AttributeSet#getAttributeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        public Enumeration<?> getAttributeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return new KeyEnumeration(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * Checks whether a given attribute name/value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * @param name the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
         * @return true if the name/value is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
         * @see AttributeSet#containsAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            return value.equals(getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * Checks whether the attribute set contains all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * the given attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         * @param attrs the attributes to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
         * @return true if the element contains all the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
         * @see AttributeSet#containsAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        public boolean containsAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            boolean result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            Enumeration names = attrs.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            while (result && names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                result = attrs.getAttribute(name).equals(getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * If not overriden, the resolving parent defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * the parent element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * @return the attributes from the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            return resolveParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // --- variables -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        Object[] attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        // This is also stored in attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        AttributeSet resolveParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * An enumeration of the keys in a SmallAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    class KeyEnumeration implements Enumeration<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        KeyEnumeration(Object[] attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            this.attr = attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         * Tests if this enumeration contains more elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         * @return  <code>true</code> if this enumeration contains more elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
         * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            return i < attr.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         * Returns the next element of this enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
         * @return     the next element of this enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         * @exception  NoSuchElementException  if no more elements exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
         * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (i < attr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                Object o = attr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        Object[] attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * Sorts the key strings so that they can be very quickly compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * in the attribute set searchs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    class KeyBuilder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        public void initialize(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            if (a instanceof SmallAttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                initialize(((SmallAttributeSet)a).attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                keys.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                data.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                Enumeration names = a.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    addAttribute(name, a.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
         * Initialize with a set of already sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
         * keys (data from an existing SmallAttributeSet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        private void initialize(Object[] sorted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            keys.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            data.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            int n = sorted.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            for (int i = 0; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                keys.addElement(sorted[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                data.addElement(sorted[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * Creates a table of sorted key/value entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * suitable for creation of an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         * SmallAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        public Object[] createTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            int n = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            Object[] tbl = new Object[2 * n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            for (int i = 0; i < n; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                int offs = 2 * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                tbl[offs] = keys.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                tbl[offs + 1] = data.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            return tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         * The number of key/value pairs contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         * in the current key being forged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        int getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            return keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
         * Adds a key/value to the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        public void addAttribute(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            keys.addElement(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            data.addElement(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
         * Adds a set of key/value pairs to the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        public void addAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (attr instanceof SmallAttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                // avoid searching the keys, they are already interned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                Object[] tbl = ((SmallAttributeSet)attr).attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                int n = tbl.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                for (int i = 0; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    addAttribute(tbl[i], tbl[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    addAttribute(name, attr.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         * Removes the given name from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        public void removeAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            int n = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                if (keys.elementAt(i).equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    keys.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    data.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
         * Removes the set of keys from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        public void removeAttributes(Enumeration names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                removeAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * Removes the set of matching attributes from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        public void removeAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                Object value = attr.getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                removeSearchAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        private void removeSearchAttribute(Object ikey, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            int n = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                if (keys.elementAt(i).equals(ikey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    if (data.elementAt(i).equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                        keys.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                        data.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        private Vector keys = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        private Vector data = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * key for a font table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    static class FontKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        private String family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        private int style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
         * Constructs a font key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        public FontKey(String family, int style, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            setValue(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        public void setValue(String family, int style, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            this.family = (family != null) ? family.intern() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            this.style = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
         * Returns a hashcode for this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         * @return     a hashcode value for this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            int fhash = (family != null) ? family.hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            return fhash ^ style ^ size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
         * Compares this object to the specifed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         * The result is <code>true</code> if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
         * <code>null</code> and is a <code>Font</code> object with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
         * name, style, and point size as this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         * @param     obj   the object to compare this font with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
         * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            if (obj instanceof FontKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                FontKey font = (FontKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                return (size == font.size) && (style == font.style) && (family == font.family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * A collection of attributes, typically used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * character and paragraph styles.  This is an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * of MutableAttributeSet that can be observed if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * These styles will take advantage of immutability while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * the sets are small enough, and may be substantially more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * efficient than something like SimpleAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    public class NamedStyle implements Style, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * Creates a new named style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
         * @param name the style name, null for unnamed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
         * @param parent the parent style, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        public NamedStyle(String name, Style parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            attributes = getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                setName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                setResolveParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         * Creates a new named style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         * @param parent the parent style, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        public NamedStyle(Style parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            this(null, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
         * Creates a new named style, with a null name and parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        public NamedStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            attributes = getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
         * Converts the style to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
         * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            return "NamedStyle:" + getName() + " " + attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
         * Fetches the name of the style.   A style is not required to be named,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * so null is returned if there is no name associated with the style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            if (isDefined(StyleConstants.NameAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                return getAttribute(StyleConstants.NameAttribute).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
         * Changes the name of the style.  Does nothing with a null name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
         * @param name the new name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        public void setName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                this.addAttribute(StyleConstants.NameAttribute, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         * Adds a change listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
         * @param l the change listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         * Removes a change listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
         * @param l the change listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         * to this NamedStyle with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        public ChangeListener[] getChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            return (ChangeListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    ChangeListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
         * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
         * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
         * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
         * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                if (listeners[i]==ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    if (changeEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
         * Return an array of all the listeners of the given type that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
         * were added to this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         * @return all of the objects receiving <em>listenerType</em> notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
         *          from this model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        // --- AttributeSet ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        // delegated to the immutable field "attributes"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
         * Gets the number of attributes that are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
         * @return the number of attributes >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
         * @see AttributeSet#getAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            return attributes.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         * @param attrName the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        public boolean isDefined(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            return attributes.isDefined(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         * Checks whether two attribute sets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         * @param attr the attribute set to check against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
         * @return true if the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         * @see AttributeSet#isEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            return attributes.isEqual(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         * Copies a set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
         * @see AttributeSet#copyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            NamedStyle a = new NamedStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            a.attributes = attributes.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
         * @param attrName the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        public Object getAttribute(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            return attributes.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
         * Gets the names of all attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
         * @return the attribute names as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
         * @see AttributeSet#getAttributeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        public Enumeration<?> getAttributeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            return attributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * Checks whether a given attribute name/value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
         * @return true if the name/value is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         * @see AttributeSet#containsAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            return attributes.containsAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
         * Checks whether the element contains all the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
         * @param attrs the attributes to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
         * @return true if the element contains all the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         * @see AttributeSet#containsAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        public boolean containsAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            return attributes.containsAttributes(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         * Gets attributes from the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         * If not overriden, the resolving parent defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         * the parent element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
         * @return the attributes from the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            return attributes.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        // --- MutableAttributeSet ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        // should fetch a new immutable record for the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        // "attributes".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
         * Adds an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        public void addAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            attributes = context.addAttribute(attributes, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
         * Adds a set of attributes to the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
         * @param attr the attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        public void addAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            attributes = context.addAttributes(attributes, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
         * Removes an attribute from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
         * @see MutableAttributeSet#removeAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        public void removeAttribute(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            attributes = context.removeAttribute(attributes, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
         * @param names the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        public void removeAttributes(Enumeration<?> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            attributes = context.removeAttributes(attributes, names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         * @param attrs the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        public void removeAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            if (attrs == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                attributes = context.getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                attributes = context.removeAttributes(attributes, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         * Sets the resolving parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         * @param parent the parent, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
         * @see MutableAttributeSet#setResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        public void setResolveParent(AttributeSet parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                addAttribute(StyleConstants.ResolveAttribute, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                removeAttribute(StyleConstants.ResolveAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            writeAttributeSet(s, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            attributes = SimpleAttributeSet.EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            readAttributeSet(s, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        // --- member variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
         * The change listeners for the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
         * Only one ChangeEvent is needed per model instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
         * of events generated here is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
         * Inner AttributeSet implementation, which may be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
         * immutable unique set being shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        private transient AttributeSet attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        // initialize the static key registry with the StyleConstants keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            int n = StyleConstants.keys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                StyleContext.registerStaticAttributeKey(StyleConstants.keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
}