2313 lines · 106.6 KB · Java
RawDownload
/* * Copyright (c) 1997, 2010, 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.basic; import java.awt.Font;import java.awt.Color;import java.awt.SystemColor;import java.awt.event.*;import java.awt.Insets;import java.awt.Component;import java.awt.Container;import java.awt.FocusTraversalPolicy;import java.awt.AWTEvent;import java.awt.Toolkit;import java.awt.Point;import java.net.URL;import java.io.*;import java.awt.Dimension;import java.awt.KeyboardFocusManager;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.*;import java.lang.reflect.*;import javax.sound.sampled.*; import sun.awt.AppContext; import sun.swing.SwingLazyValue;import sun.swing.SwingUtilities2; import javax.swing.LookAndFeel;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.BorderFactory;import javax.swing.JComponent;import javax.swing.ImageIcon;import javax.swing.UIDefaults;import javax.swing.UIManager;import javax.swing.KeyStroke;import javax.swing.JTextField;import javax.swing.DefaultListCellRenderer;import javax.swing.FocusManager;import javax.swing.LayoutFocusTraversalPolicy;import javax.swing.SwingUtilities;import javax.swing.MenuSelectionManager;import javax.swing.MenuElement;import javax.swing.border.*;import javax.swing.plaf.*;import javax.swing.text.JTextComponent;import javax.swing.text.DefaultEditorKit;import javax.swing.JInternalFrame;import java.beans.PropertyVetoException;import java.awt.Window;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;  /** * A base class to use in creating a look and feel for Swing. * <p> * Each of the {@code ComponentUI}s provided by {@code * BasicLookAndFeel} 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> * <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}. * * @author unattributed */public abstract class BasicLookAndFeel extends LookAndFeel implements Serializable{    /**     * Whether or not the developer has created a JPopupMenu.     */    static boolean needsEventHelper;     /**     * Lock used when manipulating clipPlaying.     */    private transient Object audioLock = new Object();    /**     * The Clip that is currently playing (set in AudioAction).     */    private Clip clipPlaying;     AWTEventHelper invocator = null;     /*     * Listen for our AppContext being disposed     */    private PropertyChangeListener disposer = null;     /**     * Returns the look and feel defaults. The returned {@code UIDefaults}     * is populated by invoking, in order, {@code initClassDefaults},     * {@code initSystemColorDefaults} and {@code initComponentDefaults}.     * <p>     * While this method is public, it should only be invoked by the     * {@code UIManager} when the look and feel is set as the current     * look and feel and after {@code initialize} has been invoked.     *     * @return the look and feel defaults     *     * @see #initClassDefaults     * @see #initSystemColorDefaults     * @see #initComponentDefaults     */    public UIDefaults getDefaults() {        UIDefaults table = new UIDefaults(610, 0.75f);         initClassDefaults(table);        initSystemColorDefaults(table);        initComponentDefaults(table);         return table;    }     /**     * {@inheritDoc}     */    public void initialize() {        if (needsEventHelper) {            installAWTEventListener();        }    }     void installAWTEventListener() {        if (invocator == null) {            invocator = new AWTEventHelper();            needsEventHelper = true;             // Add a PropertyChangeListener to our AppContext so we're alerted            // when the AppContext is disposed(), at which time this laf should            // be uninitialize()d.            disposer = new PropertyChangeListener() {                public void propertyChange(PropertyChangeEvent prpChg) {                    uninitialize();                }            };            AppContext.getAppContext().addPropertyChangeListener(                                                        AppContext.GUI_DISPOSED,                                                        disposer);        }    }     /**     * {@inheritDoc}     */    public void uninitialize() {        AppContext context = AppContext.getAppContext();        synchronized (BasicPopupMenuUI.MOUSE_GRABBER_KEY) {            Object grabber = context.get(BasicPopupMenuUI.MOUSE_GRABBER_KEY);            if (grabber != null) {                ((BasicPopupMenuUI.MouseGrabber)grabber).uninstall();            }        }        synchronized (BasicPopupMenuUI.MENU_KEYBOARD_HELPER_KEY) {            Object helper =                    context.get(BasicPopupMenuUI.MENU_KEYBOARD_HELPER_KEY);            if (helper != null) {                ((BasicPopupMenuUI.MenuKeyboardHelper)helper).uninstall();            }        }         if(invocator != null) {            AccessController.doPrivileged(invocator);            invocator = null;        }         if (disposer != null) {            // Note that we're likely calling removePropertyChangeListener()            // during the course of AppContext.firePropertyChange().            // However, EventListenerAggreggate has code to safely modify            // the list under such circumstances.            context.removePropertyChangeListener(AppContext.GUI_DISPOSED,                                                 disposer);            disposer = null;        }    }     /**     * Populates {@code table} with mappings from {@code uiClassID} to the     * fully qualified name of the ui class. The value for a     * particular {@code uiClassID} is {@code     * "javax.swing.plaf.basic.Basic + uiClassID"}. For example, the     * value for the {@code uiClassID} {@code TreeUI} is {@code     * "javax.swing.plaf.basic.BasicTreeUI"}.     *     * @param table the {@code UIDefaults} instance the entries are     *        added to     * @throws NullPointerException if {@code table} is {@code null}     *     * @see javax.swing.LookAndFeel     * @see #getDefaults     */    protected void initClassDefaults(UIDefaults table)    {        final String basicPackageName = "javax.swing.plaf.basic.";        Object[] uiDefaults = {                   "ButtonUI", basicPackageName + "BasicButtonUI",                 "CheckBoxUI", basicPackageName + "BasicCheckBoxUI",             "ColorChooserUI", basicPackageName + "BasicColorChooserUI",       "FormattedTextFieldUI", basicPackageName + "BasicFormattedTextFieldUI",                  "MenuBarUI", basicPackageName + "BasicMenuBarUI",                     "MenuUI", basicPackageName + "BasicMenuUI",                 "MenuItemUI", basicPackageName + "BasicMenuItemUI",         "CheckBoxMenuItemUI", basicPackageName + "BasicCheckBoxMenuItemUI",      "RadioButtonMenuItemUI", basicPackageName + "BasicRadioButtonMenuItemUI",              "RadioButtonUI", basicPackageName + "BasicRadioButtonUI",             "ToggleButtonUI", basicPackageName + "BasicToggleButtonUI",                "PopupMenuUI", basicPackageName + "BasicPopupMenuUI",              "ProgressBarUI", basicPackageName + "BasicProgressBarUI",                "ScrollBarUI", basicPackageName + "BasicScrollBarUI",               "ScrollPaneUI", basicPackageName + "BasicScrollPaneUI",                "SplitPaneUI", basicPackageName + "BasicSplitPaneUI",                   "SliderUI", basicPackageName + "BasicSliderUI",                "SeparatorUI", basicPackageName + "BasicSeparatorUI",                  "SpinnerUI", basicPackageName + "BasicSpinnerUI",         "ToolBarSeparatorUI", basicPackageName + "BasicToolBarSeparatorUI",       "PopupMenuSeparatorUI", basicPackageName + "BasicPopupMenuSeparatorUI",               "TabbedPaneUI", basicPackageName + "BasicTabbedPaneUI",                 "TextAreaUI", basicPackageName + "BasicTextAreaUI",                "TextFieldUI", basicPackageName + "BasicTextFieldUI",            "PasswordFieldUI", basicPackageName + "BasicPasswordFieldUI",                 "TextPaneUI", basicPackageName + "BasicTextPaneUI",               "EditorPaneUI", basicPackageName + "BasicEditorPaneUI",                     "TreeUI", basicPackageName + "BasicTreeUI",                    "LabelUI", basicPackageName + "BasicLabelUI",                     "ListUI", basicPackageName + "BasicListUI",                  "ToolBarUI", basicPackageName + "BasicToolBarUI",                  "ToolTipUI", basicPackageName + "BasicToolTipUI",                 "ComboBoxUI", basicPackageName + "BasicComboBoxUI",                    "TableUI", basicPackageName + "BasicTableUI",              "TableHeaderUI", basicPackageName + "BasicTableHeaderUI",            "InternalFrameUI", basicPackageName + "BasicInternalFrameUI",              "DesktopPaneUI", basicPackageName + "BasicDesktopPaneUI",              "DesktopIconUI", basicPackageName + "BasicDesktopIconUI",              "FileChooserUI", basicPackageName + "BasicFileChooserUI",               "OptionPaneUI", basicPackageName + "BasicOptionPaneUI",                    "PanelUI", basicPackageName + "BasicPanelUI",                 "ViewportUI", basicPackageName + "BasicViewportUI",                 "RootPaneUI", basicPackageName + "BasicRootPaneUI",        };         table.putDefaults(uiDefaults);    }     /**     * Populates {@code table} with system colors. This creates an     * array of {@code name-color} pairs and invokes {@code     * loadSystemColors}.     * <p>     * The name is a {@code String} that corresponds to the name of     * one of the static {@code SystemColor} fields in the {@code     * SystemColor} class.  A name-color pair is created for every     * such {@code SystemColor} field.     * <p>     * The {@code color} corresponds to a hex {@code String} as     * understood by {@code Color.decode}. For example, one of the     * {@code name-color} pairs is {@code     * "desktop"-"#005C5C"}. This corresponds to the {@code     * SystemColor} field {@code desktop}, with a color value of     * {@code new Color(0x005C5C)}.     * <p>     * The following shows two of the {@code name-color} pairs:     * <pre>     *   String[] nameColorPairs = new String[] {     *          "desktop", "#005C5C",     *    "activeCaption", "#000080" };     *   loadSystemColors(table, nameColorPairs, isNativeLookAndFeel());     * </pre>     *     * As previously stated, this invokes {@code loadSystemColors}     * with the supplied {@code table} and {@code name-color} pair     * array. The last argument to {@code loadSystemColors} indicates     * whether the value of the field in {@code SystemColor} should be     * used. This method passes the value of {@code     * isNativeLookAndFeel()} as the last argument to {@code loadSystemColors}.     *     * @param table the {@code UIDefaults} object the values are added to     * @throws NullPointerException if {@code table} is {@code null}     *     * @see java.awt.SystemColor     * @see #getDefaults     * @see #loadSystemColors     */    protected void initSystemColorDefaults(UIDefaults table)    {        String[] defaultSystemColors = {                "desktop", "#005C5C", /* Color of the desktop background */          "activeCaption", "#000080", /* Color for captions (title bars) when they are active. */      "activeCaptionText", "#FFFFFF", /* Text color for text in captions (title bars). */    "activeCaptionBorder", "#C0C0C0", /* Border color for caption (title bar) window borders. */        "inactiveCaption", "#808080", /* Color for captions (title bars) when not active. */    "inactiveCaptionText", "#C0C0C0", /* Text color for text in inactive captions (title bars). */  "inactiveCaptionBorder", "#C0C0C0", /* Border color for inactive caption (title bar) window borders. */                 "window", "#FFFFFF", /* Default color for the interior of windows */           "windowBorder", "#000000", /* ??? */             "windowText", "#000000", /* ??? */                   "menu", "#C0C0C0", /* Background color for menus */               "menuText", "#000000", /* Text color for menus  */                   "text", "#C0C0C0", /* Text background color */               "textText", "#000000", /* Text foreground color */          "textHighlight", "#000080", /* Text background color when selected */      "textHighlightText", "#FFFFFF", /* Text color when selected */       "textInactiveText", "#808080", /* Text color when disabled */                "control", "#C0C0C0", /* Default color for controls (buttons, sliders, etc) */            "controlText", "#000000", /* Default color for text in controls */       "controlHighlight", "#C0C0C0", /* Specular highlight (opposite of the shadow) */     "controlLtHighlight", "#FFFFFF", /* Highlight color for controls */          "controlShadow", "#808080", /* Shadow color for controls */        "controlDkShadow", "#000000", /* Dark shadow color for controls */              "scrollbar", "#E0E0E0", /* Scrollbar background (usually the "track") */                   "info", "#FFFFE1", /* ??? */               "infoText", "#000000"  /* ??? */        };         loadSystemColors(table, defaultSystemColors, isNativeLookAndFeel());    }      /**     * Populates {@code table} with the {@code name-color} pairs in     * {@code systemColors}. Refer to     * {@link #initSystemColorDefaults(UIDefaults)} for details on     * the format of {@code systemColors}.     * <p>     * An entry is added to {@code table} for each of the {@code name-color}     * pairs in {@code systemColors}. The entry key is     * the {@code name} of the {@code name-color} pair.     * <p>     * The value of the entry corresponds to the {@code color} of the     * {@code name-color} pair.  The value of the entry is calculated     * in one of two ways. With either approach the value is always a     * {@code ColorUIResource}.     * <p>     * If {@code useNative} is {@code false}, the {@code color} is     * created by using {@code Color.decode} to convert the {@code     * String} into a {@code Color}. If {@code decode} can not convert     * the {@code String} into a {@code Color} ({@code     * NumberFormatException} is thrown) then a {@code     * ColorUIResource} of black is used.     * <p>     * If {@code useNative} is {@code true}, the {@code color} is the     * value of the field in {@code SystemColor} with the same name as     * the {@code name} of the {@code name-color} pair. If the field     * is not valid, a {@code ColorUIResource} of black is used.     *     * @param table the {@code UIDefaults} object the values are added to     * @param systemColors array of {@code name-color} pairs as described     *        in {@link #initSystemColorDefaults(UIDefaults)}     * @param useNative whether the color is obtained from {@code SystemColor}     *        or {@code Color.decode}     * @throws NullPointerException if {@code systemColors} is {@code null}; or     *         {@code systemColors} is not empty, and {@code table} is     *         {@code null}; or one of the     *         names of the {@code name-color} pairs is {@code null}; or     *         {@code useNative} is {@code false} and one of the     *         {@code colors} of the {@code name-color} pairs is {@code null}     * @throws ArrayIndexOutOfBoundsException if {@code useNative} is     *         {@code false} and {@code systemColors.length} is odd     *     * @see #initSystemColorDefaults(javax.swing.UIDefaults)     * @see java.awt.SystemColor     * @see java.awt.Color#decode(String)     */    protected void loadSystemColors(UIDefaults table, String[] systemColors, boolean useNative)    {        /* PENDING(hmuller) We don't load the system colors below because         * they're not reliable.  Hopefully we'll be able to do better in         * a future version of AWT.         */        if (useNative) {            for(int i = 0; i < systemColors.length; i += 2) {                Color color = Color.black;                try {                    String name = systemColors[i];                    color = (Color)(SystemColor.class.getField(name).get(null));                } catch (Exception e) {                }                table.put(systemColors[i], new ColorUIResource(color));            }        } else {            for(int i = 0; i < systemColors.length; i += 2) {                Color color = Color.black;                try {                    color = Color.decode(systemColors[i + 1]);                }                catch(NumberFormatException e) {                    e.printStackTrace();                }                table.put(systemColors[i], new ColorUIResource(color));            }        }    }    /**     * Initialize the defaults table with the name of the ResourceBundle     * used for getting localized defaults.  Also initialize the default     * locale used when no locale is passed into UIDefaults.get().  The     * default locale should generally not be relied upon. It is here for     * compatability with releases prior to 1.4.     */    private void initResourceBundle(UIDefaults table) {        table.setDefaultLocale( Locale.getDefault() );        table.addResourceBundle( "com.sun.swing.internal.plaf.basic.resources.basic" );    }     /**     * Populates {@code table} with the defaults for the basic look and     * feel.     *     * @param table the {@code UIDefaults} to add the values to     * @throws NullPointerException if {@code table} is {@code null}     */    protected void initComponentDefaults(UIDefaults table)    {         initResourceBundle(table);         // *** Shared Integers        Integer fiveHundred = new Integer(500);         // *** Shared Longs        Long oneThousand = new Long(1000);         // *** Shared Fonts        Integer twelve = new Integer(12);        Integer fontPlain = new Integer(Font.PLAIN);        Integer fontBold = new Integer(Font.BOLD);        Object dialogPlain12 = new SwingLazyValue(                          "javax.swing.plaf.FontUIResource",                          null,                          new Object[] {Font.DIALOG, fontPlain, twelve});        Object serifPlain12 = new SwingLazyValue(                          "javax.swing.plaf.FontUIResource",                          null,                          new Object[] {Font.SERIF, fontPlain, twelve});        Object sansSerifPlain12 =  new SwingLazyValue(                          "javax.swing.plaf.FontUIResource",                          null,                          new Object[] {Font.SANS_SERIF, fontPlain, twelve});        Object monospacedPlain12 = new SwingLazyValue(                          "javax.swing.plaf.FontUIResource",                          null,                          new Object[] {Font.MONOSPACED, fontPlain, twelve});        Object dialogBold12 = new SwingLazyValue(                          "javax.swing.plaf.FontUIResource",                          null,                          new Object[] {Font.DIALOG, fontBold, twelve});          // *** Shared Colors        ColorUIResource red = new ColorUIResource(Color.red);        ColorUIResource black = new ColorUIResource(Color.black);        ColorUIResource white = new ColorUIResource(Color.white);        ColorUIResource yellow = new ColorUIResource(Color.yellow);        ColorUIResource gray = new ColorUIResource(Color.gray);        ColorUIResource lightGray = new ColorUIResource(Color.lightGray);        ColorUIResource darkGray = new ColorUIResource(Color.darkGray);        ColorUIResource scrollBarTrack = new ColorUIResource(224, 224, 224);         Color control = table.getColor("control");        Color controlDkShadow = table.getColor("controlDkShadow");        Color controlHighlight = table.getColor("controlHighlight");        Color controlLtHighlight = table.getColor("controlLtHighlight");        Color controlShadow = table.getColor("controlShadow");

Lines 1–500 of 2313.