2505 lines · 112.3 KB · Java
RawDownload
/* * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.plaf.metal; import java.awt.*;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.plaf.*;import javax.swing.*;import javax.swing.plaf.basic.*;import javax.swing.text.DefaultEditorKit; import java.awt.Color;import java.awt.event.KeyEvent;import java.lang.reflect.*;import java.lang.ref.ReferenceQueue;import java.lang.ref.WeakReference; import java.security.AccessController;import java.security.PrivilegedAction; import sun.awt.*;import sun.security.action.GetPropertyAction;import sun.swing.DefaultLayoutStyle;import sun.swing.SwingLazyValue;import sun.swing.SwingUtilities2; /** * The Java Look and Feel, otherwise known as Metal. * <p> * Each of the {@code ComponentUI}s provided by {@code * MetalLookAndFeel} derives its behavior from the defaults * table. Unless otherwise noted each of the {@code ComponentUI} * implementations in this package document the set of defaults they * use. Unless otherwise noted the defaults are installed at the time * {@code installUI} is invoked, and follow the recommendations * outlined in {@code LookAndFeel} for installing defaults. * <p> * {@code MetalLookAndFeel} derives it's color palette and fonts from * {@code MetalTheme}. The default theme is {@code OceanTheme}. The theme * can be changed using the {@code setCurrentTheme} method, refer to it * for details on changing the theme. Prior to 1.5 the default * theme was {@code DefaultMetalTheme}. The system property * {@code "swing.metalTheme"} can be set to {@code "steel"} to indicate * the default should be {@code DefaultMetalTheme}. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing.  As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see MetalTheme * @see DefaultMetalTheme * @see OceanTheme * * @author Steve Wilson */public class MetalLookAndFeel extends BasicLookAndFeel{     private static boolean METAL_LOOK_AND_FEEL_INITED = false;      /**     * True if checked for windows yet.     */    private static boolean checkedWindows;    /**     * True if running on Windows.     */    private static boolean isWindows;     /**     * Set to true first time we've checked swing.useSystemFontSettings.     */    private static boolean checkedSystemFontSettings;     /**     * True indicates we should use system fonts, unless the developer has     * specified otherwise with Application.useSystemFontSettings.     */    private static boolean useSystemFonts;      /**     * Returns true if running on Windows.     */    static boolean isWindows() {        if (!checkedWindows) {            OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());            if (osType == OSInfo.OSType.WINDOWS) {                isWindows = true;                String systemFonts = AccessController.doPrivileged(                    new GetPropertyAction("swing.useSystemFontSettings"));                useSystemFonts = (systemFonts != null &&                               (Boolean.valueOf(systemFonts).booleanValue()));            }            checkedWindows = true;        }        return isWindows;    }     /**     * Returns true if system fonts should be used, this is only useful     * for windows.     */    static boolean useSystemFonts() {        if (isWindows() && useSystemFonts) {            if (METAL_LOOK_AND_FEEL_INITED) {                Object value = UIManager.get(                                 "Application.useSystemFontSettings");                 return (value == null || Boolean.TRUE.equals(value));            }            // If an instanceof MetalLookAndFeel hasn't been inited yet, we            // don't want to trigger loading of a UI by asking the UIManager            // for a property, assume the user wants system fonts. This will            // be properly adjusted when install is invoked on the            // MetalTheme            return true;        }        return false;    }     /**     * Returns true if the high contrast theme should be used as the default     * theme.     */    private static boolean useHighContrastTheme() {        if (isWindows() && useSystemFonts()) {            Boolean highContrast = (Boolean)Toolkit.getDefaultToolkit().                                  getDesktopProperty("win.highContrast.on");             return (highContrast == null) ? false : highContrast.                                            booleanValue();        }        return false;    }     /**     * Returns true if we're using the Ocean Theme.     */    static boolean usingOcean() {        return (getCurrentTheme() instanceof OceanTheme);    }     /**     * Returns the name of this look and feel. This returns     * {@code "Metal"}.     *     * @return the name of this look and feel     */    public String getName() {        return "Metal";    }     /**     * Returns an identifier for this look and feel. This returns     * {@code "Metal"}.     *     * @return the identifier of this look and feel     */    public String getID() {        return "Metal";    }     /**     * Returns a short description of this look and feel. This returns     * {@code "The Java(tm) Look and Feel"}.      * @return a short description for the look and feel     */    public String getDescription() {        return "The Java(tm) Look and Feel";    }     /**     * Returns {@code false}; {@code MetalLookAndFeel} is not a native     * look and feel.     *     * @return {@code false}     */    public boolean isNativeLookAndFeel() {        return false;    }     /**     * Returns {@code true}; {@code MetalLookAndFeel} can be run on     * any platform.     *     * @return {@code true}     */    public boolean isSupportedLookAndFeel() {        return true;    }     /**     * Returns {@code true}; metal can provide {@code Window}     * decorations.     *     * @return {@code true}     *     * @see JDialog#setDefaultLookAndFeelDecorated     * @see JFrame#setDefaultLookAndFeelDecorated     * @see JRootPane#setWindowDecorationStyle     * @since 1.4     */    public boolean getSupportsWindowDecorations() {        return true;    }     /**     * Populates {@code table} with mappings from {@code uiClassID} to     * the fully qualified name of the ui class. {@code     * MetalLookAndFeel} registers an entry for each of the classes in     * the package {@code javax.swing.plaf.metal} that are named     * MetalXXXUI. The string {@code XXX} is one of Swing's uiClassIDs. For     * the {@code uiClassIDs} that do not have a class in metal, the     * corresponding class in {@code javax.swing.plaf.basic} is     * used. For example, metal does not have a class named {@code     * "MetalColorChooserUI"}, as such, {@code     * javax.swing.plaf.basic.BasicColorChooserUI} is used.     *     * @param table the {@code UIDefaults} instance the entries are     *        added to     * @throws NullPointerException if {@code table} is {@code null}     *     * @see javax.swing.plaf.basic.BasicLookAndFeel#initClassDefaults     */    protected void initClassDefaults(UIDefaults table)    {        super.initClassDefaults(table);        final String metalPackageName = "javax.swing.plaf.metal.";         Object[] uiDefaults = {                   "ButtonUI", metalPackageName + "MetalButtonUI",                 "CheckBoxUI", metalPackageName + "MetalCheckBoxUI",                 "ComboBoxUI", metalPackageName + "MetalComboBoxUI",              "DesktopIconUI", metalPackageName + "MetalDesktopIconUI",              "FileChooserUI", metalPackageName + "MetalFileChooserUI",            "InternalFrameUI", metalPackageName + "MetalInternalFrameUI",                    "LabelUI", metalPackageName + "MetalLabelUI",       "PopupMenuSeparatorUI", metalPackageName + "MetalPopupMenuSeparatorUI",              "ProgressBarUI", metalPackageName + "MetalProgressBarUI",              "RadioButtonUI", metalPackageName + "MetalRadioButtonUI",                "ScrollBarUI", metalPackageName + "MetalScrollBarUI",               "ScrollPaneUI", metalPackageName + "MetalScrollPaneUI",                "SeparatorUI", metalPackageName + "MetalSeparatorUI",                   "SliderUI", metalPackageName + "MetalSliderUI",                "SplitPaneUI", metalPackageName + "MetalSplitPaneUI",               "TabbedPaneUI", metalPackageName + "MetalTabbedPaneUI",                "TextFieldUI", metalPackageName + "MetalTextFieldUI",             "ToggleButtonUI", metalPackageName + "MetalToggleButtonUI",                  "ToolBarUI", metalPackageName + "MetalToolBarUI",                  "ToolTipUI", metalPackageName + "MetalToolTipUI",                     "TreeUI", metalPackageName + "MetalTreeUI",                 "RootPaneUI", metalPackageName + "MetalRootPaneUI",        };         table.putDefaults(uiDefaults);    }     /**     * Populates {@code table} with system colors. The following values are     * added to {@code table}:     * <table border="1" cellpadding="1" cellspacing="0"     *         summary="Metal's system color mapping"     *         valign="top" >     *  <tr valign="top"  align="left">     *    <th bgcolor="#CCCCFF" align="left">Key     *    <th bgcolor="#CCCCFF" align="left">Value     *  <tr valign="top"  align="left">     *    <td>"desktop"     *    <td>{@code theme.getDesktopColor()}     *  <tr valign="top"  align="left">     *    <td>"activeCaption"     *    <td>{@code theme.getWindowTitleBackground()}     *  <tr valign="top"  align="left">     *    <td>"activeCaptionText"     *    <td>{@code theme.getWindowTitleForeground()}     *  <tr valign="top"  align="left">     *    <td>"activeCaptionBorder"     *    <td>{@code theme.getPrimaryControlShadow()}     *  <tr valign="top"  align="left">     *    <td>"inactiveCaption"     *    <td>{@code theme.getWindowTitleInactiveBackground()}     *  <tr valign="top"  align="left">     *    <td>"inactiveCaptionText"     *    <td>{@code theme.getWindowTitleInactiveForeground()}     *  <tr valign="top"  align="left">     *    <td>"inactiveCaptionBorder"     *    <td>{@code theme.getControlShadow()}     *  <tr valign="top"  align="left">     *    <td>"window"     *    <td>{@code theme.getWindowBackground()}     *  <tr valign="top"  align="left">     *    <td>"windowBorder"     *    <td>{@code theme.getControl()}     *  <tr valign="top"  align="left">     *    <td>"windowText"     *    <td>{@code theme.getUserTextColor()}     *  <tr valign="top"  align="left">     *    <td>"menu"     *    <td>{@code theme.getMenuBackground()}     *  <tr valign="top"  align="left">     *    <td>"menuText"     *    <td>{@code theme.getMenuForeground()}     *  <tr valign="top"  align="left">     *    <td>"text"     *    <td>{@code theme.getWindowBackground()}     *  <tr valign="top"  align="left">     *    <td>"textText"     *    <td>{@code theme.getUserTextColor()}     *  <tr valign="top"  align="left">     *    <td>"textHighlight"     *    <td>{@code theme.getTextHighlightColor()}     *  <tr valign="top"  align="left">     *    <td>"textHighlightText"     *    <td>{@code theme.getHighlightedTextColor()}     *  <tr valign="top"  align="left">     *    <td>"textInactiveText"     *    <td>{@code theme.getInactiveSystemTextColor()}     *  <tr valign="top"  align="left">     *    <td>"control"     *    <td>{@code theme.getControl()}     *  <tr valign="top"  align="left">     *    <td>"controlText"     *    <td>{@code theme.getControlTextColor()}     *  <tr valign="top"  align="left">     *    <td>"controlHighlight"     *    <td>{@code theme.getControlHighlight()}     *  <tr valign="top"  align="left">     *    <td>"controlLtHighlight"     *    <td>{@code theme.getControlHighlight()}     *  <tr valign="top"  align="left">     *    <td>"controlShadow"     *    <td>{@code theme.getControlShadow()}     *  <tr valign="top"  align="left">     *    <td>"controlDkShadow"     *    <td>{@code theme.getControlDarkShadow()}     *  <tr valign="top"  align="left">     *    <td>"scrollbar"     *    <td>{@code theme.getControl()}     *  <tr valign="top"  align="left">     *    <td>"info"     *    <td>{@code theme.getPrimaryControl()}     *  <tr valign="top"  align="left">     *    <td>"infoText"     *    <td>{@code theme.getPrimaryControlInfo()}     * </table>     * The value {@code theme} corresponds to the current {@code MetalTheme}.     *     * @param table the {@code UIDefaults} object the values are added to     * @throws NullPointerException if {@code table} is {@code null}     */    protected void initSystemColorDefaults(UIDefaults table)    {        MetalTheme theme = getCurrentTheme();        Color control = theme.getControl();        Object[] systemColors = {                "desktop", theme.getDesktopColor(), /* Color of the desktop background */          "activeCaption", theme.getWindowTitleBackground(), /* Color for captions (title bars) when they are active. */      "activeCaptionText", theme.getWindowTitleForeground(), /* Text color for text in captions (title bars). */    "activeCaptionBorder", theme.getPrimaryControlShadow(), /* Border color for caption (title bar) window borders. */        "inactiveCaption", theme.getWindowTitleInactiveBackground(), /* Color for captions (title bars) when not active. */    "inactiveCaptionText", theme.getWindowTitleInactiveForeground(), /* Text color for text in inactive captions (title bars). */  "inactiveCaptionBorder", theme.getControlShadow(), /* Border color for inactive caption (title bar) window borders. */                 "window", theme.getWindowBackground(), /* Default color for the interior of windows */           "windowBorder", control, /* ??? */             "windowText", theme.getUserTextColor(), /* ??? */                   "menu", theme.getMenuBackground(), /* Background color for menus */               "menuText", theme.getMenuForeground(), /* Text color for menus  */                   "text", theme.getWindowBackground(), /* Text background color */               "textText", theme.getUserTextColor(), /* Text foreground color */          "textHighlight", theme.getTextHighlightColor(), /* Text background color when selected */      "textHighlightText", theme.getHighlightedTextColor(), /* Text color when selected */       "textInactiveText", theme.getInactiveSystemTextColor(), /* Text color when disabled */                "control", control, /* Default color for controls (buttons, sliders, etc) */            "controlText", theme.getControlTextColor(), /* Default color for text in controls */       "controlHighlight", theme.getControlHighlight(), /* Specular highlight (opposite of the shadow) */     "controlLtHighlight", theme.getControlHighlight(), /* Highlight color for controls */          "controlShadow", theme.getControlShadow(), /* Shadow color for controls */        "controlDkShadow", theme.getControlDarkShadow(), /* Dark shadow color for controls */              "scrollbar", control, /* Scrollbar background (usually the "track") */                   "info", theme.getPrimaryControl(), /* ToolTip Background */               "infoText", theme.getPrimaryControlInfo()  /* ToolTip Text */        };         table.putDefaults(systemColors);    }     /**     * Initialize the defaults table with the name of the ResourceBundle     * used for getting localized defaults.     */    private void initResourceBundle(UIDefaults table) {        table.addResourceBundle( "com.sun.swing.internal.plaf.metal.resources.metal" );    }     /**     * Populates {@code table} with the defaults for metal.     *     * @param table the {@code UIDefaults} to add the values to     * @throws NullPointerException if {@code table} is {@code null}     */    protected void initComponentDefaults(UIDefaults table) {        super.initComponentDefaults( table );         initResourceBundle(table);         Color acceleratorForeground = getAcceleratorForeground();        Color acceleratorSelectedForeground = getAcceleratorSelectedForeground();        Color control = getControl();        Color controlHighlight = getControlHighlight();        Color controlShadow = getControlShadow();        Color controlDarkShadow = getControlDarkShadow();        Color controlTextColor = getControlTextColor();        Color focusColor = getFocusColor();        Color inactiveControlTextColor = getInactiveControlTextColor();        Color menuBackground = getMenuBackground();        Color menuSelectedBackground = getMenuSelectedBackground();        Color menuDisabledForeground = getMenuDisabledForeground();        Color menuSelectedForeground = getMenuSelectedForeground();        Color primaryControl = getPrimaryControl();        Color primaryControlDarkShadow = getPrimaryControlDarkShadow();        Color primaryControlShadow = getPrimaryControlShadow();        Color systemTextColor = getSystemTextColor();         Insets zeroInsets = new InsetsUIResource(0, 0, 0, 0);         Integer zero = Integer.valueOf(0);         Object textFieldBorder =            new SwingLazyValue("javax.swing.plaf.metal.MetalBorders",                                          "getTextFieldBorder");         Object dialogBorder = new MetalLazyValue(                          "javax.swing.plaf.metal.MetalBorders$DialogBorder");         Object questionDialogBorder = new MetalLazyValue(                  "javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");         Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[] {                           "ctrl C", DefaultEditorKit.copyAction,                           "ctrl V", DefaultEditorKit.pasteAction,                           "ctrl X", DefaultEditorKit.cutAction,                             "COPY", DefaultEditorKit.copyAction,                            "PASTE", DefaultEditorKit.pasteAction,                              "CUT", DefaultEditorKit.cutAction,                   "control INSERT", DefaultEditorKit.copyAction,                     "shift INSERT", DefaultEditorKit.pasteAction,                     "shift DELETE", DefaultEditorKit.cutAction,                       "shift LEFT", DefaultEditorKit.selectionBackwardAction,                    "shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,                      "shift RIGHT", DefaultEditorKit.selectionForwardAction,                   "shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,                        "ctrl LEFT", DefaultEditorKit.previousWordAction,                     "ctrl KP_LEFT", DefaultEditorKit.previousWordAction,                       "ctrl RIGHT", DefaultEditorKit.nextWordAction,                    "ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,                  "ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,               "ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,                 "ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,              "ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,                           "ctrl A", DefaultEditorKit.selectAllAction,                             "HOME", DefaultEditorKit.beginLineAction,                              "END", DefaultEditorKit.endLineAction,                       "shift HOME", DefaultEditorKit.selectionBeginLineAction,                        "shift END", DefaultEditorKit.selectionEndLineAction,                       "BACK_SPACE", DefaultEditorKit.deletePrevCharAction,                 "shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,                           "ctrl H", DefaultEditorKit.deletePrevCharAction,                           "DELETE", DefaultEditorKit.deleteNextCharAction,

Lines 1–500 of 2505.