jdk/src/share/classes/javax/swing/text/StyleContext.java
author yan
Tue, 24 Sep 2013 17:04:47 +0400
changeset 20158 1c5d22e5b898
parent 10316 8d1ca7d93fb2
child 20455 f6f9a0c2796b
permissions -rw-r--r--
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.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
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 1639
diff changeset
    38
import sun.font.FontUtilities;
2
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)
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10316
diff changeset
   244
     * @param size the point size &gt;= 1
2
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);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   249
        Font f = fontTable.get(fontSearch);
2
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
            }
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 1639
diff changeset
   266
            if (! FontUtilities.fontSupportsDefaultEncoding(f)) {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 1639
diff changeset
   267
                f = FontUtilities.getCompositeFontUIResource(f);
2
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
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   295
     * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   296
     * in Swing</A> for more information.
2
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
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   324
     * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   325
     * in Swing</A> for more information.
2
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
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   352
     * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   353
     * in Swing</A> for more information.
2
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
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   380
     * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   381
     * in Swing</A> for more information.
2
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
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   408
     * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   409
     * in Swing</A> for more information.
2
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
10316
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   448
     * <A HREF="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
8d1ca7d93fb2 7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents: 5506
diff changeset
   449
     * in Swing</A> for more information.
2
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);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   520
        WeakReference<SmallAttributeSet> reference = attributesPool.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        SmallAttributeSet a;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   522
        if (reference == null || (a = reference.get()) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            a = key;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   524
            attributesPool.put(a, new WeakReference<SmallAttributeSet>(a));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Creates a mutable attribute set to hand out because the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * needs are too big to try and use a shared version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    MutableAttributeSet getMutableAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (a instanceof MutableAttributeSet &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            a != SimpleAttributeSet.EMPTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return (MutableAttributeSet) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return createLargeAttributeSet(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Converts a StyleContext to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        removeUnusedSets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        String s = "";
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   549
        for (SmallAttributeSet set : attributesPool.keySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            s = s + set + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Context-specific handling of writing out attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void writeAttributes(ObjectOutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                  AttributeSet a) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        writeAttributeSet(out, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Context-specific handling of reading in attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public void readAttributes(ObjectInputStream in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                               MutableAttributeSet a) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        readAttributeSet(in, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Writes a set of attributes to the given object stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * for the purpose of serialization.  This will take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * special care to deal with static attribute keys that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * have been registered wit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <code>registerStaticAttributeKey</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Any attribute key not regsitered as a static key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * will be serialized directly.  All values are expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * to be serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param a the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public static void writeAttributeSet(ObjectOutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                                         AttributeSet a) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        int n = a.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        out.writeInt(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        Enumeration keys = a.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (key instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                out.writeObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                Object ioFmt = freezeKeyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                if (ioFmt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    throw new NotSerializableException(key.getClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                 getName() + " is not serializable as a key in an AttributeSet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                out.writeObject(ioFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            Object value = a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            Object ioFmt = freezeKeyMap.get(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (value instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                out.writeObject((ioFmt != null) ? ioFmt : value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                if (ioFmt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    throw new NotSerializableException(value.getClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                 getName() + " is not serializable as a value in an AttributeSet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                out.writeObject(ioFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Reads a set of attributes from the given object input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * stream that have been previously written out with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <code>writeAttributeSet</code>.  This will try to restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * keys that were static objects to the static objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * the current virtual machine considering only those keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * that have been registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <code>registerStaticAttributeKey</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * The attributes retrieved from the stream will be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * into the given mutable set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @param in the object stream to read the attribute data from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param a  the attribute set to place the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *   definitions in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @exception ClassNotFoundException passed upward if encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *  when reading the object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @exception IOException passed upward if encountered when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *  reading the object stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public static void readAttributeSet(ObjectInputStream in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        MutableAttributeSet a) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        int n = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            Object key = in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            Object value = in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (thawKeyMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                Object staticKey = thawKeyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                if (staticKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    key = staticKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                Object staticValue = thawKeyMap.get(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                if (staticValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    value = staticValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            a.addAttribute(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Registers an object as a static object that is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * used as a key in attribute sets.  This allows the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * to be treated specially for serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * For operation under a 1.1 virtual machine, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * uses the value returned by <code>toString</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * concatenated to the classname.  The value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * by toString should not have the class reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * in it (ie it should be reimplemented from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * definition in Object) in order to be the same when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * recomputed later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param key the non-null object key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public static void registerStaticAttributeKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        String ioFmt = key.getClass().getName() + "." + key.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (freezeKeyMap == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   676
            freezeKeyMap = new Hashtable<Object, String>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   677
            thawKeyMap = new Hashtable<String, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        freezeKeyMap.put(key, ioFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        thawKeyMap.put(ioFmt, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns the object previously registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <code>registerStaticAttributeKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public static Object getStaticAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (thawKeyMap == null || key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return thawKeyMap.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Returns the String that <code>key</code> will be registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @see #getStaticAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @see #registerStaticAttributeKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public static Object getStaticAttributeKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return key.getClass().getName() + "." + key.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // clean out unused sets before saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        removeUnusedSets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
      throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        fontSearch = new FontKey(null, 0, 0);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   716
        fontTable = new Hashtable<FontKey, Font>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        search = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        attributesPool = Collections.
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   719
                synchronizedMap(new WeakHashMap<SmallAttributeSet, WeakReference<SmallAttributeSet>>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    // --- variables ---------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * The name given to the default logical style attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * to paragraphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public static final String DEFAULT_STYLE = "default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   731
    private static Hashtable<Object, String> freezeKeyMap;
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   732
    private static Hashtable<String, Object> thawKeyMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    private Style styles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private transient FontKey fontSearch = new FontKey(null, 0, 0);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   736
    private transient Hashtable<FontKey, Font> fontTable = new Hashtable<FontKey, Font>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   738
    private transient Map<SmallAttributeSet, WeakReference<SmallAttributeSet>> attributesPool = Collections.
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   739
            synchronizedMap(new WeakHashMap<SmallAttributeSet, WeakReference<SmallAttributeSet>>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private transient MutableAttributeSet search = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Number of immutable sets that are not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * being used.  This helps indicate when the sets need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * to be cleaned out of the hashtable they are stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    private int unusedSets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * The threshold for no longer sharing the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * in an immutable table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    static final int THRESHOLD = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * This class holds a small number of attributes in an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * The storage format is key, value, key, value, etc.  The size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * of the set is the length of the array divided by two.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * default, this is the class that will be used to store attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * when held in the compact sharable form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public class SmallAttributeSet implements AttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        public SmallAttributeSet(Object[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            this.attributes = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            updateResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        public SmallAttributeSet(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            int n = attrs.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            Object[] tbl = new Object[2 * n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            Enumeration names = attrs.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                tbl[i] = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                tbl[i+1] = attrs.getAttribute(tbl[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            attributes = tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            updateResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        private void updateResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            resolveParent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            for (int i = 0; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                if (tbl[i] == StyleConstants.ResolveAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    resolveParent = (AttributeSet)tbl[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        Object getLocalAttribute(Object nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (nm == StyleConstants.ResolveAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                return resolveParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            for (int i = 0; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                if (nm.equals(tbl[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    return tbl[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        // --- Object methods -------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * Returns a string showing the key/value pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            String s = "{";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            for (int i = 0; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                if (tbl[i+1] instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    // don't recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    s = s + tbl[i] + "=" + "AttributeSet" + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    s = s + tbl[i] + "=" + tbl[i+1] + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            s = s + "}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         * Returns a hashcode for this set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * @return     a hashcode value for this set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            int code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            Object[] tbl = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (int i = 1; i < tbl.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                code ^= tbl[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
         * Compares this object to the specifed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
         * The result is <code>true</code> if the object is an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
         * set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * @param     obj   the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (obj instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                AttributeSet attrs = (AttributeSet) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                return ((getAttributeCount() == attrs.getAttributeCount()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        containsAttributes(attrs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * Clones a set of attributes.  Since the set is immutable, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         * clone is basically the same set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * @return the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        //  --- AttributeSet methods ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * Gets the number of attributes that are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         * @return the number of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         * @see AttributeSet#getAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            return attributes.length / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         * @param key the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        public boolean isDefined(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            Object[] a = attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            int n = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            for (int i = 0; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                if (key.equals(a[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
         * Checks whether two attribute sets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         * @param attr the attribute set to check against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * @return true if the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         * @see AttributeSet#isEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (attr instanceof SmallAttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                return attr == this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            return ((getAttributeCount() == attr.getAttributeCount()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    containsAttributes(attr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         * Copies a set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         * @see AttributeSet#copyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * @param key the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        public Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            Object value = getLocalAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                AttributeSet parent = getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                if (parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    value = parent.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         * Gets the names of all attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         * @return the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * @see AttributeSet#getAttributeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public Enumeration<?> getAttributeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            return new KeyEnumeration(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * Checks whether a given attribute name/value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
         * @param name the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         * @return true if the name/value is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * @see AttributeSet#containsAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return value.equals(getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * Checks whether the attribute set contains all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         * the given attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * @param attrs the attributes to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * @return true if the element contains all the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * @see AttributeSet#containsAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        public boolean containsAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            boolean result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            Enumeration names = attrs.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            while (result && names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                result = attrs.getAttribute(name).equals(getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         * If not overriden, the resolving parent defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         * the parent element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * @return the attributes from the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            return resolveParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        // --- variables -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        Object[] attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // This is also stored in attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        AttributeSet resolveParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * An enumeration of the keys in a SmallAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    class KeyEnumeration implements Enumeration<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        KeyEnumeration(Object[] attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            this.attr = attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
         * Tests if this enumeration contains more elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         * @return  <code>true</code> if this enumeration contains more elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return i < attr.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         * Returns the next element of this enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         * @return     the next element of this enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         * @exception  NoSuchElementException  if no more elements exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if (i < attr.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                Object o = attr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        Object[] attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Sorts the key strings so that they can be very quickly compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * in the attribute set searchs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    class KeyBuilder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        public void initialize(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            if (a instanceof SmallAttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                initialize(((SmallAttributeSet)a).attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                keys.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                data.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                Enumeration names = a.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    addAttribute(name, a.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
         * Initialize with a set of already sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
         * keys (data from an existing SmallAttributeSet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        private void initialize(Object[] sorted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            keys.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            data.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            int n = sorted.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            for (int i = 0; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                keys.addElement(sorted[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                data.addElement(sorted[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         * Creates a table of sorted key/value entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         * suitable for creation of an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * SmallAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        public Object[] createTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            int n = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            Object[] tbl = new Object[2 * n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            for (int i = 0; i < n; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                int offs = 2 * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                tbl[offs] = keys.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                tbl[offs + 1] = data.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            return tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
         * The number of key/value pairs contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         * in the current key being forged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        int getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            return keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
         * Adds a key/value to the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        public void addAttribute(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            keys.addElement(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            data.addElement(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
         * Adds a set of key/value pairs to the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        public void addAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            if (attr instanceof SmallAttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                // avoid searching the keys, they are already interned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                Object[] tbl = ((SmallAttributeSet)attr).attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                int n = tbl.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                for (int i = 0; i < n; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    addAttribute(tbl[i], tbl[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    addAttribute(name, attr.getAttribute(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         * Removes the given name from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        public void removeAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            int n = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                if (keys.elementAt(i).equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    keys.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                    data.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
         * Removes the set of keys from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        public void removeAttributes(Enumeration names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                removeAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
         * Removes the set of matching attributes from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        public void removeAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            Enumeration names = attr.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                Object value = attr.getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                removeSearchAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        private void removeSearchAttribute(Object ikey, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            int n = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                if (keys.elementAt(i).equals(ikey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    if (data.elementAt(i).equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                        keys.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                        data.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1176
        private Vector<Object> keys = new Vector<Object>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1177
        private Vector<Object> data = new Vector<Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * key for a font table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    static class FontKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        private String family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        private int style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         * Constructs a font key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        public FontKey(String family, int style, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            setValue(family, style, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        public void setValue(String family, int style, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            this.family = (family != null) ? family.intern() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            this.style = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         * Returns a hashcode for this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * @return     a hashcode value for this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            int fhash = (family != null) ? family.hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            return fhash ^ style ^ size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
         * Compares this object to the specifed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
         * The result is <code>true</code> if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
         * <code>null</code> and is a <code>Font</code> object with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
         * name, style, and point size as this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         * @param     obj   the object to compare this font with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
         * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
         *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            if (obj instanceof FontKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                FontKey font = (FontKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                return (size == font.size) && (style == font.style) && (family == font.family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * A collection of attributes, typically used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * character and paragraph styles.  This is an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * of MutableAttributeSet that can be observed if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * These styles will take advantage of immutability while
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * the sets are small enough, and may be substantially more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * efficient than something like SimpleAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    public class NamedStyle implements Style, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
         * Creates a new named style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
         * @param name the style name, null for unnamed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * @param parent the parent style, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        public NamedStyle(String name, Style parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            attributes = getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                setName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                setResolveParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         * Creates a new named style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         * @param parent the parent style, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        public NamedStyle(Style parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            this(null, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
         * Creates a new named style, with a null name and parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        public NamedStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            attributes = getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
         * Converts the style to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
         * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            return "NamedStyle:" + getName() + " " + attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
         * Fetches the name of the style.   A style is not required to be named,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
         * so null is returned if there is no name associated with the style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
         * @return the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            if (isDefined(StyleConstants.NameAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                return getAttribute(StyleConstants.NameAttribute).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
         * Changes the name of the style.  Does nothing with a null name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
         * @param name the new name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        public void setName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                this.addAttribute(StyleConstants.NameAttribute, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
         * Adds a change listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
         * @param l the change listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         * Removes a change listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * @param l the change listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
         * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
         * to this NamedStyle with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        public ChangeListener[] getChangeListeners() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1344
            return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
         * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
         * the fire method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
         * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                if (listeners[i]==ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    if (changeEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                        changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
         * Return an array of all the listeners of the given type that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
         * were added to this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
         * @return all of the objects receiving <em>listenerType</em> notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
         *          from this model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        // --- AttributeSet ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        // delegated to the immutable field "attributes"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
         * Gets the number of attributes that are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 10316
diff changeset
  1390
         * @return the number of attributes &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
         * @see AttributeSet#getAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        public int getAttributeCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            return attributes.getAttributeCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
         * @param attrName the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        public boolean isDefined(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            return attributes.isDefined(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
         * Checks whether two attribute sets are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         * @param attr the attribute set to check against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * @return true if the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         * @see AttributeSet#isEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        public boolean isEqual(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            return attributes.isEqual(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
         * Copies a set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         * @see AttributeSet#copyAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        public AttributeSet copyAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            NamedStyle a = new NamedStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            a.attributes = attributes.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
         * @param attrName the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        public Object getAttribute(Object attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            return attributes.getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         * Gets the names of all attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         * @return the attribute names as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         * @see AttributeSet#getAttributeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        public Enumeration<?> getAttributeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            return attributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
         * Checks whether a given attribute name/value is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * @return true if the name/value is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         * @see AttributeSet#containsAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        public boolean containsAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            return attributes.containsAttribute(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         * Checks whether the element contains all the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         * @param attrs the attributes to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
         * @return true if the element contains all the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
         * @see AttributeSet#containsAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        public boolean containsAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            return attributes.containsAttributes(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
         * Gets attributes from the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * If not overriden, the resolving parent defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         * the parent element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         * @return the attributes from the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            return attributes.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        // --- MutableAttributeSet ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        // should fetch a new immutable record for the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        // "attributes".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         * Adds an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
         * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        public void addAttribute(Object name, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            attributes = context.addAttribute(attributes, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
         * Adds a set of attributes to the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
         * @param attr the attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
         * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        public void addAttributes(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            attributes = context.addAttributes(attributes, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
         * Removes an attribute from the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
         * @param name the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
         * @see MutableAttributeSet#removeAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        public void removeAttribute(Object name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            attributes = context.removeAttribute(attributes, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
         * @param names the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        public void removeAttributes(Enumeration<?> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            attributes = context.removeAttributes(attributes, names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
         * Removes a set of attributes for the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
         * @param attrs the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
         * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        public void removeAttributes(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            StyleContext context = StyleContext.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            if (attrs == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                attributes = context.getEmptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                attributes = context.removeAttributes(attributes, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * Sets the resolving parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         * @param parent the parent, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
         * @see MutableAttributeSet#setResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        public void setResolveParent(AttributeSet parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                addAttribute(StyleConstants.ResolveAttribute, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                removeAttribute(StyleConstants.ResolveAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            writeAttributeSet(s, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            attributes = SimpleAttributeSet.EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            readAttributeSet(s, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        // --- member variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
         * The change listeners for the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         * Only one ChangeEvent is needed per model instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
         * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
         * of events generated here is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
         * Inner AttributeSet implementation, which may be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
         * immutable unique set being shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        private transient AttributeSet attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        // initialize the static key registry with the StyleConstants keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            int n = StyleConstants.keys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                StyleContext.registerStaticAttributeKey(StyleConstants.keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
}