jdk/src/share/classes/javax/swing/plaf/nimbus/Defaults.template
author peterz
Mon, 31 Aug 2009 13:46:24 +0400
changeset 3741 4021567cd4ca
parent 3093 3ae581eddd21
child 3932 c9cd7ff79037
permissions -rw-r--r--
6802944: Nimbus initialization is too slow Reviewed-by: jasper
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     1
/*
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     4
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    10
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    15
 * accompanied this code).
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    16
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    20
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    23
 * have any questions.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    24
 */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    25
package ${PACKAGE};
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    26
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    27
import javax.swing.Painter;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    28
import java.awt.Graphics;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    29
import sun.font.FontManager;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    30
import sun.swing.plaf.synth.DefaultSynthStyle;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    31
import javax.swing.BorderFactory;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    32
import javax.swing.JComponent;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    33
import javax.swing.JInternalFrame;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    34
import javax.swing.UIDefaults;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    35
import javax.swing.UIManager;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    36
import javax.swing.plaf.BorderUIResource;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    37
import javax.swing.plaf.ColorUIResource;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    38
import javax.swing.plaf.DimensionUIResource;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    39
import javax.swing.plaf.FontUIResource;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    40
import javax.swing.plaf.InsetsUIResource;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    41
import javax.swing.plaf.synth.Region;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    42
import javax.swing.plaf.synth.SynthStyle;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    43
import java.awt.Color;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    44
import java.awt.Component;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    45
import java.awt.Dimension;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    46
import java.awt.Font;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    47
import java.awt.Graphics2D;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    48
import java.awt.Insets;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    49
import java.awt.image.BufferedImage;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    50
import static java.awt.image.BufferedImage.*;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    51
import java.beans.PropertyChangeEvent;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    52
import java.beans.PropertyChangeListener;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    53
import java.lang.ref.WeakReference;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    54
import java.lang.reflect.Constructor;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    55
import java.util.ArrayList;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    56
import java.util.HashMap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    57
import java.util.LinkedList;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    58
import java.util.List;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    59
import java.util.Map;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    60
import java.util.Set;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    61
import java.util.WeakHashMap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    62
import javax.swing.border.Border;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    63
import javax.swing.plaf.UIResource;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    64
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    65
/**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    66
 * This class contains all the implementation details related to
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    67
 * ${LAF_NAME}. It contains all the code for initializing the UIDefaults table,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    68
 * as well as for selecting
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    69
 * a SynthStyle based on a JComponent/Region pair.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    70
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    71
 * @author Richard Bair
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    72
 */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    73
