synthoptionpaneui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,377 行 · 第 1/4 页

JAVA
1,377
字号
/* * @(#)SynthOptionPaneUI.java	1.11 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import javax.swing.border.Border;import javax.swing.border.EmptyBorder;import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.ActionMapUIResource;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.OptionPaneUI;import java.awt.*;import java.awt.event.*;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.Locale;import java.security.AccessController;import sun.security.action.GetPropertyAction;/** * Provides the basic look and feel for a <code>JOptionPane</code>. * <code>BasicMessagePaneUI</code> provides a means to place an icon, * message and buttons into a <code>Container</code>. * Generally, the layout will look like:<p> * <pre> *        ------------------ *        | i | message    | *        | c | message    | *        | o | message    | *        | n | message    | *        ------------------ *        |     buttons    | *        |________________| * </pre> * icon is an instance of <code>Icon</code> that is wrapped inside a * <code>JLabel</code>.  The message is an opaque object and is tested * for the following: if the message is a <code>Component</code> it is * added to the <code>Container</code>, if it is an <code>Icon</code> * it is wrapped inside a <code>JLabel</code> and added to the  * <code>Container</code> otherwise it is wrapped inside a <code>JLabel</code>. * <p> * The above layout is used when the option pane's  * <code>ComponentOrientation</code> property is horizontal, left-to-right. * The layout will be adjusted appropriately for other orientations. * <p> * The <code>Container</code>, message, icon, and buttons are all * determined from abstract methods. *  * @version 1.11, 01/23/03 (based on BasicOptionPaneUI v 1.54) * @author James Gosling * @author Scott Violet * @author Amy Fowler */class SynthOptionPaneUI extends OptionPaneUI implements SynthUI{    public static final int MinimumWidth = 262;    public static final int MinimumHeight = 90;    private static String newline;    private SynthStyle style;    /**     * <code>JOptionPane</code> that the receiver is providing the     * look and feel for.     */    protected JOptionPane         optionPane;    protected Dimension minimumSize;    /** JComponent provide for input if optionPane.getWantsInput() returns     * true. */    protected JComponent          inputComponent;    /** Component to receive focus when messaged with selectInitialValue. */    protected Component           initialFocusComponent;    /** This is set to true in validateComponent if a Component is contained     * in either the message or the buttons. */    protected boolean             hasCustomComponents;    protected PropertyChangeListener propertyChangeListener;    private int multiClickThreshold;    static {	newline = (String)java.security.AccessController.doPrivileged(                                new GetPropertyAction("line.separator"));        if (newline == null) {            newline = "\n";        }    }    /**      * Creates a new BasicOptionPaneUI instance.      */    public static ComponentUI createUI(JComponent x) {	return new SynthOptionPaneUI();    }   public static void loadActionMap(ActionMap map) {        // NOTE: this needs to remain static. If you have a need to        // have Actions that reference the UI in the ActionMap,        // then you'll also need to change the registeration of the        // ActionMap.	map.put("close", new CloseAction());	// Set the ActionMap's parent to the Auditory Feedback Action Map        // PENDING: audio/*	BasicLookAndFeel lf = (BasicLookAndFeel)UIManager.getLookAndFeel();	ActionMap audioMap = lf.getAudioActionMap();	map.setParent(audioMap);*/    }     /**      * Installs the receiver as the L&F for the passed in      * <code>JOptionPane</code>.      */    public void installUI(JComponent c) {	optionPane = (JOptionPane)c;        installDefaults();        optionPane.setLayout(createLayoutManager());	installComponents();        installListeners();         installKeyboardActions();    }    /**      * Removes the receiver from the L&F controller of the passed in split      * pane.      */    public void uninstallUI(JComponent c) {        uninstallComponents();        optionPane.setLayout(null);        uninstallKeyboardActions();        uninstallListeners();        uninstallDefaults();	optionPane = null;    }    protected void installDefaults() {        fetchStyle(optionPane);    }    private void fetchStyle(JComponent c) {        SynthContext context = getContext(c, ENABLED);        SynthStyle oldStyle = style;        style = SynthLookAndFeel.updateStyle(context, this);        if (style != oldStyle) {            minimumSize = (Dimension)style.get(context,                                               "OptionPane.minimumSize");            multiClickThreshold = style.getInt(                      context, "OptionPane.buttonClickThreshhold", 0);        }        context.dispose();    }    protected void uninstallDefaults() {        SynthContext context = getContext(optionPane, ENABLED);        style.uninstallDefaults(context);        context.dispose();        style = null;    }    protected void installComponents() {	optionPane.add(createMessageArea());                Container separator = createSeparator();        if (separator != null) {            optionPane.add(separator);            SynthContext context = getContext(optionPane, ENABLED);            optionPane.add(Box.createVerticalStrut(context.getStyle().                       getInt(context, "OptionPane.separatorPadding", 6)));            context.dispose();        }	optionPane.add(createButtonArea());	optionPane.applyComponentOrientation(optionPane.getComponentOrientation());    }    protected void uninstallComponents() {	hasCustomComponents = false;        inputComponent = null;	initialFocusComponent = null;	optionPane.removeAll();    }    protected LayoutManager createLayoutManager() {        return new BoxLayout(optionPane, BoxLayout.Y_AXIS);    }    protected void installListeners() {        if ((propertyChangeListener = createPropertyChangeListener()) != null) {            optionPane.addPropertyChangeListener(propertyChangeListener);        }    }    protected void uninstallListeners() {        if (propertyChangeListener != null) {            optionPane.removePropertyChangeListener(propertyChangeListener);            propertyChangeListener = null;        }    }    protected PropertyChangeListener createPropertyChangeListener() {        return new PropertyChangeHandler();    }    protected void installKeyboardActions() {	InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);	SwingUtilities.replaceUIInputMap(optionPane, JComponent.				       WHEN_IN_FOCUSED_WINDOW, map);        LazyActionMap.installLazyActionMap(optionPane, SynthOptionPaneUI.class,                                           "OptionPane.actionMap");    }    protected void uninstallKeyboardActions() {	SwingUtilities.replaceUIInputMap(optionPane, JComponent.				       WHEN_IN_FOCUSED_WINDOW, null);	SwingUtilities.replaceUIActionMap(optionPane, null);    }    InputMap getInputMap(int condition) {	if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {            SynthContext context = getContext(optionPane, ENABLED);	    Object[] bindings = (Object[])context.getStyle().get		                (context, "OptionPane.windowBindings");            InputMap map = null;	    if (bindings != null) {		map = LookAndFeel.makeComponentInputMap(optionPane, bindings);	    }            context.dispose();            return map;	}	return null;    }    public SynthContext getContext(JComponent c) {        return getContext(c, getComponentState(c));    }    private SynthContext getContext(JComponent c, int state) {        return SynthContext.getContext(SynthContext.class, c,                    SynthLookAndFeel.getRegion(c), style, state);    }    private Region getRegion(JComponent c) {        return SynthLookAndFeel.getRegion(c);    }    private int getComponentState(JComponent c) {        return SynthLookAndFeel.getComponentState(c);    }    public void update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        paint(context, g);        context.dispose();    }    public void paint(Graphics g, JComponent c) {        SynthContext context = getContext(c);        paint(context, g);        context.dispose();    }    protected void paint(SynthContext context, Graphics g) {    }    /**     * Returns the minimum size the option pane should be. Primarily     * provided for subclassers wishing to offer a different minimum size.     */    public Dimension getMinimumOptionPaneSize() {        if (minimumSize == null) {            return new Dimension(MinimumWidth, MinimumHeight);        }	return new Dimension(minimumSize.width,			     minimumSize.height);    }    /**     * If <code>c</code> is the <code>JOptionPane</code> the receiver     * is contained in, the preferred     * size that is returned is the maximum of the preferred size of     * the <code>LayoutManager</code> for the <code>JOptionPane</code>, and     * <code>getMinimumOptionPaneSize</code>.     */    public Dimension getPreferredSize(JComponent c) {	if ((JOptionPane)c == optionPane) {	    Dimension            ourMin = getMinimumOptionPaneSize();	    LayoutManager        lm = c.getLayout();	    if (lm != null) {		Dimension         lmSize = lm.preferredLayoutSize(c);		if (ourMin != null)		    return new Dimension			(Math.max(lmSize.width, ourMin.width),			 Math.max(lmSize.height, ourMin.height));		return lmSize;	    }	    return ourMin;	}	return null;    }    /**     * Messaged from installComponents to create a Container containing the     * body of the message. The icon is the created by calling     * <code>addIcon</code>.     */    protected Container createMessageArea() {        JPanel top = new JPanel();        top.setName("OptionPane.messageArea");	top.setLayout(new BorderLayout());	/* Fill the body. */	Container          body = new JPanel(new GridBagLayout());	Container          realBody = new JPanel(new BorderLayout());        body.setName("OptionPane.body");        realBody.setName("OptionPane.realBody");	if (getIcon() != null) {            JPanel sep = new JPanel();            sep.setName("OptionPane.separator");            sep.setPreferredSize(new Dimension(15, 1));	    realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);	}	realBody.add(body, BorderLayout.CENTER);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?