jdk/src/share/classes/javax/swing/plaf/synth/DefaultSynthStyleFactory.java
author rupashka
Fri, 25 Jul 2008 17:50:36 +0400
changeset 1290 da8902cd496c
parent 2 90ce3da70b43
child 1639 a97859015238
permissions -rw-r--r--
6727661: Code improvement and warnings removing from the swing/plaf packages Summary: Removed unnecessary castings and other warnings Reviewed-by: alexp Contributed-by: Florian Brunner <fbrunnerlist@gmx.ch>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.plaf.FontUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.swing.plaf.synth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.swing.BakedArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Factory used for obtaining styles. Supports associating a style based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the name of the component as returned by <code>Component.getName()</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and the <code>Region</code> associated with the <code>JComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Lookup is done using regular expressions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class DefaultSynthStyleFactory extends SynthStyleFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Used to indicate the lookup should be done based on Component name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static final int NAME = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Used to indicate the lookup should be done based on region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final int REGION = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * List containing set of StyleAssociations used in determining matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private List<StyleAssociation> _styles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Used during lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private BakedArrayList _tmpList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Maps from a List (BakedArrayList to be precise) to the merged style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    66
    private Map<BakedArrayList, SynthStyle> _resolvedStyles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Used if there are no styles matching a widget.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private SynthStyle _defaultStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    DefaultSynthStyleFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        _tmpList = new BakedArrayList(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        _styles = new ArrayList<StyleAssociation>();
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    77
        _resolvedStyles = new HashMap<BakedArrayList, SynthStyle>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public synchronized void addStyle(DefaultSynthStyle style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         String path, int type) throws PatternSyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // Make an empty path match all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            path = ".*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (type == NAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            _styles.add(StyleAssociation.createStyleAssociation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                            path, style, type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        else if (type == REGION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            _styles.add(StyleAssociation.createStyleAssociation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                            path.toLowerCase(), style, type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Returns the style for the specified Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param c Component asking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param id ID of the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public synchronized SynthStyle getStyle(JComponent c, Region id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        BakedArrayList matches = _tmpList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        matches.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        getMatchingStyles(matches, c, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (matches.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return getDefaultStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // Use a cached Style if possible, otherwise create a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        matches.cacheHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SynthStyle style = getCachedStyle(matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            style = mergeStyles(matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (style != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                cacheStyle(matches, style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the style to use if there are no matching styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private SynthStyle getDefaultStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (_defaultStyle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            _defaultStyle = new DefaultSynthStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ((DefaultSynthStyle)_defaultStyle).setFont(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                new FontUIResource(Font.DIALOG, Font.PLAIN,12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return _defaultStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Fetches any styles that match the passed into arguments into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>matches</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   141
    private void getMatchingStyles(List matches, JComponent c,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                   Region id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String idName = id.getLowerCaseName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String cName = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (cName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            cName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (int counter = _styles.size() - 1; counter >= 0; counter--){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            StyleAssociation sa = _styles.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (sa.getID() == NAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                path = cName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                path = idName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (sa.matches(path) && matches.indexOf(sa.getStyle()) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                matches.add(sa.getStyle());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Caches the specified style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   169
    private void cacheStyle(List styles, SynthStyle style) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        BakedArrayList cachedStyles = new BakedArrayList(styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        _resolvedStyles.put(cachedStyles, style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns the cached style from the passed in arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   178
    private SynthStyle getCachedStyle(List styles) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (styles.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   182
        return _resolvedStyles.get(styles);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Creates a single Style from the passed in styles. The passed in List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * is reverse sorted, that is the most recently added style found to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * match will be first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   190
    private SynthStyle mergeStyles(List styles) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int size = styles.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        else if (size == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return (SynthStyle)((DefaultSynthStyle)styles.get(0)).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // NOTE: merging is done backwards as DefaultSynthStyleFactory reverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // order, that is, the most specific style is first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        DefaultSynthStyle style = (DefaultSynthStyle)styles.get(size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        style = (DefaultSynthStyle)style.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int counter = size - 2; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            style = ((DefaultSynthStyle)styles.get(counter)).addTo(style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}