jdk/src/java.desktop/share/classes/javax/swing/text/html/StyleSheet.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 28231 b608ffcaed74
child 47160 ac5434728c3b
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 22260
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2493
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: 2493
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: 2493
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2493
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2493
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.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.Icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.ImageIcon;
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
    34
import javax.swing.UIManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.event.ChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Support for defining the visual characteristics of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * HTML views being rendered.  The StyleSheet is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * translate the HTML model into visual characteristics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This enables views to be customized by a look-and-feel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * multiple views over the same model can be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * differently, etc.  This can be thought of as a CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * rule repository.  The key for CSS attributes is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * object of type CSS.Attribute.  The type of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * is up to the StyleSheet implementation, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>toString</code> method is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * to return a string representation of CSS value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The primary entry point for HTML View implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * to get their attributes is the
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 7668
diff changeset
    54
 * {@link #getViewAttributes getViewAttributes}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * method.  This should be implemented to establish the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * desired policy used to associate attributes with the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Each HTMLEditorKit (i.e. and therefore each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * JEditorPane) can have its own StyleSheet, but by default one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * sheet will be shared by all of the HTMLEditorKit instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * HTMLDocument instance can also have a StyleSheet, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * holds the document-specific CSS specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * In order for Views to store less state and therefore be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * more lightweight, the StyleSheet can act as a factory for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * painters that handle some of the rendering tasks.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * implementations to determine what they want to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * and have the sharing potentially at the level that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * selector is common to multiple views.  Since the StyleSheet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * may be used by views over multiple documents and typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the HTML attributes don't effect the selector being used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the potential for sharing is significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The rules are stored as named styles, and other information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * is stored to translate the context of an element to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * rule quickly.  The following code fragment will display
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * the named styles, and therefore the CSS rules contained.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 9035
diff changeset
    77
 * <pre><code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * &nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * &nbsp; import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * &nbsp; import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * &nbsp; import javax.swing.text.html.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * &nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * &nbsp; public class ShowStyles {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * &nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * &nbsp;     public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * &nbsp;       HTMLEditorKit kit = new HTMLEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * &nbsp;       HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * &nbsp;       StyleSheet styles = doc.getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * &nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * &nbsp;       Enumeration rules = styles.getStyleNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * &nbsp;       while (rules.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * &nbsp;           String name = (String) rules.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * &nbsp;           Style rule = styles.getStyle(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * &nbsp;           System.out.println(rule.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * &nbsp;       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * &nbsp;       System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * &nbsp;     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * &nbsp; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * &nbsp;
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 9035
diff changeset
   100
 * </code></pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * The semantics for when a CSS style should overide visual attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * defined by an element are not well defined. For example, the html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <code>&lt;body bgcolor=red&gt;</code> makes the body have a red
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * background. But if the html file also contains the CSS rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <code>body { background: blue }</code> it becomes less clear as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * what color the background of the body should be. The current
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   108
 * implementation gives visual attributes defined in the element the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * highest precedence, that is they are always checked before any styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Therefore, in the previous example the background would have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * red color as the body element defines the background color to be red.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * As already mentioned this supports CSS. We don't support the full CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * spec. Refer to the javadoc of the CSS class to see what properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * we support. The two major CSS parsing related
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * concepts we do not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * support are pseudo selectors, such as <code>A:link { color: red }</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * and the <code>important</code> modifier.
23715
54ae9dd9df73 8039074: Tidy warnings cleanup for javax.swing
yan
parents: 23697
diff changeset
   119
 *
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21278
diff changeset
   120
 * @implNote This implementation is currently
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * incomplete.  It can be replaced with alternative implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * that are complete.  Future versions of this class will provide
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21278
diff changeset
   123
 * better CSS support.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @author  Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @author  Sara Swanson
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @author  Jill Nakata
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
   130
@SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
public class StyleSheet extends StyleContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // As the javadoc states, this class maintains a mapping between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // a CSS selector (such as p.bar) and a Style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // This consists of a number of parts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // . Each selector is broken down into its constituent simple selectors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    //   and stored in an inverted graph, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    //     p { color: red } ol p { font-size: 10pt } ul p { font-size: 12pt }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    //   results in the graph:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    //          root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    //           |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    //           p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    //          / \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    //         ol ul
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    //   each node (an instance of SelectorMapping) has an associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    //   specificity and potentially a Style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // . Every rule that is asked for (either by way of getRule(String) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    //   getRule(HTML.Tag, Element)) results in a unique instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    //   ResolvedStyle. ResolvedStyles contain the AttributeSets from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    //   SelectorMapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // . When a new rule is created it is inserted into the graph, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    //   the AttributeSets of each ResolvedStyles are updated appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // . This class creates special AttributeSets, LargeConversionSet and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    //   SmallConversionSet, that maintain a mapping between StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    //   and CSS so that developers that wish to use the StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    //   methods can do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // . When one of the AttributeSets is mutated by way of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    //   StyleConstants key, all the associated CSS keys are removed. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    //   done so that the two representations don't get out of sync. For
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   159
    //   example, if the developer adds StyleConstants.BOLD, FALSE to an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    //   AttributeSet that contains HTML.Tag.B, the HTML.Tag.B entry will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    //   be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Construct a StyleSheet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public StyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        selectorMapping = new SelectorMapping(0);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   169
        resolvedStyles = new Hashtable<String, ResolvedStyle>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (css == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            css = new CSS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
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
     * Fetches the style to use to render the given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * of HTML tag.  The element given is representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * the tag and can be used to determine the nesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * for situations where the attributes will differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * if nesting inside of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param t the type to translate to visual attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param e the element representing the tag; the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *  can be used to determine the nesting for situations where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *  the attributes will differ if nested inside of other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *  elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return the set of CSS attributes to use to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *  the tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Style getRule(HTML.Tag t, Element e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // Build an array of all the parent elements.
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
   195
            @SuppressWarnings("unchecked")
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   196
            Vector<Element> searchContext = sb.getVector();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            for (Element p = e; p != null; p = p.getParentElement()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                searchContext.addElement(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // Build a fully qualified selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            int              n = searchContext.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            StringBuffer     cacheLookup = sb.getStringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            AttributeSet     attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            String           eName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            Object           name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // >= 1 as the HTML.Tag for the 0th element is passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            for (int counter = n - 1; counter >= 1; counter--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   211
                e = searchContext.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                attr = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                name = attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                eName = name.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                cacheLookup.append(eName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (attr.isDefined(HTML.Attribute.ID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        cacheLookup.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        cacheLookup.append(attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                           (HTML.Attribute.ID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    else if (attr.isDefined(HTML.Attribute.CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        cacheLookup.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        cacheLookup.append(attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                           (HTML.Attribute.CLASS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                cacheLookup.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            cacheLookup.append(t.toString());
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   231
            e = searchContext.elementAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            attr = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                // For leafs, we use the second tier attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                Object testAttr = attr.getAttribute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (testAttr instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    attr = (AttributeSet)testAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    attr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (attr.isDefined(HTML.Attribute.ID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    cacheLookup.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    cacheLookup.append(attr.getAttribute(HTML.Attribute.ID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                else if (attr.isDefined(HTML.Attribute.CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    cacheLookup.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    cacheLookup.append(attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                       (HTML.Attribute.CLASS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            Style style = getResolvedStyle(cacheLookup.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                           searchContext, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            SearchBuffer.releaseSearchBuffer(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Fetches the rule that best matches the selector given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * in string form. Where <code>selector</code> is a space separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * String of the element names. For example, <code>selector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * might be 'html body tr td''<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * The attributes of the returned Style will change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * as rules are added and removed. That is if you to ask for a rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * with a selector "table p" and a new rule was added with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * of "p" the returned Style would include the new attributes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the rule "p".
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   274
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   275
     * @param selector a space separated String of the element names.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   276
     * @return the rule that best matches the selector.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public Style getRule(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        selector = cleanSelectorString(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (selector != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            Style style = getResolvedStyle(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Adds a set of rules to the sheet.  The rules are expected to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * be in valid CSS format.  Typically this would be called as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * a result of parsing a &lt;style&gt; tag.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   291
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   292
     * @param rule a set of rules
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public void addRule(String rule) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (rule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            //tweaks to control display properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            //see BasicEditorPaneUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            final String baseUnitsDisable = "BASE_SIZE_DISABLE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            final String baseUnits = "BASE_SIZE ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            final String w3cLengthUnitsEnable = "W3C_LENGTH_UNITS_ENABLE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            final String w3cLengthUnitsDisable = "W3C_LENGTH_UNITS_DISABLE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (rule == baseUnitsDisable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                sizeMap = sizeMapDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            } else if (rule.startsWith(baseUnits)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                rebaseSizeMap(Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                              parseInt(rule.substring(baseUnits.length())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } else if (rule == w3cLengthUnitsEnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                w3cLengthUnits = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else if (rule == w3cLengthUnitsDisable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                w3cLengthUnits = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                CssParser parser = new CssParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    parser.parse(getBase(), new StringReader(rule), false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Translates a CSS declaration to an AttributeSet that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * the CSS declaration.  Typically this would be called as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * result of encountering an HTML style attribute.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   324
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   325
     * @param decl a CSS declaration
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   326
     * @return a set of attributes that represents the CSS declaration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public AttributeSet getDeclaration(String decl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (decl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return SimpleAttributeSet.EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        CssParser parser = new CssParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return parser.parseDeclaration(decl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Loads a set of rules that have been specified in terms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * CSS1 grammar.  If there are collisions with existing rules,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * the newly specified rule will win.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param in the stream to read the CSS grammar from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param ref the reference URL.  This value represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *  location of the stream and may be null.  All relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *  URLs specified in the stream will be based upon this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *  parameter.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   346
     * @throws java.io.IOException if I/O error occured.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public void loadRules(Reader in, URL ref) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        CssParser parser = new CssParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        parser.parse(ref, in, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Fetches a set of attributes to use in the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * displaying.  This is basically a set of attributes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * can be used for View.getAttributes.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   357
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   358
     * @param v a view
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   359
     * @return the of attributes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public AttributeSet getViewAttributes(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return new ViewAttributeSet(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Removes a named style previously added to the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param nm  the name of the style to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public void removeStyle(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Style       aStyle = getStyle(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (aStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            String selector = cleanSelectorString(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            String[] selectors = getSimpleSelectors(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                SelectorMapping mapping = getRootSelectorMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                for (int i = selectors.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    mapping = mapping.getChildSelectorMapping(selectors[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                                              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                Style rule = mapping.getStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                if (rule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    mapping.setStyle(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    if (resolvedStyles.size() > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   386
                        Enumeration<ResolvedStyle> values = resolvedStyles.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        while (values.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   388
                            ResolvedStyle style = values.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            style.removeStyle(rule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        super.removeStyle(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Adds the rules from the StyleSheet <code>ss</code> to those of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * the receiver. <code>ss's</code> rules will override the rules of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * any previously added style sheets. An added StyleSheet will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * override the rules of the receiving style sheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   404
     * @param ss a StyleSheet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public void addStyleSheet(StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (linkedStyleSheets == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   410
                linkedStyleSheets = new Vector<StyleSheet>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (!linkedStyleSheets.contains(ss)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (ss instanceof javax.swing.plaf.UIResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    && linkedStyleSheets.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    index = linkedStyleSheets.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                linkedStyleSheets.insertElementAt(ss, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                linkStyleSheetAt(ss, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Removes the StyleSheet <code>ss</code> from those of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   427
     * @param ss a StyleSheet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void removeStyleSheet(StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (linkedStyleSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                int index = linkedStyleSheets.indexOf(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    linkedStyleSheets.removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    unlinkStyleSheet(ss, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    if (index == 0 && linkedStyleSheets.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        linkedStyleSheets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    // The following is used to import style sheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Returns an array of the linked StyleSheets. Will return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * if there are no linked StyleSheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   453
     * @return an array of StyleSheets.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public StyleSheet[] getStyleSheets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        StyleSheet[] retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (linkedStyleSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                retValue = new StyleSheet[linkedStyleSheets.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                linkedStyleSheets.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                retValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Imports a style sheet from <code>url</code>. The resulting rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * are directly added to the receiver. If you do not want the rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * to become part of the receiver, create a new StyleSheet and use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * addStyleSheet to link it in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   477
     * @param url an url
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public void importStyleSheet(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            is = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            Reader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            CssParser parser = new CssParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            parser.parse(url, r, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // on error we simply have no styles... the html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            // will look mighty wrong but still function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Sets the base. All import statements that are relative, will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * relative to <code>base</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   500
     * @param base a base.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public void setBase(URL base) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        this.base = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Returns the base.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   510
     * @return the base.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public URL getBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Adds a CSS attribute to the given set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   520
     * @param attr a set of attributes
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   521
     * @param key a CSS property
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   522
     * @param value an HTML attribute value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        css.addInternalCSSValue(attr, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Adds a CSS attribute to the given set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   533
     * @param attr a set of attributes
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   534
     * @param key a CSS property
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   535
     * @param value an HTML attribute value
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   536
     * @return {@code true} if an HTML attribute {@code value} can be converted
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   537
     *         to a CSS attribute, false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public boolean addCSSAttributeFromHTML(MutableAttributeSet attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                           CSS.Attribute key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        Object iValue = css.getCssValue(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (iValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            attr.addAttribute(key, iValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    // ---- Conversion functionality ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Converts a set of HTML attributes to an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * set of CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @param htmlAttrSet AttributeSet containing the HTML attributes.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   557
     * @return the set of CSS attributes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        AttributeSet cssAttrSet = css.translateHTMLToCSS(htmlAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        MutableAttributeSet cssStyleSet = addStyle(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        cssStyleSet.addAttributes(cssAttrSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return cssStyleSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Adds an attribute to the given set, and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * the new representative set.  This is reimplemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * convert StyleConstant attributes to CSS prior to forwarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * to the superclass behavior.  The StyleConstants attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * has no corresponding CSS entry, the StyleConstants attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * is stored (but will likely be unused).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param key the non-null attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param value the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public AttributeSet addAttribute(AttributeSet old, Object key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                     Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (css == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            // supers constructor will call this before returning,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            // and we need to make sure CSS is non null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            css = new CSS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            HTML.Tag tag = HTML.getTagForStyleConstantsKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                (StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (tag != null && old.isDefined(tag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                old = removeAttribute(old, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            Object cssValue = css.styleConstantsValueToCSSValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                              ((StyleConstants)key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (cssValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                    ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    return super.addAttribute(old, cssKey, cssValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return super.addAttribute(old, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Adds a set of attributes to the element.  If any of these attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * are StyleConstants attributes, they will be converted to CSS prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * to forwarding to the superclass behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @param attr the attributes to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @see MutableAttributeSet#addAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public AttributeSet addAttributes(AttributeSet old, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (!(attr instanceof HTMLDocument.TaggedAttributeSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            old = removeHTMLTags(old, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        return super.addAttributes(old, convertAttributeSet(attr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Removes an attribute from the set.  If the attribute is a StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * attribute, the request will be converted to a CSS attribute prior to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * forwarding to the superclass behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param old the old set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param key the non-null attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @see MutableAttributeSet#removeAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public AttributeSet removeAttribute(AttributeSet old, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            HTML.Tag tag = HTML.getTagForStyleConstantsKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                   (StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (tag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                old = super.removeAttribute(old, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            Object cssKey = css.styleConstantsKeyToCSSKey((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                return super.removeAttribute(old, cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return super.removeAttribute(old, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Removes a set of attributes for the element.  If any of the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * is a StyleConstants attribute, the request will be converted to a CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * attribute prior to forwarding to the superclass behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @param names the attribute names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // PENDING: Should really be doing something similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // removeHTMLTags here, but it is rather expensive to have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        // clone names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return super.removeAttributes(old, names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Removes a set of attributes. If any of the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * is a StyleConstants attribute, the request will be converted to a CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * attribute prior to forwarding to the superclass behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @param old the old attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param attrs the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @return the updated attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see MutableAttributeSet#removeAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (old != attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            old = removeHTMLTags(old, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return super.removeAttributes(old, convertAttributeSet(attrs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Creates a compact set of attributes that might be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * This is a hook for subclasses that want to alter the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * behavior of SmallAttributeSet.  This can be reimplemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * to return an AttributeSet that provides some sort of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * attribute conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param a The set of attributes to be represented in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *  the compact form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return new SmallConversionSet(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Creates a large set of attributes that should trade off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * space for time.  This set will not be shared.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * a hook for subclasses that want to alter the behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * of the larger attribute storage format (which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * SimpleAttributeSet by default).   This can be reimplemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * to return a MutableAttributeSet that provides some sort of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * attribute conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @param a The set of attributes to be represented in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *  the larger form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    protected MutableAttributeSet createLargeAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        return new LargeConversionSet(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * For any StyleConstants key in attr that has an associated HTML.Tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * it is removed from old. The resulting AttributeSet is then returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    private AttributeSet removeHTMLTags(AttributeSet old, AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (!(attr instanceof LargeConversionSet) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            !(attr instanceof SmallConversionSet)) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
   724
            Enumeration<?> names = attr.getAttributeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                Object key = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    HTML.Tag tag = HTML.getTagForStyleConstantsKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        (StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    if (tag != null && old.isDefined(tag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                        old = super.removeAttribute(old, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Converts a set of attributes (if necessary) so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * any attributes that were specified as StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * attributes and have a CSS mapping, will be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * to CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    AttributeSet convertAttributeSet(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if ((a instanceof LargeConversionSet) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            (a instanceof SmallConversionSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            // known to be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // in most cases, there are no StyleConstants attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // so we iterate the collection of keys to avoid creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // a new set.
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
   757
        Enumeration<?> names = a.getAttributeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        while (names.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            Object name = names.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (name instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                // we really need to do a conversion, iterate again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                // building a new set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                MutableAttributeSet converted = new LargeConversionSet();
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
   764
                Enumeration<?> keys = a.getAttributeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    Object cssValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                        // convert the StyleConstants attribute if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                            ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                            Object value = a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                            cssValue = css.styleConstantsValueToCSSValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                           ((StyleConstants)key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                            if (cssValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                converted.addAttribute(cssKey, cssValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    if (cssValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                        converted.addAttribute(key, a.getAttribute(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                return converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Large set of attributes that does conversion of requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * for attributes of type StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
   795
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    class LargeConversionSet extends SimpleAttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * Creates a new attribute set based on a supplied set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * @param source the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        public LargeConversionSet(AttributeSet source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            super(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        public LargeConversionSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         * @param key the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        public boolean isDefined(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                    ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    return super.isDefined(cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return super.isDefined(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         * @param key the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        public Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                    ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    Object value = super.getAttribute(cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        return css.cssValueToStyleConstantsValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                           ((StyleConstants)key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return super.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Small set of attributes that does conversion of requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * for attributes of type StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    class SmallConversionSet extends SmallAttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * Creates a new attribute set based on a supplied set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         *
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   861
         * @param attrs the set of attributes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        public SmallConversionSet(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            super(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * @param key the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        public boolean isDefined(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                                    ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    return super.isDefined(cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            return super.isDefined(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         * Gets the value of an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
         * @param key the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        public Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                                    ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    Object value = super.getAttribute(cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        return css.cssValueToStyleConstantsValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                           ((StyleConstants)key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            return super.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    // ---- Resource handling ----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * Fetches the font to use for the given set of attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public Font getFont(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        return css.getFont(this, a, 12, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Takes a set of attributes and turn it into a foreground color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * specification.  This might be used to specify things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * like brighter, more hue, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @param a the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public Color getForeground(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        Color c = css.getColor(a, CSS.Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            return Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Takes a set of attributes and turn it into a background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * specification.  This might be used to specify things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * like brighter, more hue, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @param a the set of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    public Color getBackground(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return css.getColor(a, CSS.Attribute.BACKGROUND_COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * Fetches the box formatter to use for the given set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * of CSS attributes.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   948
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   949
     * @param a a set of CSS attributes
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   950
     * @return the box formatter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    public BoxPainter getBoxPainter(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        return new BoxPainter(a, css, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * Fetches the list formatter to use for the given set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * of CSS attributes.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   959
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   960
     * @param a a set of CSS attributes
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   961
     * @return the list formatter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    public ListPainter getListPainter(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        return new ListPainter(a, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Sets the base font size, with valid values between 1 and 7.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   969
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   970
     * @param sz a font size.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    public void setBaseFontSize(int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        css.setBaseFontSize(sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Sets the base font size from the passed in String. The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * can either identify a specific font size, with legal values between
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   979
     * 1 and 7, or identify a relative font size such as +1 or -2.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   980
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   981
     * @param size a font size.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    public void setBaseFontSize(String size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        css.setBaseFontSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   987
    /**
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   988
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   989
     * Returns the index of HTML/CSS size model.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   990
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   991
     * @param pt a size of point
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   992
     * @return the index of HTML/CSS size model.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
   993
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public static int getIndexOfSize(float pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return CSS.getIndexOfSize(pt, sizeMapDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Returns the point size, given a size index.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1000
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1001
     * @param index a size index
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1002
     * @return the point size value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    public float getPointSize(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        return css.getPointSize(index, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *  Given a string such as "+2", "-2", or "2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *  returns a point size value.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1011
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1012
     * @param size a CSS string describing font size
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1013
     * @return the point size value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    public float getPointSize(String size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        return css.getPointSize(size, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * Converts a color string such as "RED" or "#NNNNNN" to a Color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Note: This will only convert the HTML3.2 color strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *       or a string of length 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *       otherwise, it will return null.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1024
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1025
     * @param string color string such as "RED" or "#NNNNNN"
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1026
     * @return the color
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    public Color stringToColor(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        return CSS.stringToColor(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * Returns the ImageIcon to draw in the background for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * <code>attr</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    ImageIcon getBackgroundImage(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        Object value = attr.getAttribute(CSS.Attribute.BACKGROUND_IMAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            return ((CSS.BackgroundImage)value).getImage(getBase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Adds a rule into the StyleSheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @param selector the selector to use for the rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *  This will be a set of simple selectors, and must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *  be a length of 1 or greater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @param declaration the set of CSS attributes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *  make up the rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    void addRule(String[] selector, AttributeSet declaration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                 boolean isLinked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        int n = selector.length;
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
  1057
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        sb.append(selector[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        for (int counter = 1; counter < n; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            sb.append(selector[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        String selectorName = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        Style rule = getStyle(selectorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (rule == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            // Notice how the rule is first created, and it not part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            // the synchronized block. It is done like this as creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            // a new rule will fire a ChangeEvent. We do not want to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // holding the lock when calling to other objects, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            // result in deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            Style altRule = addStyle(selectorName, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                SelectorMapping mapping = getRootSelectorMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                for (int i = n - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    mapping = mapping.getChildSelectorMapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                                      (selector[i], true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                rule = mapping.getStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                if (rule == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    rule = altRule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    mapping.setStyle(rule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    refreshResolvedRules(selectorName, selector, rule,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                                         mapping.getSpecificity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (isLinked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            rule = getLinkedStyle(rule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        rule.addAttributes(declaration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    //
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1094
    // The following gaggle of methods is used in maintaining the rules from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    // the sheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Updates the attributes of the rules to reference any related
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * rules in <code>ss</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    private synchronized void linkStyleSheetAt(StyleSheet ss, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if (resolvedStyles.size() > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1104
            Enumeration<ResolvedStyle> values = resolvedStyles.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            while (values.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1106
                ResolvedStyle rule = values.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                rule.insertExtendedStyleAt(ss.getRule(rule.getName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                                           index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * Removes references to the rules in <code>ss</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * <code>index</code> gives the index the StyleSheet was at, that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * how many StyleSheets had been added before it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    private synchronized void unlinkStyleSheet(StyleSheet ss, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        if (resolvedStyles.size() > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1120
            Enumeration<ResolvedStyle> values = resolvedStyles.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            while (values.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1122
                ResolvedStyle rule = values.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                rule.removeExtendedStyleAt(index);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * Returns the simple selectors that comprise selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /* protected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    String[] getSimpleSelectors(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        selector = cleanSelectorString(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1135
        @SuppressWarnings("unchecked")
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1136
        Vector<String> selectors = sb.getVector();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        int lastIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        int length = selector.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        while (lastIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            int newIndex = selector.indexOf(' ', lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            if (newIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                selectors.addElement(selector.substring(lastIndex, newIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                if (++newIndex == length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    lastIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    lastIndex = newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                selectors.addElement(selector.substring(lastIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                lastIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        String[] retValue = new String[selectors.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        selectors.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        SearchBuffer.releaseSearchBuffer(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Returns a string that only has one space between simple selectors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * which may be the passed in String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /*protected*/ String cleanSelectorString(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        boolean lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        for (int counter = 0, maxCounter = selector.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
             counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            switch(selector.charAt(counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                if (lastWasSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                    return _cleanSelectorString(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                return _cleanSelectorString(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                lastWasSpace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (lastWasSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            return _cleanSelectorString(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        // It was fine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        return selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * Returns a new String that contains only one space between non
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * white space characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    private String _cleanSelectorString(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        StringBuffer buff = sb.getStringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        boolean lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        int lastIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        char[] chars = selector.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        int numChars = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        String retValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            for (int counter = 0; counter < numChars; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                switch(chars[counter]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    if (!lastWasSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                        lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                        if (lastIndex < counter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                            buff.append(chars, lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                        1 + counter - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    lastIndex = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    if (!lastWasSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                        lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                        if (lastIndex < counter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                            buff.append(chars, lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                                        counter - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                            buff.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    lastIndex = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    lastWasSpace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            if (lastWasSpace && buff.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                // Remove last space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                buff.setLength(buff.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            else if (lastIndex < numChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                buff.append(chars, lastIndex, numChars - lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            retValue = buff.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            SearchBuffer.releaseSearchBuffer(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * Returns the root selector mapping that all selectors are relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * to. This is an inverted graph of the selectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    private SelectorMapping getRootSelectorMapping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        return selectorMapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * Returns the specificity of the passed in String. It assumes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * passed in string doesn't contain junk, that is each selector is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * separated by a space and each selector at most contains one . or one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * #. A simple selector has a weight of 1, an id selector has a weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * of 100, and a class selector has a weight of 10000.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    /*protected*/ static int getSpecificity(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        int specificity = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        boolean lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        for (int counter = 0, maxCounter = selector.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
             counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            switch(selector.charAt(counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            case '.':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                specificity += 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            case '#':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                specificity += 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                lastWasSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                if (lastWasSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    lastWasSpace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    specificity += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return specificity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * Returns the style that linked attributes should be added to. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * will create the style if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    private Style getLinkedStyle(Style localStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        // NOTE: This is not synchronized, and the caller of this does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        // not synchronize. There is the chance for one of the callers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        // overwrite the existing resolved parent, but it is quite rare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        // The reason this is left like this is because setResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        // will fire a ChangeEvent. It is really, REALLY bad for us to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // hold a lock when calling outside of us, it may cause a deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        Style retStyle = (Style)localStyle.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        if (retStyle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            retStyle = addStyle(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            localStyle.setResolveParent(retStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        return retStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * Returns the resolved style for <code>selector</code>. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * create the resolved style, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    private synchronized Style getResolvedStyle(String selector,
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1314
                                                Vector<Element> elements,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                                HTML.Tag t) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1316
        Style retStyle = resolvedStyles.get(selector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        if (retStyle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            retStyle = createResolvedStyle(selector, elements, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        return retStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * Returns the resolved style for <code>selector</code>. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * create the resolved style, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    private synchronized Style getResolvedStyle(String selector) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1328
        Style retStyle = resolvedStyles.get(selector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        if (retStyle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            retStyle = createResolvedStyle(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        return retStyle;
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
     * Adds <code>mapping</code> to <code>elements</code>. It is added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * such that <code>elements</code> will remain ordered by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * specificity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1340
    private void addSortedStyle(SelectorMapping mapping, Vector<SelectorMapping> elements) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        int       size = elements.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            int     specificity = mapping.getSpecificity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            for (int counter = 0; counter < size; counter++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1347
                if (specificity >= elements.elementAt(counter).getSpecificity()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    elements.insertElementAt(mapping, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        elements.addElement(mapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * Adds <code>parentMapping</code> to <code>styles</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * recursively calls this method if <code>parentMapping</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * any child mappings for any of the Elements in <code>elements</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    private synchronized void getStyles(SelectorMapping parentMapping,
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1362
                           Vector<SelectorMapping> styles,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                           String[] tags, String[] ids, String[] classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                           int index, int numElements,
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1365
                           Hashtable<SelectorMapping, SelectorMapping> alreadyChecked) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        // Avoid desending the same mapping twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        if (alreadyChecked.contains(parentMapping)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        alreadyChecked.put(parentMapping, parentMapping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        Style style = parentMapping.getStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        if (style != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            addSortedStyle(parentMapping, styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        for (int counter = index; counter < numElements; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            String tagString = tags[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            if (tagString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                SelectorMapping childMapping = parentMapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                getChildSelectorMapping(tagString, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    getStyles(childMapping, styles, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                              counter + 1, numElements, alreadyChecked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                if (classes[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    String className = classes[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    childMapping = parentMapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                                         tagString + "." + className, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                        getStyles(childMapping, styles, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                                  counter + 1, numElements, alreadyChecked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    childMapping = parentMapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                                         "." + className, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                        getStyles(childMapping, styles, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                                  counter + 1, numElements, alreadyChecked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                if (ids[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                    String idName = ids[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    childMapping = parentMapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                                         tagString + "#" + idName, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                        getStyles(childMapping, styles, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                                  counter + 1, numElements, alreadyChecked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    childMapping = parentMapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                   "#" + idName, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                    if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                        getStyles(childMapping, styles, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                                  counter + 1, numElements, alreadyChecked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * Creates and returns a Style containing all the rules that match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *  <code>selector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    private synchronized Style createResolvedStyle(String selector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                                      String[] tags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                                      String[] ids, String[] classes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1426
        @SuppressWarnings("unchecked")
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1427
        Vector<SelectorMapping> tempVector = sb.getVector();
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1428
        @SuppressWarnings("unchecked")
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1429
        Hashtable<SelectorMapping, SelectorMapping> tempHashtable = sb.getHashtable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        // Determine all the Styles that are appropriate, placing them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        // in tempVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            SelectorMapping mapping = getRootSelectorMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            int numElements = tags.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            String tagString = tags[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            SelectorMapping childMapping = mapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                                                   tagString, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                getStyles(childMapping, tempVector, tags, ids, classes, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                          numElements, tempHashtable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            if (classes[0] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                String className = classes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                childMapping = mapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                                       tagString + "." + className, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    getStyles(childMapping, tempVector, tags, ids, classes, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                              numElements, tempHashtable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                childMapping = mapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                                       "." + className, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    getStyles(childMapping, tempVector, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                              1, numElements, tempHashtable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            if (ids[0] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                String idName = ids[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                childMapping = mapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                                       tagString + "#" + idName, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    getStyles(childMapping, tempVector, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                              1, numElements, tempHashtable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                childMapping = mapping.getChildSelectorMapping(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                                       "#" + idName, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                if (childMapping != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                    getStyles(childMapping, tempVector, tags, ids, classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                              1, numElements, tempHashtable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            // Create a new Style that will delegate to all the matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            // Styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            int numLinkedSS = (linkedStyleSheets != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                              linkedStyleSheets.size() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            int numStyles = tempVector.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            AttributeSet[] attrs = new AttributeSet[numStyles + numLinkedSS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            for (int counter = 0; counter < numStyles; counter++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1479
                attrs[counter] = tempVector.elementAt(counter).getStyle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            // Get the AttributeSet from linked style sheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            for (int counter = 0; counter < numLinkedSS; counter++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1483
                AttributeSet attr = linkedStyleSheets.elementAt(counter).getRule(selector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                    attrs[counter + numStyles] = SimpleAttributeSet.EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    attrs[counter + numStyles] = attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            ResolvedStyle retStyle = new ResolvedStyle(selector, attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                                                       numStyles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            resolvedStyles.put(selector, retStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            return retStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            SearchBuffer.releaseSearchBuffer(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * Creates and returns a Style containing all the rules that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * matches <code>selector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * @param elements  a Vector of all the Elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *                  the style is being asked for. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *                  first Element is the deepest Element, with the last Element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     *                  representing the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * @param t         the Tag to use for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     *                  the first Element in <code>elements</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1512
    private Style createResolvedStyle(String selector, Vector<Element> elements,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                                      HTML.Tag t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        int numElements = elements.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        // Build three arrays, one for tags, one for class's, and one for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        // id's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        String tags[] = new String[numElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        String ids[] = new String[numElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        String classes[] = new String[numElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        for (int counter = 0; counter < numElements; counter++) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1521
            Element e = elements.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            AttributeSet attr = e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            if (counter == 0 && e.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                // For leafs, we use the second tier attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                Object testAttr = attr.getAttribute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                if (testAttr instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    attr = (AttributeSet)testAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                    attr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                HTML.Tag tag = (HTML.Tag)attr.getAttribute(StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                                                           NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                if (tag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    tags[counter] = tag.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    tags[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                if (attr.isDefined(HTML.Attribute.CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    classes[counter] = attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                      (HTML.Attribute.CLASS).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    classes[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                if (attr.isDefined(HTML.Attribute.ID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    ids[counter] = attr.getAttribute(HTML.Attribute.ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                                        toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    ids[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                tags[counter] = ids[counter] = classes[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        tags[0] = t.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        return createResolvedStyle(selector, tags, ids, classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * Creates and returns a Style containing all the rules that match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     *  <code>selector</code>. It is assumed that each simple selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * in <code>selector</code> is separated by a space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    private Style createResolvedStyle(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        // Will contain the tags, ids, and classes, in that order.
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1573
        @SuppressWarnings("unchecked")
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1574
        Vector<String> elements = sb.getVector();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            int dotIndex = 0;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1578
            int spaceIndex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            int poundIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            int lastIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            int length = selector.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            while (lastIndex < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                if (dotIndex == lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    dotIndex = selector.indexOf('.', lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                if (poundIndex == lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                    poundIndex = selector.indexOf('#', lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                spaceIndex = selector.indexOf(' ', lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                if (spaceIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                    spaceIndex = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                if (dotIndex != -1 && poundIndex != -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                    dotIndex < spaceIndex && poundIndex < spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                    if (poundIndex < dotIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                        // #.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                        if (lastIndex == poundIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                            elements.addElement("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                            elements.addElement(selector.substring(lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                                                                  poundIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                        if ((dotIndex + 1) < spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                            elements.addElement(selector.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                                                (dotIndex + 1, spaceIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                            elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                        if ((poundIndex + 1) == dotIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                            elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                            elements.addElement(selector.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                                                (poundIndex + 1, dotIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    else if(poundIndex < spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                        // .#
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        if (lastIndex == dotIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                            elements.addElement("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                            elements.addElement(selector.substring(lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                                                                  dotIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                        if ((dotIndex + 1) < poundIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                            elements.addElement(selector.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                                                (dotIndex + 1, poundIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                            elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                        if ((poundIndex + 1) == spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                            elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                            elements.addElement(selector.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                                                (poundIndex + 1, spaceIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    dotIndex = poundIndex = spaceIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                else if (dotIndex != -1 && dotIndex < spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                    // .
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    if (dotIndex == lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                        elements.addElement("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                        elements.addElement(selector.substring(lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                                                               dotIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                    if ((dotIndex + 1) == spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                        elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                        elements.addElement(selector.substring(dotIndex + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                                                               spaceIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                    elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                    dotIndex = spaceIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                else if (poundIndex != -1 && poundIndex < spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                    // #
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    if (poundIndex == lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                        elements.addElement("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                        elements.addElement(selector.substring(lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                                                               poundIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                    elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                    if ((poundIndex + 1) == spaceIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                        elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                        elements.addElement(selector.substring(poundIndex + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                                                               spaceIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    poundIndex = spaceIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                    // id
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                    elements.addElement(selector.substring(lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                                                           spaceIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                    elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    elements.addElement(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                lastIndex = spaceIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            // Create the tag, id, and class arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            int total = elements.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            int numTags = total / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            String[] tags = new String[numTags];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            String[] ids = new String[numTags];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            String[] classes = new String[numTags];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            for (int index = 0, eIndex = total - 3; index < numTags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                 index++, eIndex -= 3) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1700
                tags[index] = elements.elementAt(eIndex);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1701
                classes[index] = elements.elementAt(eIndex + 1);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1702
                ids[index] = elements.elementAt(eIndex + 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            return createResolvedStyle(selector, tags, ids, classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            SearchBuffer.releaseSearchBuffer(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * Should be invoked when a new rule is added that did not previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * exist. Goes through and refreshes the necessary resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    private synchronized void refreshResolvedRules(String selectorName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                                                   String[] selector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                                                   Style newStyle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                                                   int specificity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        if (resolvedStyles.size() > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1721
            Enumeration<ResolvedStyle> values = resolvedStyles.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            while (values.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1723
                ResolvedStyle style = values.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                if (style.matches(selectorName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                    style.insertStyle(newStyle, specificity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * A temporary class used to hold a Vector, a StringBuffer and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * Hashtable. This is used to avoid allocing a lot of garbage when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * searching for rules. Use the static method obtainSearchBuffer and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * releaseSearchBuffer to get a SearchBuffer, and release it when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  1739
    @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    private static class SearchBuffer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        /** A stack containing instances of SearchBuffer. Used in getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
         * rules. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1743
        static Stack<SearchBuffer> searchBuffers = new Stack<SearchBuffer>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        // A set of temporary variables that can be used in whatever way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        Vector vector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        StringBuffer stringBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        Hashtable hashtable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
         * Returns an instance of SearchBuffer. Be sure and issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
         * a releaseSearchBuffer when done with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        static SearchBuffer obtainSearchBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            SearchBuffer sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                if(!searchBuffers.empty()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1757
                   sb = searchBuffers.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                   sb = new SearchBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            } catch (EmptyStackException ese) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                sb = new SearchBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
         * Adds <code>sb</code> to the stack of SearchBuffers that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
         * be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        static void releaseSearchBuffer(SearchBuffer sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            sb.empty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            searchBuffers.push(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        StringBuffer getStringBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            if (stringBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                stringBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            return stringBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        Vector getVector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            if (vector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                vector = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            return vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        Hashtable getHashtable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            if (hashtable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                hashtable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            return hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        void empty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            if (stringBuffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                stringBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            if (vector != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                vector.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            if (hashtable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                hashtable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    static final Border noBorder = new EmptyBorder(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * Class to carry out some of the duties of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * CSS formatting.  Implementations of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * class enable views to present the CSS formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * while not knowing anything about how the CSS values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * are being cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * As a delegate of Views, this object is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * the insets of a View and making sure the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * is maintained according to the CSS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  1824
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    public static class BoxPainter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        BoxPainter(AttributeSet a, CSS css, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            this.css = css;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            border = getBorder(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            binsets = border.getBorderInsets(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            topMargin = getLength(CSS.Attribute.MARGIN_TOP, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            bg = ss.getBackground(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            if (ss.getBackgroundImage(a) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                bgPainter = new BackgroundImagePainter(a, css, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
         * Fetches a border to render for the given attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
         * PENDING(prinz) This is pretty badly hacked at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         * moment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        Border getBorder(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            return new CSSBorder(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
         * Fetches the color to use for borders.  This will either be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
         * the value specified by the border-color attribute (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
         * is not inherited), or it will default to the color attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
         * (which is inherited).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        Color getBorderColor(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            Color color = css.getColor(a, CSS.Attribute.BORDER_COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                color = css.getColor(a, CSS.Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    return Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
         * Fetches the inset needed on a given side to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
         * account for the margin, border, and padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * @param side The size of the box to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
         *  inset for.  This can be View.TOP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
         *  View.LEFT, View.BOTTOM, or View.RIGHT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
         * @param v the view making the request.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
         *  used to get the AttributeSet, and may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         *  resolve percentage arguments.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 24528
diff changeset
  1878
         * @return the inset needed for the margin, border and padding.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * @exception IllegalArgumentException for an invalid direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        public float getInset(int side, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            AttributeSet a = v.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            float inset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            switch(side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            case View.LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                inset += getOrientationMargin(HorizontalMargin.LEFT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                                              leftMargin, a, isLeftToRight(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                inset += binsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                inset += getLength(CSS.Attribute.PADDING_LEFT, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            case View.RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                inset += getOrientationMargin(HorizontalMargin.RIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                                              rightMargin, a, isLeftToRight(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                inset += binsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                inset += getLength(CSS.Attribute.PADDING_RIGHT, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            case View.TOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                inset += topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                inset += binsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                inset += getLength(CSS.Attribute.PADDING_TOP, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            case View.BOTTOM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                inset += bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                inset += binsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                inset += getLength(CSS.Attribute.PADDING_BOTTOM, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                throw new IllegalArgumentException("Invalid side: " + side);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            return inset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
         * Paints the CSS box according to the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
         * given.  This should paint the border, padding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
         * and background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
         * @param g the rendering surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
         * @param x the x coordinate of the allocated area to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
         *  render into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
         * @param y the y coordinate of the allocated area to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         *  render into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         * @param w the width of the allocated area to render into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         * @param h the height of the allocated area to render into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
         * @param v the view making the request.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
         *  used to get the AttributeSet, and may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         *  resolve percentage arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        public void paint(Graphics g, float x, float y, float w, float h, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            // PENDING(prinz) implement real rendering... which would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            // do full set of border and background capabilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            // remove margin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            float dx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            float dy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            float dw = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            float dh = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            AttributeSet a = v.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            boolean isLeftToRight = isLeftToRight(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            float localLeftMargin = getOrientationMargin(HorizontalMargin.LEFT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                                                         leftMargin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                                                         a, isLeftToRight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            float localRightMargin = getOrientationMargin(HorizontalMargin.RIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                                                          rightMargin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                                                          a, isLeftToRight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            if (!(v instanceof HTMLEditorKit.HTMLFactory.BodyBlockView)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                dx = localLeftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                dy = topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                dw = -(localLeftMargin + localRightMargin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                dh = -(topMargin + bottomMargin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            if (bg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                g.setColor(bg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                g.fillRect((int) (x + dx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                           (int) (y + dy),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                           (int) (w + dw),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                           (int) (h + dh));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            if (bgPainter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                bgPainter.paint(g, x + dx, y + dy, w + dw, h + dh, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            x += localLeftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            y += topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            w -= localLeftMargin + localRightMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            h -= topMargin + bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            if (border instanceof BevelBorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                //BevelBorder does not support border width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                int bw = (int) getLength(CSS.Attribute.BORDER_TOP_WIDTH, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                for (int i = bw - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    border.paintBorder(null, g, (int) x + i, (int) y + i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                                       (int) w - 2 * i, (int) h - 2 * i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                border.paintBorder(null, g, (int) x, (int) y, (int) w, (int) h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        float getLength(CSS.Attribute key, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            return css.getLength(a, key, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        static boolean isLeftToRight(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            boolean ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            if (isOrientationAware(v)) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1985
                Container container;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                if (v != null && (container = v.getContainer()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    ret = container.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * only certain tags are concerned about orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         * <dir>, <menu>, <ul>, <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
         * for all others we return true. It is implemented this way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
         * for performance purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        static boolean isOrientationAware(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            boolean ret = false;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2001
            AttributeSet attr;
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2002
            Object obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            if (v != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                && (attr = v.getElement().getAttributes()) != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                && (obj = attr.getAttribute(StyleConstants.NameAttribute)) instanceof HTML.Tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                && (obj == HTML.Tag.DIR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    || obj == HTML.Tag.MENU
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                    || obj == HTML.Tag.UL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                    || obj == HTML.Tag.OL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2016
        static enum HorizontalMargin { LEFT, RIGHT }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
         * for <dir>, <menu>, <ul> etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
         * margins are Left-To-Right/Right-To-Left depended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
         * see 5088268 for more details
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         * margin-(left|right)-(ltr|rtl) were introduced to describe it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         * if margin-(left|right) is present we are to use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
         * @param side The horizontal side to fetch margin for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
         *  This can be HorizontalMargin.LEFT or HorizontalMargin.RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
         * @param cssMargin margin from css
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
         * @param a AttributeSet for the View we getting margin for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
         * @param isLeftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
         * @return orientation depended margin
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        float getOrientationMargin(HorizontalMargin side, float cssMargin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                                   AttributeSet a, boolean isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            float margin = cssMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            float orientationMargin = cssMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            Object cssMarginValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            switch (side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            case RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                    orientationMargin = (isLeftToRight) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                        getLength(CSS.Attribute.MARGIN_RIGHT_LTR, a) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                        getLength(CSS.Attribute.MARGIN_RIGHT_RTL, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                    cssMarginValue = a.getAttribute(CSS.Attribute.MARGIN_RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            case LEFT :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                    orientationMargin = (isLeftToRight) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                        getLength(CSS.Attribute.MARGIN_LEFT_LTR, a) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                        getLength(CSS.Attribute.MARGIN_LEFT_RTL, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                    cssMarginValue = a.getAttribute(CSS.Attribute.MARGIN_LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            if (cssMarginValue == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                && orientationMargin != Integer.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                margin = orientationMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            return margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        float topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        float bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        float leftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        float rightMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        // Bitmask, used to indicate what margins are relative:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        // bit 0 for top, 1 for bottom, 2 for left and 3 for right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        short marginFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        Border border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        Insets binsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        CSS css;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        StyleSheet ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        Color bg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        BackgroundImagePainter bgPainter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * Class to carry out some of the duties of CSS list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * formatting.  Implementations of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * class enable views to present the CSS formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * while not knowing anything about how the CSS values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * are being cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2085
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
    public static class ListPainter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        ListPainter(AttributeSet attr, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            /* Get the image to use as a list bullet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            String imgstr = (String)attr.getAttribute(CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                                                      LIST_STYLE_IMAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            if (imgstr != null && !imgstr.equals("none")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                String tmpstr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                    StringTokenizer st = new StringTokenizer(imgstr, "()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                    if (st.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                        tmpstr = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                    if (st.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                        tmpstr = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                    URL u = new URL(tmpstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    img = new ImageIcon(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    if (tmpstr != null && ss != null && ss.getBase() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                            URL u = new URL(ss.getBase(), tmpstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                            img = new ImageIcon(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                        } catch (MalformedURLException murle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                            img = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                        img = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            /* Get the type of bullet to use in the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            if (img == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                type = (CSS.Value)attr.getAttribute(CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                                                    LIST_STYLE_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            start = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            paintRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
         * Returns a string that represents the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
         * of the HTML.Attribute.TYPE attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
         * If this attributes is not defined, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
         * then the type defaults to "disc" unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
         * the tag is on Ordered list.  In the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
         * of the latter, the default type is "decimal".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        private CSS.Value getChildType(View childView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            CSS.Value childtype = (CSS.Value)childView.getAttributes().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                                  getAttribute(CSS.Attribute.LIST_STYLE_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
            if (childtype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    // Parent view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                    View v = childView.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                    HTMLDocument doc = (HTMLDocument)v.getDocument();
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 22260
diff changeset
  2146
                    if (HTMLDocument.matchNameAttribute(v.getElement().getAttributes(),
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 22260
diff changeset
  2147
                                                        HTML.Tag.OL)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                        childtype = CSS.Value.DECIMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                        childtype = CSS.Value.DISC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                    childtype = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            return childtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
         * Obtains the starting index from <code>parent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        private void getStart(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            checkedForStart = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            Element element = parent.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            if (element != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                AttributeSet attr = element.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                Object startValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                if (attr != null && attr.isDefined(HTML.Attribute.START) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                    (startValue = attr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                     (HTML.Attribute.START)) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                    (startValue instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                        start = Integer.parseInt((String)startValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         * Returns an integer that should be used to render the child at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         * <code>childIndex</code> with. The retValue will usually be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
         * <code>childIndex</code> + 1, unless <code>parentView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
         * has some Views that do not represent LI's, or one of the views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
         * has a HTML.Attribute.START specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        private int getRenderIndex(View parentView, int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            if (!checkedForStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                getStart(parentView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
            int retIndex = childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            for (int counter = childIndex; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                AttributeSet as = parentView.getElement().getElement(counter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                                  getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                if (as.getAttribute(StyleConstants.NameAttribute) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    HTML.Tag.LI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                    retIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                } else if (as.isDefined(HTML.Attribute.VALUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    Object value = as.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    if (value != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                        (value instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                            int iValue = Integer.parseInt((String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                            return retIndex - counter + iValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                        catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            return retIndex + start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
         * Paints the CSS list decoration according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
         * attributes given.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
         * @param g the rendering surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
         * @param x the x coordinate of the list item allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
         * @param y the y coordinate of the list item allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
         * @param w the width of the list item allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
         * @param h the height of the list item allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
         * @param v the allocated area to paint into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
         * @param item which list item is being painted.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
         *  is a number greater than or equal to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        public void paint(Graphics g, float x, float y, float w, float h, View v, int item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
            View cv = v.getView(item);
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2229
            Container host = v.getContainer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            Object name = cv.getElement().getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                         (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            // Only draw something if the View is a list item. This won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            // be the case for comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            if (!(name instanceof HTML.Tag) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                name != HTML.Tag.LI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            // deside on what side draw bullets, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            isLeftToRight =
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2240
                host.getComponentOrientation().isLeftToRight();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            // How the list indicator is aligned is not specified, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            // left up to the UA. IE and NS differ on this behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            // This is closer to NS where we align to the first line of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            // If the child is not text we draw the indicator at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            // origin (0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            float align = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            if (cv.getViewCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                View pView = cv.getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                Object cName = pView.getElement().getAttributes().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                               getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                if ((cName == HTML.Tag.P || cName == HTML.Tag.IMPLIED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                              pView.getViewCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                    paintRect.setBounds((int)x, (int)y, (int)w, (int)h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    Shape shape = cv.getChildAllocation(0, paintRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                    if (shape != null && (shape = pView.getView(0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                                 getChildAllocation(0, shape)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                        Rectangle rect = (shape instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                                         (Rectangle)shape : shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                        align = pView.getView(0).getAlignment(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                        y = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                        h = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            // set the color of a decoration
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2269
            Color c = (host.isEnabled()
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2270
                ? (ss != null
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2271
                    ? ss.getForeground(cv.getAttributes())
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2272
                    : host.getForeground())
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2273
                : UIManager.getColor("textInactiveText"));
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2274
            g.setColor(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            if (img != null) {
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 1639
diff changeset
  2277
                drawIcon(g, (int) x, (int) y, (int) w, (int) h, align, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            CSS.Value childtype = getChildType(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            Font font = ((StyledDocument)cv.getDocument()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                                         getFont(cv.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                g.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            if (childtype == CSS.Value.SQUARE || childtype == CSS.Value.CIRCLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                || childtype == CSS.Value.DISC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                drawShape(g, childtype, (int) x, (int) y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                          (int) w, (int) h, align);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            } else if (childtype == CSS.Value.DECIMAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                drawLetter(g, '1', (int) x, (int) y, (int) w, (int) h, align,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                           getRenderIndex(v, item));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            } else if (childtype == CSS.Value.LOWER_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                drawLetter(g, 'a', (int) x, (int) y, (int) w, (int) h, align,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                           getRenderIndex(v, item));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            } else if (childtype == CSS.Value.UPPER_ALPHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                drawLetter(g, 'A', (int) x, (int) y, (int) w, (int) h, align,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                           getRenderIndex(v, item));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            } else if (childtype == CSS.Value.LOWER_ROMAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                drawLetter(g, 'i', (int) x, (int) y, (int) w, (int) h, align,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                           getRenderIndex(v, item));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            } else if (childtype == CSS.Value.UPPER_ROMAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                drawLetter(g, 'I', (int) x, (int) y, (int) w, (int) h, align,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                           getRenderIndex(v, item));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
         * Draws the bullet icon specified by the list-style-image argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
         * @param g     the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
         * @param ax    x coordinate to place the bullet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
         * @param ay    y coordinate to place the bullet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
         * @param aw    width of the container the bullet is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
         * @param ah    height of the container the bullet is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
         * @param align preferred alignment factor for the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        void drawIcon(Graphics g, int ax, int ay, int aw, int ah,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                      float align, Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            // Align to bottom of icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            int gap = isLeftToRight ? - (img.getIconWidth() + bulletgap) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                                        (aw + bulletgap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            int x = ax + gap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            int y = Math.max(ay, ay + (int)(align * ah) -img.getIconHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            img.paintIcon(c, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
         * Draws the graphical bullet item specified by the type argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
         * @param g     the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
         * @param type  type of bullet to draw (circle, square, disc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
         * @param ax    x coordinate to place the bullet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
         * @param ay    y coordinate to place the bullet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
         * @param aw    width of the container the bullet is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
         * @param ah    height of the container the bullet is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
         * @param align preferred alignment factor for the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        void drawShape(Graphics g, CSS.Value type, int ax, int ay, int aw,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                       int ah, float align) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            // Align to bottom of shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            int gap = isLeftToRight ? - (bulletgap + 8) : (aw + bulletgap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            int x = ax + gap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            int y = Math.max(ay, ay + (int)(align * ah) - 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            if (type == CSS.Value.SQUARE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                g.drawRect(x, y, 8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            } else if (type == CSS.Value.CIRCLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                g.drawOval(x, y, 8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                g.fillOval(x, y, 8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
         * Draws the letter or number for an ordered list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
         * @param g     the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
         * @param letter type of ordered list to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
         * @param ax    x coordinate to place the bullet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
         * @param ay    y coordinate to place the bullet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
         * @param aw    width of the container the bullet is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
         * @param ah    height of the container the bullet is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
         * @param index position of the list item in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        void drawLetter(Graphics g, char letter, int ax, int ay, int aw,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                        int ah, float align, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            String str = formatItemNum(index, letter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            str = isLeftToRight ? str + "." : "." + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            FontMetrics fm = SwingUtilities2.getFontMetrics(null, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            int stringwidth = SwingUtilities2.stringWidth(null, fm, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            int gap = isLeftToRight ? - (stringwidth + bulletgap) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                                        (aw + bulletgap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            int x = ax + gap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            int y = Math.max(ay + fm.getAscent(), ay + (int)(ah * align));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            SwingUtilities2.drawString(null, g, str, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
         * Converts the item number into the ordered list number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
         * (i.e.  1 2 3, i ii iii, a b c, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
         * @param itemNum number to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
         * @param type    type of ordered list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
         */
24528
21c5bb3d76cc 8039860: Fix fallthrough lint warnings in swing
darcy
parents: 23715
diff changeset
  2387
        @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        String formatItemNum(int itemNum, char type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            String numStyle = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            boolean uppercase = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            String formattedNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                formattedNum = String.valueOf(itemNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            case 'A':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                uppercase = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                formattedNum = formatAlphaNumerals(itemNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                uppercase = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            case 'i':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                formattedNum = formatRomanNumerals(itemNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            if (uppercase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                formattedNum = formattedNum.toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            return formattedNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
         * Converts the item number into an alphabetic character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
         * @param itemNum number to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        String formatAlphaNumerals(int itemNum) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2428
            String result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            if (itemNum > 26) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                result = formatAlphaNumerals(itemNum / 26) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                    formatAlphaNumerals(itemNum % 26);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                // -1 because item is 1 based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                result = String.valueOf((char)('a' + itemNum - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        /* list of roman numerals */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        static final char romanChars[][] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            {'i', 'v'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            {'x', 'l' },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            {'c', 'd' },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            {'m', '?' },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
         * Converts the item number into a roman numeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
         * @param num  number to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        String formatRomanNumerals(int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            return formatRomanNumerals(0, num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
         * Converts the item number into a roman numeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
         * @param num  number to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        String formatRomanNumerals(int level, int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
            if (num < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                return formatRomanDigit(level, num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                return formatRomanNumerals(level + 1, num / 10) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                    formatRomanDigit(level, num % 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
         * Converts the item number into a roman numeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
         * @param level position
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2477
         * @param digit digit to format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
        String formatRomanDigit(int level, int digit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
            String result = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            if (digit == 9) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                result = result + romanChars[level][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                result = result + romanChars[level + 1][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
            } else if (digit == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                result = result + romanChars[level][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                result = result + romanChars[level][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            } else if (digit >= 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                result = result + romanChars[level][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                digit -= 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            for (int i = 0; i < digit; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                result = result + romanChars[level][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        private Rectangle paintRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        private boolean checkedForStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        private int start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        private CSS.Value type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        URL imageurl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        private StyleSheet ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        Icon img = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        private int bulletgap = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
        private boolean isLeftToRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * Paints the background image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2516
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
    static class BackgroundImagePainter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        ImageIcon   backgroundImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
        float       hPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
        float       vPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        // bit mask: 0 for repeat x, 1 for repeat y, 2 for horiz relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        // 3 for vert relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
        short       flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        // These are used when painting, updatePaintCoordinates updates them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
        private int paintX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        private int paintY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        private int paintMaxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        private int paintMaxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
        BackgroundImagePainter(AttributeSet a, CSS css, StyleSheet ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            backgroundImage = ss.getBackgroundImage(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            // Determine the position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            CSS.BackgroundPosition pos = (CSS.BackgroundPosition)a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                                           (CSS.Attribute.BACKGROUND_POSITION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            if (pos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                hPosition = pos.getHorizontalPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                vPosition = pos.getVerticalPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                if (pos.isHorizontalPositionRelativeToSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                    flags |= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                else if (pos.isHorizontalPositionRelativeToSize()) {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 22260
diff changeset
  2542
                    hPosition *= CSS.getFontSize(a, 12, ss);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                if (pos.isVerticalPositionRelativeToSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                    flags |= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                else if (pos.isVerticalPositionRelativeToFontSize()) {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 22260
diff changeset
  2548
                    vPosition *= CSS.getFontSize(a, 12, ss);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            // Determine any repeating values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            CSS.Value repeats = (CSS.Value)a.getAttribute(CSS.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                                                          BACKGROUND_REPEAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            if (repeats == null || repeats == CSS.Value.BACKGROUND_REPEAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                flags |= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
            else if (repeats == CSS.Value.BACKGROUND_REPEAT_X) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                flags |= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            else if (repeats == CSS.Value.BACKGROUND_REPEAT_Y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                flags |= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
  2565
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        void paint(Graphics g, float x, float y, float w, float h, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
            Rectangle clip = g.getClipRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                // Constrain the clip so that images don't draw outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                // legal bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                g.clipRect((int)x, (int)y, (int)w, (int)h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            if ((flags & 3) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                // no repeating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                int width = backgroundImage.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                int height = backgroundImage.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                if ((flags & 4) == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                    paintX = (int)(x + w * hPosition -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                                  (float)width * hPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                    paintX = (int)x + (int)hPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                if ((flags & 8) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                    paintY = (int)(y + h * vPosition -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                                  (float)height * vPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                    paintY = (int)y + (int)vPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                if (clip == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                    !((paintX + width <= clip.x) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                      (paintY + height <= clip.y) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                      (paintX >= clip.x + clip.width) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                      (paintY >= clip.y + clip.height))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                    backgroundImage.paintIcon(null, g, paintX, paintY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                int width = backgroundImage.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                int height = backgroundImage.getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                if (width > 0 && height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                    paintX = (int)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                    paintY = (int)y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                    paintMaxX = (int)(x + w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                    paintMaxY = (int)(y + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                    if (updatePaintCoordinates(clip, width, height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                        while (paintX < paintMaxX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                            int ySpot = paintY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                            while (ySpot < paintMaxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                                backgroundImage.paintIcon(null, g, paintX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                                                          ySpot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                                ySpot += height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                            paintX += width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
                // Reset clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                g.setClip(clip.x, clip.y, clip.width, clip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        private boolean updatePaintCoordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                 (Rectangle clip, int width, int height){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            if ((flags & 3) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                paintMaxY = paintY + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            else if ((flags & 3) == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                paintMaxX = paintX + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                if ((flags & 3) == 1 && ((paintY + height <= clip.y) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                                         (paintY > clip.y + clip.height))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                    // not visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                if ((flags & 3) == 2 && ((paintX + width <= clip.x) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                                         (paintX > clip.x + clip.width))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                    // not visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                if ((flags & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                    if ((clip.x + clip.width) < paintMaxX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                        if ((clip.x + clip.width - paintX) % width == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                            paintMaxX = clip.x + clip.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                            paintMaxX = ((clip.x + clip.width - paintX) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                                         width + 1) * width + paintX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                    if (clip.x > paintX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                        paintX = (clip.x - paintX) / width * width + paintX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                if ((flags & 2) == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                    if ((clip.y + clip.height) < paintMaxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                        if ((clip.y + clip.height - paintY) % height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                            paintMaxY = clip.y + clip.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                            paintMaxY = ((clip.y + clip.height - paintY) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                                         height + 1) * height + paintY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                    if (clip.y > paintY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                        paintY = (clip.y - paintY) / height * height + paintY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            // Valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
     * A subclass of MuxingAttributeSet that translates between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
     * CSS and HTML and StyleConstants. The AttributeSets used are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
     * the CSS rules that match the Views Elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2685
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
    class ViewAttributeSet extends MuxingAttributeSet {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        ViewAttributeSet(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            host = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            // PENDING(prinz) fix this up to be a more realistic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            // implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            Document doc = v.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            SearchBuffer sb = SearchBuffer.obtainSearchBuffer();
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  2694
            @SuppressWarnings("unchecked")
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2695
            Vector<AttributeSet> muxList = sb.getVector();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                    StyleSheet styles = StyleSheet.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                    Element elem = v.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                    AttributeSet a = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                    AttributeSet htmlAttr = styles.translateHTMLToCSS(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                    if (htmlAttr.getAttributeCount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                        muxList.addElement(htmlAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                    if (elem.isLeaf()) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24528
diff changeset
  2707
                        Enumeration<?> keys = a.getAttributeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                            Object key = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
                            if (key instanceof HTML.Tag) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2711
                                if (key == HTML.Tag.A) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  2712
                                    Object o = a.getAttribute(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                                   In the case of an A tag, the css rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                                   apply only for tags that have their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                                   href attribute defined and not for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                                   anchors that only have their name attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                                   defined, i.e anchors that function as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                                   destinations.  Hence we do not add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                                   attributes for that latter kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                                   anchors.  When CSS2 support is added,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                                   it will be possible to specificity this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                                   kind of conditional behaviour in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                                   stylesheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                                    if (o != null && o instanceof AttributeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                                        AttributeSet attr = (AttributeSet)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                                        if (attr.getAttribute(HTML.Attribute.HREF) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                                AttributeSet cssRule = styles.getRule((HTML.Tag) key, elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                                if (cssRule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                                    muxList.addElement(cssRule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                        HTML.Tag t = (HTML.Tag) a.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                                     (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                        AttributeSet cssRule = styles.getRule(t, elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                        if (cssRule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                            muxList.addElement(cssRule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                AttributeSet[] attrs = new AttributeSet[muxList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                muxList.copyInto(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                setAttributes(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                SearchBuffer.releaseSearchBuffer(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        //  --- AttributeSet methods ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
         * Checks whether a given attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
         * This will convert the key over to CSS if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
         * key is a StyleConstants key that has a CSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
         * mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
         * @param key the attribute key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
         * @return true if the attribute is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
         * @see AttributeSet#isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
        public boolean isDefined(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
            if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                                    ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                    key = cssKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            return super.isDefined(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
         * Gets the value of an attribute.  If the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
         * attribute is a StyleConstants attribute that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
         * a CSS mapping, the request will be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
         * @param key the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
         * @return the attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
         * @see AttributeSet#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        public Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
            if (key instanceof StyleConstants) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                Object cssKey = css.styleConstantsKeyToCSSKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                               ((StyleConstants)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                    Object value = doGetAttribute(cssKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    if (value instanceof CSS.CssValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                        return ((CSS.CssValue)value).toStyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                                     ((StyleConstants)key, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            return doGetAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
        Object doGetAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
            Object retValue = super.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            if (retValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            // didn't find it... try parent if it's a css attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            // that is inherited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            if (key instanceof CSS.Attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                CSS.Attribute css = (CSS.Attribute) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                if (css.isInherited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                    AttributeSet parent = getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                    if (parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                        return parent.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
         * If not overriden, the resolving parent defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
         * the parent element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
         * @return the attributes from the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
         * @see AttributeSet#getResolveParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        public AttributeSet getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
            View parent = host.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
            return (parent != null) ? parent.getAttributes() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        /** View created for. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        View host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * A subclass of MuxingAttributeSet that implements Style. Currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     * the MutableAttributeSet methods are unimplemented, that is they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
    // PENDING(sky): Decide what to do with this. Either make it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
    // contain a SimpleAttributeSet that modify methods are delegated to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
    // or change getRule to return an AttributeSet and then don't make this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
    // implement Style.
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  2851
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
    static class ResolvedStyle extends MuxingAttributeSet implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                  Serializable, Style {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        ResolvedStyle(String name, AttributeSet[] attrs, int extendedIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
            super(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            this.extendedIndex = extendedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
         * Inserts a Style into the receiver so that the styles the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
         * receiver represents are still ordered by specificity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
         * <code>style</code> will be added before any extended styles, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
         * is before extendedIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        synchronized void insertStyle(Style style, int specificity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
            AttributeSet[] attrs = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            int maxCounter = attrs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
            int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            for (;counter < extendedIndex; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                if (specificity > getSpecificity(((Style)attrs[counter]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                                                 getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            insertAttributeSetAt(style, counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
            extendedIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
         * Removes a previously added style. This will do nothing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
         * <code>style</code> is not referenced by the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
        synchronized void removeStyle(Style style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            AttributeSet[] attrs = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
            for (int counter = attrs.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                if (attrs[counter] == style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                    removeAttributeSetAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                    if (counter < extendedIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                        extendedIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
         * Adds <code>s</code> as one of the Attributesets to look up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
         * attributes in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        synchronized void insertExtendedStyleAt(Style attr, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            insertAttributeSetAt(attr, extendedIndex + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
         * Adds <code>s</code> as one of the AttributeSets to look up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
         * attributes in. It will be the AttributeSet last checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        synchronized void addExtendedStyle(Style attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            insertAttributeSetAt(attr, getAttributes().length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
         * Removes the style at <code>index</code> +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
         * <code>extendedIndex</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
        synchronized void removeExtendedStyleAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            removeAttributeSetAt(extendedIndex + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
         * Returns true if the receiver matches <code>selector</code>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
         * a match is defined by the CSS rule matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
         * Each simple selector must be separated by a single space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
        protected boolean matches(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
            int sLast = selector.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
            if (sLast == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
            int thisLast = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
            int sCurrent = selector.lastIndexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
            int thisCurrent = name.lastIndexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            if (sCurrent >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                sCurrent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            if (thisCurrent >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                thisCurrent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            if (!matches(selector, sCurrent, sLast, thisCurrent, thisLast)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            while (sCurrent != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                sLast = sCurrent - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                sCurrent = selector.lastIndexOf(' ', sLast - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                if (sCurrent >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                    sCurrent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                boolean match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                while (!match && thisCurrent != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                    thisLast = thisCurrent - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                    thisCurrent = name.lastIndexOf(' ', thisLast - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                    if (thisCurrent >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                        thisCurrent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                    match = matches(selector, sCurrent, sLast, thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                                    thisLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                if (!match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
         * Returns true if the substring of the receiver, in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
         * thisCurrent, thisLast matches the substring of selector in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
         * the ranme sCurrent to sLast based on CSS selector matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        boolean matches(String selector, int sCurrent, int sLast,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                       int thisCurrent, int thisLast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            sCurrent = Math.max(sCurrent, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
            thisCurrent = Math.max(thisCurrent, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
            int thisDotIndex = boundedIndexOf(name, '.', thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                                              thisLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            int thisPoundIndex = boundedIndexOf(name, '#', thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                                                thisLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
            int sDotIndex = boundedIndexOf(selector, '.', sCurrent, sLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            int sPoundIndex = boundedIndexOf(selector, '#', sCurrent, sLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
            if (sDotIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                // Selector has a '.', which indicates name must match it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                // or if the '.' starts the selector than name must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
                // the same class (doesn't matter what element name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                if (thisDotIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                if (sCurrent == sDotIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                    if ((thisLast - thisDotIndex) != (sLast - sDotIndex) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
                        !selector.regionMatches(sCurrent, name, thisDotIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
                                                (thisLast - thisDotIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                    // Has to fully match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                    if ((sLast - sCurrent) != (thisLast - thisCurrent) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                        !selector.regionMatches(sCurrent, name, thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                                                (thisLast - thisCurrent))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            if (sPoundIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                // Selector has a '#', which indicates name must match it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                // or if the '#' starts the selector than name must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                // the same id (doesn't matter what element name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                if (thisPoundIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                if (sCurrent == sPoundIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                    if ((thisLast - thisPoundIndex) !=(sLast - sPoundIndex) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                        !selector.regionMatches(sCurrent, name, thisPoundIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                                                (thisLast - thisPoundIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                    // Has to fully match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                    if ((sLast - sCurrent) != (thisLast - thisCurrent) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                        !selector.regionMatches(sCurrent, name, thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                                               (thisLast - thisCurrent))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            if (thisDotIndex != -1) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  3032
                // Receiver references a class, just check element name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
                return (((thisDotIndex - thisCurrent) == (sLast - sCurrent)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
                        selector.regionMatches(sCurrent, name, thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
                                               thisDotIndex - thisCurrent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
            if (thisPoundIndex != -1) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  3038
                // Receiver references an id, just check element name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
                return (((thisPoundIndex - thisCurrent) ==(sLast - sCurrent))&&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
                        selector.regionMatches(sCurrent, name, thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                                               thisPoundIndex - thisCurrent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            // Fail through, no classes or ides, just check string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            return (((thisLast - thisCurrent) == (sLast - sCurrent)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                    selector.regionMatches(sCurrent, name, thisCurrent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                                           thisLast - thisCurrent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
        /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  3050
         * Similar to String.indexOf, but allows an upper bound
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
         * (this is slower in that it will still check string starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
         * start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        int boundedIndexOf(String string, char search, int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
                           int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            int retValue = string.indexOf(search, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
            if (retValue >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
        public void addAttribute(Object name, Object value) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        public void addAttributes(AttributeSet attributes) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
        public void removeAttribute(Object name) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
        public void removeAttributes(Enumeration<?> names) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
        public void removeAttributes(AttributeSet attributes) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
        public void setResolveParent(AttributeSet parent) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        public String getName() {return name;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
        public void addChangeListener(ChangeListener l) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        public void removeChangeListener(ChangeListener l) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        public ChangeListener[] getChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            return new ChangeListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        /** The name of the Style, which is the selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
         * This will NEVER change!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        /** Start index of styles coming from other StyleSheets. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        private int extendedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
     * SelectorMapping contains a specifitiy, as an integer, and an associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
     * Style. It can also reference children <code>SelectorMapping</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
     * so that it behaves like a tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
     * This is not thread safe, it is assumed the caller will take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     * necessary precations if this is to be used in a threaded environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22567
diff changeset
  3093
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
    static class SelectorMapping implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        public SelectorMapping(int specificity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
            this.specificity = specificity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
         * Returns the specificity this mapping represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        public int getSpecificity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            return specificity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
         * Sets the Style associated with this mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        public void setStyle(Style style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
            this.style = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
         * Returns the Style associated with this mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
        public Style getStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
            return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
         * Returns the child mapping identified by the simple selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
         * <code>selector</code>. If a child mapping does not exist for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
         *<code>selector</code>, and <code>create</code> is true, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
         * one will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        public SelectorMapping getChildSelectorMapping(String selector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
                                                       boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
            SelectorMapping retValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            if (children != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3131
                retValue = children.get(selector);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
            else if (create) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3134
                children = new HashMap<String, SelectorMapping>(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
            if (retValue == null && create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                int specificity = getChildSpecificity(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
                retValue = createChildSelectorMapping(specificity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                children.put(selector, retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
         * Creates a child <code>SelectorMapping</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
         * <code>specificity</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        protected SelectorMapping createChildSelectorMapping(int specificity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            return new SelectorMapping(specificity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
         * Returns the specificity for the child selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
         * <code>selector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        protected int getChildSpecificity(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
            // class (.) 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
            // id (#)    10000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            char    firstChar = selector.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
            int     specificity = getSpecificity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
            if (firstChar == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
                specificity += 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            else if (firstChar == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                specificity += 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
                specificity += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                if (selector.indexOf('.') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                    specificity += 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                if (selector.indexOf('#') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
                    specificity += 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
            return specificity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
         * The specificity for this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
        private int specificity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
         * Style for this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
        private Style style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
         * Any sub selectors. Key will be String, and value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
         * another SelectorMapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
         */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3193
        private HashMap<String, SelectorMapping> children;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
    // ---- Variables ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 28231
diff changeset
  3199
    static final int DEFAULT_FONT_SIZE = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    private CSS css;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     * An inverted graph of the selectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
    private SelectorMapping selectorMapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
    /** Maps from selector (as a string) to Style that includes all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     * relevant styles. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3210
    private Hashtable<String, ResolvedStyle> resolvedStyles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
    /** Vector of StyleSheets that the rules are to reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3214
    private Vector<StyleSheet> linkedStyleSheets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
    /** Where the style sheet was found. Used for relative imports. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
    private URL base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
     * Default parser for CSS specifications that get loaded into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
     * the StyleSheet.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
     * This class is NOT thread safe, do not ask it to parse while it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     * in the middle of parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
    class CssParser implements CSSParser.CSSParserCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
         * Parses the passed in CSS declaration into an AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        public AttributeSet parseDeclaration(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                return parseDeclaration(new StringReader(string));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            } catch (IOException ioe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
         * Parses the passed in CSS declaration into an AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
        public AttributeSet parseDeclaration(Reader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
            parse(base, r, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
            return declaration.copyAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
         * Parse the given CSS stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
        public void parse(URL base, Reader r, boolean parseDeclaration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
                          boolean isLink) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
            this.base = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
            this.isLink = isLink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            this.parsingDeclaration = parseDeclaration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            declaration.removeAttributes(declaration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
            selectorTokens.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
            selectors.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            propertyName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            parser.parse(r, this, parseDeclaration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
        // CSSParserCallback methods, public to implement the interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
         * Invoked when a valid @import is encountered, will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
         * <code>importStyleSheet</code> if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
         * <code>MalformedURLException</code> is not thrown in creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
         * the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
        public void handleImport(String importString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            URL url = CSS.getURL(base, importString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
                importStyleSheet(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
         * A selector has been encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
        public void handleSelector(String selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
            //class and index selectors are case sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
            if (!(selector.startsWith(".")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                  || selector.startsWith("#"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                selector = selector.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
            int length = selector.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
            if (selector.endsWith(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
                if (length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                    selector = selector.substring(0, length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                    selectorTokens.addElement(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                addSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
            else if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                selectorTokens.addElement(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
         * Invoked when the start of a rule is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
        public void startRule() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
            if (selectorTokens.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
                addSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
            propertyName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
         * Invoked when a property name is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
        public void handleProperty(String property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
            propertyName = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
         * Invoked when a property value is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
        public void handleValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
            if (propertyName != null && value != null && value.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                CSS.Attribute cssKey = CSS.getAttribute(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
                if (cssKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
                    // There is currently no mechanism to determine real
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
                    // base that style sheet was loaded from. For the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
                    // being, this maps for LIST_STYLE_IMAGE, which appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                    // to be the only one that currently matters. A more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                    // general mechanism is definately needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                    if (cssKey == CSS.Attribute.LIST_STYLE_IMAGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                        if (value != null && !value.equals("none")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                            URL url = CSS.getURL(base, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                                value = url.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                    addCSSAttribute(declaration, cssKey, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                propertyName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
         * Invoked when the end of a rule is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        public void endRule() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
            int n = selectors.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            for (int i = 0; i < n; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3351
                String[] selector = selectors.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                if (selector.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                    StyleSheet.this.addRule(selector, declaration, isLink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
            declaration.removeAttributes(declaration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
            selectors.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
        private void addSelector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            String[] selector = new String[selectorTokens.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
            selectorTokens.copyInto(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
            selectors.addElement(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
            selectorTokens.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3368
        Vector<String[]> selectors = new Vector<String[]>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  3369
        Vector<String> selectorTokens = new Vector<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        /** Name of the current property. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        String propertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        MutableAttributeSet declaration = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        /** True if parsing a declaration, that is the Reader will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
         * contain a selector. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
        boolean parsingDeclaration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        /** True if the attributes are coming from a linked/imported style. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
        boolean isLink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        /** Where the CSS stylesheet lives. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        URL base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
        CSSParser parser = new CSSParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
    void rebaseSizeMap(int base) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
        final int minimalFontSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
        sizeMap = new int[sizeMapDefault.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
        for (int i = 0; i < sizeMapDefault.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
            sizeMap[i] = Math.max(base * sizeMapDefault[i] /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                                  sizeMapDefault[CSS.baseFontSizeIndex],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                                  minimalFontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
    int[] getSizeMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
        return sizeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
    boolean isW3CLengthUnits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
        return w3cLengthUnits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * The HTML/CSS size model has seven slots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     * that one can assign sizes to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
    static final int sizeMapDefault[] = { 8, 10, 12, 14, 18, 24, 36 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
    private int sizeMap[] = sizeMapDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
    private boolean w3cLengthUnits = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
}