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