final class ${LAF_NAME}Defaults {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    74
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    75
     * The map of SynthStyles. This map is keyed by Region. Each Region maps
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    76
     * to a List of LazyStyles. Each LazyStyle has a reference to the prefix
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    77
     * that was registered with it. This reference can then be inspected to see
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    78
     * if it is the proper lazy style.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    79
     * <p/>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    80
     * There can be more than one LazyStyle for a single Region if there is more
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    81
     * than one prefix defined for a given region. For example, both Button and
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    82
     * "MyButton" might be prefixes assigned to the Region.Button region.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    83
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    84
    private Map<Region, List<LazyStyle>> m;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    85
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    86
     * A map of regions which have been registered.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    87
     * This mapping is maintained so that the Region can be found based on
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    88
     * prefix in a very fast manner. This is used in the "matches" method of
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    89
     * LazyStyle.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    90
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    91
    private Map<String, Region> registeredRegions =
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    92
            new HashMap<String, Region>();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    93
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    94
     * Our fallback style to avoid NPEs if the proper style cannot be found in
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    95
     * this class. Not sure if relying on DefaultSynthStyle is the best choice.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    96
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    97
    private DefaultSynthStyle defaultStyle;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    98
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    99
     * The default font that will be used. I store this value so that it can be
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   100
     * set in the UIDefaults when requested.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   101
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   102
    private FontUIResource defaultFont;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   103
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   104
    private ColorTree colorTree = new ColorTree();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   105
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   106
    /** Listener for changes to user defaults table */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   107
    private DefaultsListener defaultsListener = new DefaultsListener();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   108
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   109
    /** Called by UIManager when this look and feel is installed. */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   110
    void initialize() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   111
        // add listener for derived colors
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   112
        UIManager.addPropertyChangeListener(defaultsListener);
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   113
        UIManager.getDefaults().addPropertyChangeListener(colorTree);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   114
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   115
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   116
    /** Called by UIManager when this look and feel is uninstalled. */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   117
    void uninitialize() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   118
        // remove listener for derived colors
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   119
        UIManager.removePropertyChangeListener(defaultsListener);
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   120
        UIManager.getDefaults().removePropertyChangeListener(colorTree);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   121
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   122
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   123
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   124
     * Create a new ${LAF_NAME}Defaults. This constructor is only called from
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   125
     * within ${LAF_NAME}LookAndFeel.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   126
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   127
    ${LAF_NAME}Defaults() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   128
        m = new HashMap<Region, List<LazyStyle>>();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   129
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   130
        //Create the default font and default style. Also register all of the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   131
        //regions and their states that this class will use for later lookup.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   132
        //Additional regions can be registered later by 3rd party components.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   133
        //These are simply the default registrations.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   134
        defaultFont = FontManager.getFontConfigFUIR("sans", Font.PLAIN, 12);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   135
        defaultStyle = new DefaultSynthStyle();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   136
        defaultStyle.setFont(defaultFont);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   137
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   138
        //initialize the map of styles
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   139
${STYLE_INIT}
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   140
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   141
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   142
    //--------------- Methods called by ${LAF_NAME}LookAndFeel
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   143
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   144
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   145
     * Called from ${LAF_NAME}LookAndFeel to initialize the UIDefaults.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   146
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   147
     * @param d UIDefaults table to initialize. This will never be null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   148
     *          If listeners are attached to <code>d</code>, then you will
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   149
     *          only receive notification of LookAndFeel level defaults, not
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   150
     *          all defaults on the UIManager.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   151
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   152
    void initializeDefaults(UIDefaults d) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   153
${UI_DEFAULT_INIT}
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   154
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   155
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   156
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   157
     * <p>Registers the given region and prefix. The prefix, if it contains
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   158
     * quoted sections, refers to certain named components. If there are not
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   159
     * quoted sections, then the prefix refers to a generic component type.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   160
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   161
     * <p>If the given region/prefix combo has already been registered, then
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   162
     * it will not be registered twice. The second registration attempt will
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   163
     * fail silently.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   164
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   165
     * @param region The Synth Region that is being registered. Such as Button,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   166
     *        or ScrollBarThumb.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   167
     * @param prefix The UIDefault prefix. For example, could be ComboBox, or if
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   168
     *        a named components, "MyComboBox", or even something like
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   169
     *        ToolBar:"MyComboBox":"ComboBox.arrowButton"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   170
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   171
    void register(Region region, String prefix) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   172
        //validate the method arguments
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   173
        if (region == null || prefix == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   174
            throw new IllegalArgumentException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   175
                    "Neither Region nor Prefix may be null");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   176
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   177
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   178
        //Add a LazyStyle for this region/prefix to m.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   179
        List<LazyStyle> styles = m.get(region);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   180
        if (styles == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   181
            styles = new LinkedList<LazyStyle>();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   182
            styles.add(new LazyStyle(prefix));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   183
            m.put(region, styles);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   184
        } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   185
            //iterate over all the current styles and see if this prefix has
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   186
            //already been registered. If not, then register it.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   187
            for (LazyStyle s : styles) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   188
                if (prefix.equals(s.prefix)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   189
                    return;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   190
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   191
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   192
            styles.add(new LazyStyle(prefix));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   193
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   194
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   195
        //add this region to the map of registered regions
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   196
        registeredRegions.put(region.getName(), region);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   197
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   198
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   199
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   200
     * <p>Locate the style associated with the given region, and component.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   201
     * This is called from ${LAF_NAME}LookAndFeel in the SynthStyleFactory
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   202
     * implementation.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   203
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   204
     * <p>Lookup occurs as follows:<br/>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   205
     * Check the map of styles <code>m</code>. If the map contains no styles at
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   206
     * all, then simply return the defaultStyle. If the map contains styles,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   207
     * then iterate over all of the styles for the Region <code>r</code> looking
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   208
     * for the best match, based on prefix. If a match was made, then return
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   209
     * that SynthStyle. Otherwise, return the defaultStyle.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   210
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   211
     * @param comp The component associated with this region. For example, if
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   212
     *        the Region is Region.Button then the component will be a JButton.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   213
     *        If the Region is a subregion, such as ScrollBarThumb, then the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   214
     *        associated component will be the component that subregion belongs
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   215
     *        to, such as JScrollBar. The JComponent may be named. It may not be
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   216
     *        null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   217
     * @param r The region we are looking for a style for. May not be null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   218
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   219
    SynthStyle getStyle(JComponent comp, Region r) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   220
        //validate method arguments
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   221
        if (comp == null || r == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   222
            throw new IllegalArgumentException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   223
                    "Neither comp nor r may be null");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   224
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   225
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   226
        //if there are no lazy styles registered for the region r, then return
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   227
        //the default style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   228
        List<LazyStyle> styles = m.get(r);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   229
        if (styles == null || styles.size() == 0) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   230
            return defaultStyle;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   231
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   232
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   233
        //Look for the best SynthStyle for this component/region pair.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   234
        LazyStyle foundStyle = null;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   235
        for (LazyStyle s : styles) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   236
            if (s.matches(comp)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   237
                //replace the foundStyle if foundStyle is null, or
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   238
                //if the new style "s" is more specific (ie, its path was
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   239
                //longer), or if the foundStyle was "simple" and the new style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   240
                //was not (ie: the foundStyle was for something like Button and
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   241
                //the new style was for something like "MyButton", hence, being
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   242
                //more specific.) In all cases, favor the most specific style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   243
                //found.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   244
                if (foundStyle == null ||
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   245
                   (foundStyle.parts.length < s.parts.length) ||
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   246
                   (foundStyle.parts.length == s.parts.length 
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   247
                    && foundStyle.simple && !s.simple)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   248
                    foundStyle = s;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   249
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   250
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   251
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   252
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   253
        //return the style, if found, or the default style if not found
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   254
        return foundStyle == null ? defaultStyle : foundStyle.getStyle(comp);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   255
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   256
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   257
    /*
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   258
        Various public helper classes.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   259
        These may be used to register 3rd party values into UIDefaults
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   260
    */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   261
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   262
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   263
     * <p>Derives its font value based on a parent font and a set of offsets and
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   264
     * attributes. This class is an ActiveValue, meaning that it will recompute
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   265
     * its value each time it is requested from UIDefaults. It is therefore
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   266
     * recommended to read this value once and cache it in the UI delegate class
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   267
     * until asked to reinitialize.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   268
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   269
     * <p>To use this class, create an instance with the key of the font in the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   270
     * UI defaults table from which to derive this font, along with a size
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   271
     * offset (if any), and whether it is to be bold, italic, or left in its
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   272
     * default form.</p>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   273
     */
3093
3ae581eddd21 6849277: Nimbus L&F: lots of painter classes were added to JDK7 as public
peterz
parents: 2658
diff changeset
   274
    static final class DerivedFont implements UIDefaults.ActiveValue {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   275
        private float sizeOffset;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   276
        private Boolean bold;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   277
        private Boolean italic;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   278
        private String parentKey;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   279
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   280
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   281
         * Create a new DerivedFont.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   282
         *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   283
         * @param key The UIDefault key associated with this derived font's
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   284
         *            parent or source. If this key leads to a null value, or a
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   285
         *            value that is not a font, then null will be returned as
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   286
         *            the derived font. The key must not be null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   287
         * @param sizeOffset The size offset, as a percentage, to use. For
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   288
         *                   example, if the source font was a 12pt font and the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   289
         *                   sizeOffset were specified as .9, then the new font
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   290
         *                   will be 90% of what the source font was, or, 10.8
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   291
         *                   pts which is rounded to 11pts. This fractional
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   292
         *                   based offset allows for proper font scaling in high
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   293
         *                   DPI or large system font scenarios.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   294
         * @param bold Whether the new font should be bold. If null, then this
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   295
         *             new font will inherit the bold setting of the source
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   296
         *             font.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   297
         * @param italic Whether the new font should be italicized. If null,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   298
         *               then this new font will inherit the italic setting of
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   299
         *               the source font.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   300
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   301
        public DerivedFont(String key, float sizeOffset, Boolean bold,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   302
                           Boolean italic) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   303
            //validate the constructor arguments
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   304
            if (key == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   305
                throw new IllegalArgumentException("You must specify a key");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   306
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   307
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   308
            //set the values
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   309
            this.parentKey = key;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   310
            this.sizeOffset = sizeOffset;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   311
            this.bold = bold;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   312
            this.italic = italic;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   313
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   314
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   315
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   316
         * @inheritDoc
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   317
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   318
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   319
        public Object createValue(UIDefaults defaults) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   320
            Font f = defaults.getFont(parentKey);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   321
            if (f != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   322
                // always round size for now so we have exact int font size
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   323
                // (or we may have lame looking fonts)
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   324
                float size = Math.round(f.getSize2D() * sizeOffset);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   325
                int style = f.getStyle();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   326
                if (bold != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   327
                    if (bold.booleanValue()) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   328
                        style = style | Font.BOLD;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   329
                    } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   330
                        style = style & ~Font.BOLD;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   331
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   332
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   333
                if (italic != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   334
                    if (italic.booleanValue()) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   335
                        style = style | Font.ITALIC;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   336
                    } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   337
                        style = style & ~Font.ITALIC;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   338
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   339
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   340
                return f.deriveFont(style, size);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   341
            } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   342
                return null;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   343
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   344
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   345
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   346
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   347
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   348
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   349
     * This class is private because it relies on the constructor of the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   350
     * auto-generated AbstractRegionPainter subclasses. Hence, it is not
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   351
     * generally useful, and is private.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   352
     * <p/>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   353
     * LazyPainter is a LazyValue class. It will create the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   354
     * AbstractRegionPainter lazily, when asked. It uses reflection to load the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   355
     * proper class and invoke its constructor.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   356
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   357
    private static final class LazyPainter implements UIDefaults.LazyValue {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   358
        private int which;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   359
        private AbstractRegionPainter.PaintContext ctx;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   360
        private String className;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   361
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   362
        LazyPainter(String className, int which, Insets insets,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   363
                    Dimension canvasSize, boolean inverted) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   364
            if (className == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   365
                throw new IllegalArgumentException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   366
                        "The className must be specified");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   367
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   368
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   369
            this.className = className;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   370
            this.which = which;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   371
            this.ctx = new AbstractRegionPainter.PaintContext(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   372
                insets, canvasSize, inverted);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   373
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   374
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   375
        LazyPainter(String className, int which, Insets insets,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   376
                    Dimension canvasSize, boolean inverted,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   377
                    AbstractRegionPainter.PaintContext.CacheMode cacheMode,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   378
                    double maxH, double maxV) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   379
            if (className == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   380
                throw new IllegalArgumentException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   381
                        "The className must be specified");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   382
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   383
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   384
            this.className = className;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   385
            this.which = which;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   386
            this.ctx = new AbstractRegionPainter.PaintContext(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   387
                    insets, canvasSize, inverted, cacheMode, maxH, maxV);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   388
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   389
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   390
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   391
        public Object createValue(UIDefaults table) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   392
            try {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   393
                Class c;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   394
                Object cl;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   395
                // See if we should use a separate ClassLoader
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   396
                if (table == null || !((cl = table.get("ClassLoader"))
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   397
                                       instanceof ClassLoader)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   398
                    cl = Thread.currentThread().
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   399
                                getContextClassLoader();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   400
                    if (cl == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   401
                        // Fallback to the system class loader.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   402
                        cl = ClassLoader.getSystemClassLoader();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   403
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   404
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   405
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   406
                c = Class.forName(className, true, (ClassLoader)cl);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   407
                Constructor constructor = c.getConstructor(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   408
                        AbstractRegionPainter.PaintContext.class, int.class);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   409
                if (constructor == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   410
                    throw new NullPointerException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   411
                            "Failed to find the constructor for the class: " +
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   412
                            className);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   413
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   414
                return constructor.newInstance(ctx, which);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   415
            } catch (Exception e) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   416
                e.printStackTrace();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   417
                return null;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   418
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   419
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   420
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   421
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   422
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   423
     * A class which creates the NimbusStyle associated with it lazily, but also
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   424
     * manages a lot more information about the style. It is less of a LazyValue
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   425
     * type of class, and more of an Entry or Item type of class, as it
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   426
     * represents an entry in the list of LazyStyles in the map m.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   427
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   428
     * The primary responsibilities of this class include:
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   429
     * <ul>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   430
     *   <li>Determining whether a given component/region pair matches this
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   431
     *       style</li>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   432
     *   <li>Splitting the prefix specified in the constructor into its
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   433
     *       constituent parts to facilitate quicker matching</li>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   434
     *   <li>Creating and vending a NimbusStyle lazily.</li>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   435
     * </ul>
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   436
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   437
    private final class LazyStyle {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   438
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   439
         * The prefix this LazyStyle was registered with. Something like
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   440
         * Button or ComboBox:"ComboBox.arrowButton"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   441
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   442
        private String prefix;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   443
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   444
         * Whether or not this LazyStyle represents an unnamed component
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   445
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   446
        private boolean simple = true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   447
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   448
         * The various parts, or sections, of the prefix. For example,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   449
         * the prefix:
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   450
         *     ComboBox:"ComboBox.arrowButton"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   451
         *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   452
         * will be broken into two parts,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   453
         *     ComboBox and "ComboBox.arrowButton"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   454
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   455
        private Part[] parts;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   456
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   457
         * Cached shared style.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   458
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   459
        private NimbusStyle style;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   460
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   461
         * A weakly referenced hash map such that if the reference JComponent
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   462
         * key is garbage collected then the entry is removed from the map.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   463
         * This cache exists so that when a JComponent has nimbus overrides
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   464
         * in its client map, a unique style will be created and returned
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   465
         * for that JComponent instance, always. In such a situation each
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   466
         * JComponent instance must have its own instance of NimbusStyle.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   467
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   468
        private WeakHashMap<JComponent, WeakReference<NimbusStyle>> overridesCache;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   469
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   470
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   471
         * Create a new LazyStyle.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   472
         *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   473
         * @param prefix The prefix associated with this style. Cannot be null.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   474
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   475
        private LazyStyle(String prefix) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   476
            if (prefix == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   477
                throw new IllegalArgumentException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   478
                        "The prefix must not be null");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   479
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   480
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   481
            this.prefix = prefix;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   482
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   483
            //there is one odd case that needs to be supported here: cell
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   484
            //renderers. A cell renderer is defined as a named internal
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   485
            //component, so for example:
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   486
            // List."List.cellRenderer"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   487
            //The problem is that the component named List.cellRenderer is not a
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   488
            //child of a JList. Rather, it is treated more as a direct component
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   489
            //Thus, if the prefix ends with "cellRenderer", then remove all the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   490
            //previous dotted parts of the prefix name so that it becomes, for
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   491
            //example: "List.cellRenderer"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   492
            //Likewise, we have a hacked work around for cellRenderer, renderer,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   493
            //and listRenderer.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   494
            String temp = prefix;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   495
            if (temp.endsWith("cellRenderer\"")
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   496
                    || temp.endsWith("renderer\"")
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   497
                    || temp.endsWith("listRenderer\"")) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   498
                temp = temp.substring(temp.lastIndexOf(":\"") + 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   499
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   500
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   501
            //otherwise, normal code path
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   502
            List<String> sparts = split(temp);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   503
            parts = new Part[sparts.size()];
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   504
            for (int i = 0; i < parts.length; i++) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   505
                parts[i] = new Part(sparts.get(i));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   506
                if (parts[i].named) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   507
                    simple = false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   508
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   509
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   510
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   511
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   512
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   513
         * Gets the style. Creates it if necessary.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   514
         * @return the style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   515
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   516
        SynthStyle getStyle(JComponent c) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   517
            // if the component has overrides, it gets its own unique style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   518
            // instead of the shared style.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   519
            if (c.getClientProperty("Nimbus.Overrides") != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   520
                if (overridesCache == null)
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   521
                    overridesCache = new WeakHashMap<JComponent, WeakReference<NimbusStyle>>();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   522
                WeakReference<NimbusStyle> ref = overridesCache.get(c);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   523
                NimbusStyle s = ref == null ? null : ref.get();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   524
                if (s == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   525
                    s = new NimbusStyle(prefix, c);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   526
                    overridesCache.put(c, new WeakReference<NimbusStyle>(s));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   527
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   528
                return s;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   529
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   530
            
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   531
            // lazily create the style if necessary
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   532
            if (style == null)
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   533
                style = new NimbusStyle(prefix, null);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   534
            
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   535
            // return the style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   536
            return style;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   537
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   538
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   539
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   540
         * This LazyStyle is a match for the given component if, and only if,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   541
         * for each part of the prefix the component hierarchy matches exactly.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   542
         * That is, if given "a":something:"b", then:
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   543
         * c.getName() must equals "b"
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   544
         * c.getParent() can be anything
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   545
         * c.getParent().getParent().getName() must equal "a".
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   546
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   547
        boolean matches(JComponent c) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   548
            return matches(c, parts.length - 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   549
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   550
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   551
        private boolean matches(Component c, int partIndex) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   552
            if (partIndex < 0) return true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   553
            if (c == null) return false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   554
            //only get here if partIndex > 0 and c == null
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   555
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   556
            String name = c.getName();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   557
            if (parts[partIndex].named && parts[partIndex].s.equals(name)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   558
                //so far so good, recurse
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   559
                return matches(c.getParent(), partIndex - 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   560
            } else if (!parts[partIndex].named) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   561
                //if c is not named, and parts[partIndex] has an expected class
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   562
                //type registered, then check to make sure c is of the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   563
                //right type;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   564
                Class clazz = parts[partIndex].c;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   565
                if (clazz != null && clazz.isAssignableFrom(c.getClass())) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   566
                    //so far so good, recurse
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   567
                    return matches(c.getParent(), partIndex - 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   568
                } else if (clazz == null &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   569
                           registeredRegions.containsKey(parts[partIndex].s)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   570
                    Region r = registeredRegions.get(parts[partIndex].s);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   571
                    Component parent = r.isSubregion() ? c : c.getParent();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   572
                    //special case the JInternalFrameTitlePane, because it
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   573
                    //doesn't fit the mold. very, very funky.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   574
                    if (r == Region.INTERNAL_FRAME_TITLE_PANE && parent != null
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   575
                        && parent instanceof JInternalFrame.JDesktopIcon) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   576
                        JInternalFrame.JDesktopIcon icon =
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   577
                                (JInternalFrame.JDesktopIcon) parent;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   578
                        parent = icon.getInternalFrame();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   579
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   580
                    //it was the name of a region. So far, so good. Recurse.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   581
                    return matches(parent, partIndex - 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   582
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   583
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   584
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   585
            return false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   586
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   587
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   588
        /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   589
         * Given some dot separated prefix, split on the colons that are
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   590
         * not within quotes, and not within brackets.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   591
         *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   592
         * @param prefix
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   593
         * @return
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   594
         */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   595
        private List<String> split(String prefix) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   596
            List<String> parts = new ArrayList<String>();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   597
            int bracketCount = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   598
            boolean inquotes = false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   599
            int lastIndex = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   600
            for (int i = 0; i < prefix.length(); i++) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   601
                char c = prefix.charAt(i);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   602
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   603
                if (c == '[') {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   604
                    bracketCount++;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   605
                    continue;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   606
                } else if (c == '"') {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   607
                    inquotes = !inquotes;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   608
                    continue;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   609
                } else if (c == ']') {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   610
                    bracketCount--;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   611
                    if (bracketCount < 0) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   612
                        throw new RuntimeException(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   613
                                "Malformed prefix: " + prefix);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   614
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   615
                    continue;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   616
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   617
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   618
                if (c == ':' && !inquotes && bracketCount == 0) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   619
                    //found a character to split on.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   620
                    parts.add(prefix.substring(lastIndex, i));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   621
                    lastIndex = i + 1;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   622
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   623
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   624
            if (lastIndex < prefix.length() - 1 && !inquotes
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   625
                    && bracketCount == 0) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   626
                parts.add(prefix.substring(lastIndex));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   627
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   628
            return parts;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   629
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   630
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   631
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   632
        private final class Part {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   633
            private String s;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   634
            //true if this part represents a component name
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   635
            private boolean named;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   636
            private Class c;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   637
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   638
            Part(String s) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   639
                named = s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"';
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   640
                if (named) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   641
                    this.s = s.substring(1, s.length() - 1);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   642
                } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   643
                    this.s = s;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   644
                    //TODO use a map of known regions for Synth and Swing, and
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   645
                    //then use [classname] instead of org_class_name style
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   646
                    try {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   647
                        c = Class.forName("javax.swing.J" + s);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   648
                    } catch (Exception e) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   649
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   650
                    try {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   651
                        c = Class.forName(s.replace("_", "."));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   652
                    } catch (Exception e) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   653
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   654
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   655
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   656
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   657
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   658
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   659
    private void addColor(UIDefaults d, String uin, int r, int g, int b, int a) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   660
        Color color = new ColorUIResource(new Color(r, g, b, a));
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   661
        colorTree.addColor(uin, color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   662
        d.put(uin, color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   663
    }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   664
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   665
    private void addColor(UIDefaults d, String uin, String parentUin,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   666
            float hOffset, float sOffset, float bOffset, int aOffset) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   667
        addColor(d, uin, parentUin, hOffset, sOffset, bOffset, aOffset, true);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   668
    }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   669
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   670
    private void addColor(UIDefaults d, String uin, String parentUin,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   671
            float hOffset, float sOffset, float bOffset,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   672
            int aOffset, boolean uiResource) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   673
        Color color = getDerivedColor(uin, parentUin,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   674
                hOffset, sOffset, bOffset, aOffset, uiResource);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   675
        d.put(uin, color);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   676
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   677
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   678
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   679
     * Get a derived color, derived colors are shared instances and will be
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   680
     * updated when its parent UIDefault color changes.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   681
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   682
     * @param uiDefaultParentName The parent UIDefault key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   683
     * @param hOffset The hue offset
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   684
     * @param sOffset The saturation offset
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   685
     * @param bOffset The brightness offset
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   686
     * @param aOffset The alpha offset
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   687
     * @param uiResource True if the derived color should be a UIResource,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   688
     *        false if it should not be a UIResource
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   689
     * @return The stored derived color
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   690
     */
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   691
    public DerivedColor getDerivedColor(String parentUin,
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   692
                                        float hOffset, float sOffset,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   693
                                        float bOffset, int aOffset,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   694
                                        boolean uiResource){
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   695
        return getDerivedColor(null, parentUin,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   696
                hOffset, sOffset, bOffset, aOffset, uiResource);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   697
    }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   698
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   699
    private DerivedColor getDerivedColor(String uin, String parentUin,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   700
                                        float hOffset, float sOffset,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   701
                                        float bOffset, int aOffset,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   702
                                        boolean uiResource) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   703
        DerivedColor color;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   704
        if (uiResource) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   705
            color = new DerivedColor.UIResource(parentUin,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   706
                    hOffset, sOffset, bOffset, aOffset);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   707
        } else {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   708
            color = new DerivedColor(parentUin, hOffset, sOffset,
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   709
                bOffset, aOffset);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   710
        }
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   711
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   712
        if (derivedColors.containsKey(color)) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   713
            return derivedColors.get(color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   714
        } else {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   715
            derivedColors.put(color, color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   716
            color.rederiveColor(); /// move to ARP.decodeColor() ?
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   717
            colorTree.addColor(uin, color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   718
            return color;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   719
        }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   720
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   721
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   722
    private Map<DerivedColor, DerivedColor> derivedColors =
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   723
            new HashMap<DerivedColor, DerivedColor>();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   724
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   725
    private class ColorTree implements PropertyChangeListener {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   726
        private Node root = new Node(null, null);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   727
        private Map<String, Node> nodes = new HashMap<String, Node>();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   728
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   729
        public Color getColor(String uin) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   730
            return nodes.get(uin).color;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   731
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   732
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   733
        public void addColor(String uin, Color color) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   734
            Node parent = getParentNode(color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   735
            Node node = new Node(color, parent);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   736
            parent.children.add(node);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   737
            if (uin != null) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   738
                nodes.put(uin, node);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   739
            }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   740
        }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   742
        private Node getParentNode(Color color) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   743
            Node parent = root;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   744
            if (color instanceof DerivedColor) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   745
                String parentUin = ((DerivedColor)color).getUiDefaultParentName();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   746
                Node p = nodes.get(parentUin);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   747
                if (p != null) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   748
                    parent = p;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   749
                }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   750
            }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   751
            return parent;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   752
        }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   753
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   754
        public void update() {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   755
            root.update();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   756
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   757
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   758
        @Override
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   759
        public void propertyChange(PropertyChangeEvent ev) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   760
            String name = ev.getPropertyName();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   761
            Node node = nodes.get(name);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   762
            if (node != null) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   763
                // this is a registered color
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   764
                node.parent.children.remove(node);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   765
                Color color = (Color) ev.getNewValue();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   766
                Node parent = getParentNode(color);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   767
                node.set(color, parent);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   768
                parent.children.add(node);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   769
                node.update();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   770
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   771
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   772
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   773
        class Node {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   774
            Color color;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   775
            Node parent;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   776
            List<Node> children = new LinkedList<Node>();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   777
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   778
            Node(Color color, Node parent) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   779
                set(color, parent);
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   780
            }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   781
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   782
            public void set(Color color, Node parent) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   783
                this.color = color;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   784
                this.parent = parent;
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   785
            }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   786
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   787
            public void update() {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   788
                if (color instanceof DerivedColor) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   789
                    ((DerivedColor)color).rederiveColor();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   790
                }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   791
                for (Node child: children) {
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   792
                    child.update();
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   793
                }
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   794
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   795
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   796
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   797
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   798
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   799
     * Listener to update derived colors on UIManager Defaults changes
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   800
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   801
    private class DefaultsListener implements PropertyChangeListener {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   802
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   803
        public void propertyChange(PropertyChangeEvent evt) {
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   804
            if ("lookAndFeel".equals(evt.getPropertyName())) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   805
                // LAF has been installed, this is the first point at which we
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   806
                // can access our defaults table via UIManager so before now
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   807
                // all derived colors will be incorrect.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   808
                // First we need to update
3741
4021567cd4ca 6802944: Nimbus initialization is too slow
peterz
parents: 3093
diff changeset
   809
                colorTree.update();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   810
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   811
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   812
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   813
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   814
    private static final class PainterBorder implements Border, UIResource {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   815
        private Insets insets;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   816
        private Painter painter;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   817
        private String painterKey;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   818
        
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   819
        PainterBorder(String painterKey, Insets insets) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   820
            this.insets = insets;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   821
            this.painterKey = painterKey;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   822
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   823
        
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   824
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   825
        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   826
            if (painter == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   827
                painter = (Painter)UIManager.get(painterKey);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   828
                if (painter == null) return;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   829
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   830
            
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   831
            g.translate(x, y);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   832
            if (g instanceof Graphics2D)
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   833
                painter.paint((Graphics2D)g, c, w, h);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   834
            else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   835
                BufferedImage img = new BufferedImage(w, h, TYPE_INT_ARGB);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   836
                Graphics2D gfx = img.createGraphics();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   837
                painter.paint(gfx, c, w, h);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   838
                gfx.dispose();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   839
                g.drawImage(img, x, y, null);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   840
                img = null;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   841
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   842
            g.translate(-x, -y);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   843
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   844
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   845
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   846
        public Insets getBorderInsets(Component c) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   847
            return (Insets)insets.clone();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   848
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   849
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   850
        @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   851
        public boolean isBorderOpaque() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   852
            return false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   853
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   854
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   855
}