metalrootpaneui.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 981 行 · 第 1/2 页

JAVA
981
字号
/* MetalRootPaneUI.java   Copyright (C) 2005, 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.metal;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Frame;import java.awt.Graphics;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.LayoutManager2;import java.awt.Rectangle;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.WindowEvent;import java.awt.event.WindowFocusListener;import java.beans.PropertyChangeEvent;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JLayeredPane;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JRootPane;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.border.AbstractBorder;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.basic.BasicRootPaneUI;/** * A UI delegate for the {@link JRootPane} component. This implementation * supports the JRootPane <code>windowDecorationStyle</code> property.   *  * @author Roman Kennke (kennke@aicas.com) * * @since 1.4 */public class MetalRootPaneUI  extends BasicRootPaneUI{  /**   * The border that is used on JRootPane when the windowDecorationStyle   * property of the JRootPane is set to a different value than NONE.   *   * @author Roman Kennke (kennke@aicas.com)   */  private static class MetalFrameBorder    extends AbstractBorder  {    /**     * Returns the border insets.     *     * @param c the component     * @param newInsets the insets to be filled with the return value, may be     *        <code>null</code> in which case a new object is created     *     * @return the border insets     */    public Insets getBorderInsets(Component c, Insets newInsets)    {      if (newInsets == null)        newInsets = new Insets(5, 5, 5, 5);      else        {          newInsets.top = 5;          newInsets.left = 5;          newInsets.bottom = 5;          newInsets.right = 5;        }      return newInsets;      }    /**     * Returns the border insets.     *     * @param c the component     *     * @return the border insets     */    public Insets getBorderInsets(Component c)    {      return getBorderInsets(c, null);    }    /**     * Paints the border for the specified component.     *      * @param c  the component     * @param g  the graphics device     * @param x  the x-coordinate     * @param y  the y-coordinate     * @param w  the width     * @param h  the height     */    public void paintBorder(Component c, Graphics g, int x, int y, int w,                             int h)    {      JRootPane f = (JRootPane) c;      Window frame = SwingUtilities.getWindowAncestor(f);      if (frame.isActive())        g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());      else        g.setColor(MetalLookAndFeel.getControlDarkShadow());            // Fill the border background.      g.fillRect(x, y, w, 5);      g.fillRect(x, y, 5, h);      g.fillRect(x + w - 5, y, 5, h);      g.fillRect(x, y + h - 5, w, 5);            // Draw a dot in each corner.      g.setColor(MetalLookAndFeel.getControl());      g.fillRect(x, y, 1, 1);      g.fillRect(x + w - 1, y, 1, 1);      g.fillRect(x + w - 1, y + h - 1, 1, 1);      g.fillRect(x, y + h - 1, 1, 1);            // Draw the lines.      g.setColor(MetalLookAndFeel.getBlack());      g.drawLine(x + 14, y + 2, x + w - 15, y + 2);      g.drawLine(x + 14, y + h - 3, x + w - 15, y + h - 3);      g.drawLine(x + 2, y + 14, x + 2, y + h - 15);      g.drawLine(x + w - 3, y + 14, x + w - 3, y + h - 15);            // Draw the line highlights.      if (frame.isActive())        g.setColor(MetalLookAndFeel.getPrimaryControlShadow());      else         g.setColor(MetalLookAndFeel.getControlShadow());      g.drawLine(x + 15, y + 3, x + w - 14, y + 3);      g.drawLine(x + 15, y + h - 2, x + w - 14, y + h - 2);      g.drawLine(x + 3, y + 15, x + 3, y + h - 14);      g.drawLine(x + w - 2, y + 15, x + w - 2, y + h - 14);    }  }  /**   * The component that renders the title bar for frames. This duplicates   * most of {@link MetalInternalFrameTitlePane}. It is not reasonably possible   * to reuse that class because that is bound to the JInternalFrame and we   * need to handle JFrames/JRootPanes here.   *   * @author Roman Kennke (kennke@aicas.com)   */  private static class MetalTitlePane extends JComponent  {    /**     * The Action responsible for closing the JInternalFrame.     */    private class CloseAction extends AbstractAction    {      /**       * Creates a new action.       */      public CloseAction()      {        super("Close");      }            /**       * This method is called when something closes the frame.       *       * @param e the ActionEvent       */      public void actionPerformed(ActionEvent e)      {        Window frame = SwingUtilities.getWindowAncestor(rootPane);        if (frame instanceof JFrame)          {            JFrame jframe = (JFrame) frame;            switch (jframe.getDefaultCloseOperation())            {              case JFrame.EXIT_ON_CLOSE:                jframe.setVisible(false);                jframe.dispose();                System.exit(0);                break;              case JFrame.DISPOSE_ON_CLOSE:                jframe.setVisible(false);                jframe.dispose();                break;              case JFrame.HIDE_ON_CLOSE:                jframe.setVisible(false);                break;              case JFrame.DO_NOTHING_ON_CLOSE:              default:                  break;            }          }        else if (frame instanceof JDialog)          {            JDialog jdialog = (JDialog) frame;            switch (jdialog.getDefaultCloseOperation())            {              case JFrame.DISPOSE_ON_CLOSE:                jdialog.setVisible(false);                jdialog.dispose();                break;              case JFrame.HIDE_ON_CLOSE:                jdialog.setVisible(false);                break;              case JFrame.DO_NOTHING_ON_CLOSE:              default:                  break;            }          }      }    }    /**     * This helper class is used to create the minimize, maximize and close     * buttons in the top right corner of the Title Pane. These buttons are     * special since they cannot be given focus and have no border.     */    private class PaneButton extends JButton    {      /**       * Creates a new PaneButton object with the given Action.       *       * @param a The Action that the button uses.       */      public PaneButton(Action a)      {        super(a);        setMargin(new Insets(0, 0, 0, 0));      }      /**       * This method returns true if the Component can be focused.       *       * @return false.       */      public boolean isFocusable()      {        // These buttons cannot be given focus.        return false;      }    }    /**     * The layout for the JRootPane when the <code>windowDecorationStyle</code>     * property is set. In addition to the usual JRootPane.RootLayout behaviour     * this lays out the titlePane.     *     * @author Roman Kennke (kennke@aicas.com)     */    private class MetalTitlePaneLayout implements LayoutManager    {      /**       * Creates a new <code>TitlePaneLayout</code> object.       */      public MetalTitlePaneLayout()      {        // Do nothing.      }      /**       * Adds a Component to the Container.       *       * @param name The name to reference the added Component by.       * @param c The Component to add.       */      public void addLayoutComponent(String name, Component c)      {        // Do nothing.      }      /**       * This method is called to lay out the children of the Title Pane.       *       * @param c The Container to lay out.       */      public void layoutContainer(Container c)      {        Dimension size = c.getSize();        Insets insets = c.getInsets();        int width = size.width - insets.left - insets.right;        int height = size.height - insets.top - insets.bottom;        int loc = width - insets.right - 1;        int top = insets.top + 2;        int buttonHeight = height - 4;        if (closeButton.isVisible())          {            int buttonWidth = closeIcon.getIconWidth();            loc -= buttonWidth + 2;            closeButton.setBounds(loc, top, buttonWidth, buttonHeight);            loc -= 6;          }        if (maxButton.isVisible())          {            int buttonWidth = maxIcon.getIconWidth();            loc -= buttonWidth + 4;            maxButton.setBounds(loc, top, buttonWidth, buttonHeight);          }        if (iconButton.isVisible())          {            int buttonWidth = minIcon.getIconWidth();            loc -= buttonWidth + 4;            iconButton.setBounds(loc, top, buttonWidth, buttonHeight);            loc -= 2;          }        Dimension titlePreferredSize = title.getPreferredSize();        title.setBounds(insets.left + 5, insets.top,                 Math.min(titlePreferredSize.width, loc - insets.left - 10),                 height);      }      /**       * This method returns the minimum size of the given Container given the       * children that it has.       *       * @param c The Container to get a minimum size for.       *       * @return The minimum size of the Container.       */      public Dimension minimumLayoutSize(Container c)      {        return preferredLayoutSize(c);      }      /**       * Returns the preferred size of the given Container taking       * into account the children that it has.       *       * @param c The Container to lay out.       *       * @return The preferred size of the Container.       */      public Dimension preferredLayoutSize(Container c)      {        return new Dimension(22, 22);      }      /**       * Removes a Component from the Container.       *       * @param c The Component to remove.       */      public void removeLayoutComponent(Component c)      {        // Nothing to do here.      }    }    JRootPane rootPane;    /** The button that closes the JInternalFrame. */    JButton closeButton;    /** The button that iconifies the JInternalFrame. */    JButton iconButton;    /** The button that maximizes the JInternalFrame. */    JButton maxButton;    Icon minIcon;    /** The icon displayed in the maximize button. */    Icon maxIcon;    /** The icon displayed in the iconify button. */    private Icon iconIcon;    /** The icon displayed in the close button. */    Icon closeIcon;        /**     * The background color of the TitlePane when the JInternalFrame is not     * selected.     */    private Color notSelectedTitleColor;    /**     * The background color of the TitlePane when the JInternalFrame is     * selected.     */    private Color selectedTitleColor;    /**     * The label used to display the title. This label is not added to the     * TitlePane.     */    JLabel title;    /** The action associated with closing the JInternalFrame. */    private Action closeAction;    /** The action associated with iconifying the JInternalFrame. */    private Action iconifyAction;    /** The action associated with maximizing the JInternalFrame. */    private Action maximizeAction;    /** The JMenuBar that is located at the top left of the Title Pane. */    private JMenuBar menuBar;    /** The JMenu inside the menuBar. */    protected JMenu windowMenu;    MetalTitlePane(JRootPane rp)    {      rootPane = rp;      setLayout(createLayout());      title = new JLabel();      title.setHorizontalAlignment(SwingConstants.LEFT);      title.setHorizontalTextPosition(SwingConstants.LEFT);      title.setOpaque(false);      installTitlePane();    }    protected LayoutManager createLayout()    {      return new MetalTitlePaneLayout();    }    /**     * This method installs the TitlePane onto the JInternalFrameTitlePane. It     * also creates any children components that need to be created and adds     * listeners to the appropriate components.     */    protected void installTitlePane()    {      installDefaults();      installListeners();      createActions();      assembleSystemMenu();      createButtons();      setButtonIcons();      addSubComponents();      enableActions();    }    private void enableActions()    {      // TODO: Implement this.    }

⌨️ 快捷键说明

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