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