metalrootpaneui.java

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

JAVA
981
字号
    private void addSubComponents()    {      add(menuBar);      add(closeButton);      add(iconButton);      add(maxButton);    }    private void installListeners()    {      Window window = SwingUtilities.getWindowAncestor(rootPane);      window.addWindowFocusListener(new WindowFocusListener()                       {                         public void windowGainedFocus(WindowEvent ev)                         {                           repaint();                         }                         public void windowLostFocus(WindowEvent ev)                         {                           repaint();                         }                       });    }    private void createActions()    {      closeAction = new CloseAction();    }    private void assembleSystemMenu()    {      menuBar = createSystemMenuBar();      windowMenu = createSystemMenu();      menuBar.add(windowMenu);      addSystemMenuItems(windowMenu);      enableActions();    }    protected JMenuBar createSystemMenuBar()    {      if (menuBar == null)        menuBar = new JMenuBar();      menuBar.removeAll();      return menuBar;    }    protected JMenu createSystemMenu()    {      if (windowMenu == null)        windowMenu = new JMenu();      windowMenu.removeAll();      return windowMenu;    }    private void addSystemMenuItems(JMenu menu)    {      // TODO: Implement this.    }    protected void createButtons()    {      closeButton = new PaneButton(closeAction);      closeButton.setText(null);      iconButton = new PaneButton(iconifyAction);      iconButton.setText(null);      maxButton = new PaneButton(maximizeAction);      maxButton.setText(null);      closeButton.setBorderPainted(false);      closeButton.setContentAreaFilled(false);      iconButton.setBorderPainted(false);      iconButton.setContentAreaFilled(false);      maxButton.setBorderPainted(false);      maxButton.setContentAreaFilled(false);    }    protected void setButtonIcons()    {      if (closeIcon != null && closeButton != null)        closeButton.setIcon(closeIcon);      if (iconIcon != null && iconButton != null)        iconButton.setIcon(iconIcon);      if (maxIcon != null && maxButton != null)        maxButton.setIcon(maxIcon);    }    /**     * Paints a representation of the current state of the internal frame.     *      * @param g  the graphics device.     */    public void paintComponent(Graphics g)    {      Window frame = SwingUtilities.getWindowAncestor(rootPane);      Color savedColor = g.getColor();      paintTitleBackground(g);      paintChildren(g);      Dimension d = getSize();      if (frame.isActive())        g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());      else        g.setColor(MetalLookAndFeel.getControlDarkShadow());                // put a dot in each of the top corners      g.drawLine(0, 0, 0, 0);      g.drawLine(d.width - 1, 0, d.width - 1, 0);                g.drawLine(0, d.height - 1, d.width - 1, d.height - 1);                // draw the metal pattern      if (UIManager.get("InternalFrame.activeTitleGradient") != null          && frame.isActive())        {          MetalUtils.paintGradient(g, 0, 0, getWidth(), getHeight(),                                   SwingConstants.VERTICAL,          "InternalFrame.activeTitleGradient");        }      Rectangle b = title.getBounds();      int startX = b.x + b.width + 5;      int endX = startX;      if (iconButton.isVisible())        endX = Math.max(iconButton.getX(), endX);      else if (maxButton.isVisible())         endX = Math.max(maxButton.getX(), endX);      else if (closeButton.isVisible())        endX = Math.max(closeButton.getX(), endX);      endX -= 7;      if (endX > startX)        MetalUtils.fillMetalPattern(this, g, startX, 3, endX - startX, getHeight() - 6, Color.white, Color.gray);      g.setColor(savedColor);    }    /**     * This method paints the TitlePane's background.     *     * @param g The Graphics object to paint with.     */    protected void paintTitleBackground(Graphics g)    {      Window frame = SwingUtilities.getWindowAncestor(rootPane);      if (!isOpaque())        return;            Color saved = g.getColor();      Dimension dims = getSize();            Color bg = getBackground();      if (frame.isActive())        bg = selectedTitleColor;      else        bg = notSelectedTitleColor;      g.setColor(bg);      g.fillRect(0, 0, dims.width, dims.height);      g.setColor(saved);    }    /**     * This method installs the defaults determined by the look and feel.     */    private void installDefaults()    {      title.setFont(UIManager.getFont("InternalFrame.titleFont"));      selectedTitleColor = UIManager.getColor("InternalFrame.activeTitleBackground");      notSelectedTitleColor = UIManager.getColor("InternalFrame.inactiveTitleBackground");      closeIcon = UIManager.getIcon("InternalFrame.closeIcon");      iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon");      maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon");      minIcon = MetalIconFactory.getInternalFrameAltMaximizeIcon(16);      Frame frame = (Frame) SwingUtilities.getWindowAncestor(rootPane);      title = new JLabel(frame.getTitle(),               MetalIconFactory.getInternalFrameDefaultMenuIcon(),               SwingConstants.LEFT);    }  }  private static class MetalRootLayout    implements LayoutManager2  {    /**     * The cached layout info for the glass pane.     */    private Rectangle glassPaneBounds;    /**     * The cached layout info for the layered pane.     */    private Rectangle layeredPaneBounds;    /**     * The cached layout info for the content pane.     */    private Rectangle contentPaneBounds;    /**     * The cached layout info for the menu bar.     */    private Rectangle menuBarBounds;    /**     * The cached layout info for the title pane.     */    private Rectangle titlePaneBounds;        /**     * The cached preferred size.     */    private Dimension prefSize;    public void addLayoutComponent(Component component, Object constraints)    {      // Nothing to do here.    }    public Dimension maximumLayoutSize(Container target)    {      return preferredLayoutSize(target);    }    public float getLayoutAlignmentX(Container target)    {      return 0.0F;    }    public float getLayoutAlignmentY(Container target)    {      return 0.0F;    }    public void invalidateLayout(Container target)    {      synchronized (this)      {        glassPaneBounds = null;        layeredPaneBounds = null;        contentPaneBounds = null;        menuBarBounds = null;        titlePaneBounds = null;        prefSize = null;      }    }    public void addLayoutComponent(String name, Component component)    {      // Nothing to do here.    }    public void removeLayoutComponent(Component component)    {      // TODO Auto-generated method stub          }    public Dimension preferredLayoutSize(Container parent)    {      JRootPane rp = (JRootPane) parent;      JLayeredPane layeredPane = rp.getLayeredPane();      Component contentPane = layeredPane.getComponent(0);      Component titlePane = layeredPane.getComponent(1);      Component menuBar = null;      if (layeredPane.getComponentCount() > 2          && layeredPane.getComponent(2) instanceof JMenuBar)        menuBar = layeredPane.getComponent(2);      // We must synchronize here, otherwise we cannot guarantee that the      // prefSize is still non-null when returning.      synchronized (this)        {          if (prefSize == null)            {              Insets i = parent.getInsets();              prefSize = new Dimension(i.left + i.right, i.top + i.bottom);              Dimension contentPrefSize = contentPane.getPreferredSize();              prefSize.width += contentPrefSize.width;              prefSize.height += contentPrefSize.height                                 + titlePane.getPreferredSize().height;              if (menuBar != null)                {                  Dimension menuBarSize = menuBar.getPreferredSize();                  if (menuBarSize.width > contentPrefSize.width)                    prefSize.width += menuBarSize.width - contentPrefSize.width;                  prefSize.height += menuBarSize.height;                }            }          // Return a copy here so the cached value won't get trashed by some          // other component.          return new Dimension(prefSize);      }    }    public Dimension minimumLayoutSize(Container parent)    {      return preferredLayoutSize(parent);    }    public void layoutContainer(Container parent)    {      JRootPane rp = (JRootPane) parent;      JLayeredPane layeredPane = rp.getLayeredPane();      Component contentPane = layeredPane.getComponent(0);      Component titlePane = layeredPane.getComponent(1);      Component menuBar = null;      if (layeredPane.getComponentCount() > 2          && layeredPane.getComponent(2) instanceof JMenuBar)        menuBar = layeredPane.getComponent(2);      Component glassPane = rp.getGlassPane();      if (glassPaneBounds == null || layeredPaneBounds == null          || contentPaneBounds == null || menuBarBounds == null)        {          Insets i = rp.getInsets();          int containerWidth = parent.getBounds().width - i.left - i.right;          int containerHeight = parent.getBounds().height - i.top - i.bottom;          // 1. The glassPane fills entire viewable region (bounds - insets).          // 2. The layeredPane filles entire viewable region.          // 3. The titlePane is placed at the upper edge of the layeredPane.          // 4. The menuBar is positioned at the upper edge of layeredPane.          // 5. The contentPane fills viewable region minus menuBar minus          //    titlePane, if present.                // +-------------------------------+          // |  JLayeredPane                 |          // |  +--------------------------+ |          // |  | titlePane                + |          // |  +--------------------------+ |          // |  +--------------------------+ |          // |  | menuBar                  | |          // |  +--------------------------+ |          // |  +--------------------------+ |          // |  |contentPane               | |          // |  |                          | |          // |  |                          | |          // |  |                          | |          // |  +--------------------------+ |          // +-------------------------------+          // Setup titlePaneBounds.          if (titlePaneBounds == null)            titlePaneBounds = new Rectangle();          titlePaneBounds.width = containerWidth;          titlePaneBounds.height = titlePane.getPreferredSize().height;          // Setup menuBarBounds.          if (menuBarBounds == null)            menuBarBounds = new Rectangle();          menuBarBounds.setBounds(0,                                  titlePaneBounds.y + titlePaneBounds.height,                                  containerWidth, 0);          if (menuBar != null)            {              Dimension menuBarSize = menuBar.getPreferredSize();              if (menuBarSize.height > containerHeight)                menuBarBounds.height = containerHeight;              else                menuBarBounds.height = menuBarSize.height;            }          // Setup contentPaneBounds.          if (contentPaneBounds == null)            contentPaneBounds = new Rectangle();          contentPaneBounds.setBounds(0,                                      menuBarBounds.y + menuBarBounds.height,                                      containerWidth,                                      containerHeight - menuBarBounds.y                                      - menuBarBounds.height);          glassPaneBounds = new Rectangle(i.left, i.top, containerWidth, containerHeight);          layeredPaneBounds = new Rectangle(i.left, i.top, containerWidth, containerHeight);        }      // Layout components.      glassPane.setBounds(glassPaneBounds);      layeredPane.setBounds(layeredPaneBounds);      if (menuBar != null)        menuBar.setBounds(menuBarBounds);      contentPane.setBounds(contentPaneBounds);      titlePane.setBounds(titlePaneBounds);    }        }  /**   * The shared UI instance for MetalRootPaneUIs.   */  private static MetalRootPaneUI instance = null;  /**   * Constructs a shared instance of <code>MetalRootPaneUI</code>.   */  public MetalRootPaneUI()  {    super();  }  /**   * Returns a shared instance of <code>MetalRootPaneUI</code>.   *   * @param component the component for which we return an UI instance   *   * @return A shared instance of <code>MetalRootPaneUI</code>.   */  public static ComponentUI createUI(JComponent component)  {    if (instance == null)      instance = new MetalRootPaneUI();    return instance;  }  /**   * Installs this UI to the root pane. If the   * <code>windowDecorationsStyle</code> property is set on the root pane,   * the Metal window decorations are installed on the root pane.   *   * @param c   */  public void installUI(JComponent c)  {    super.installUI(c);    JRootPane rp = (JRootPane) c;    if (rp.getWindowDecorationStyle() != JRootPane.NONE)      installWindowDecorations(rp);  }  /**   * Uninstalls the UI from the root pane. This performs the superclass   * behaviour and uninstalls the window decorations that have possibly been   * installed by {@link #installUI}.   *   * @param c the root pane   */  public void uninstallUI(JComponent c)  {    JRootPane rp = (JRootPane) c;    if (rp.getWindowDecorationStyle() != JRootPane.NONE)      uninstallWindowDecorations(rp);    super.uninstallUI(c);  }  /**   * Receives notification if any of the JRootPane's property changes. In   * particular this catches changes to the <code>windowDecorationStyle</code>   * property and installs the window decorations accordingly.   *   * @param ev the property change event   */  public void propertyChange(PropertyChangeEvent ev)  {    String propertyName = ev.getPropertyName();    if (propertyName.equals("windowDecorationStyle"))      {        JRootPane rp = (JRootPane) ev.getSource();        if (rp.getWindowDecorationStyle() != JRootPane.NONE)          installWindowDecorations(rp);        else          uninstallWindowDecorations(rp);      }  }  /**   * Installs the window decorations to the root pane. This sets up a border,   * a title pane and a layout manager that can layout the root pane with that   * title pane.   *   * @param rp the root pane.   */  private void installWindowDecorations(JRootPane rp)  {    rp.setBorder(new MetalFrameBorder());    rp.setLayout(new MetalRootLayout());    // We should have a contentPane already.    assert rp.getLayeredPane().getComponentCount() == 1           : "We should have a contentPane already";    rp.getLayeredPane().add(new MetalTitlePane(rp),                            JLayeredPane.FRAME_CONTENT_LAYER);  }  /**   * Uninstalls the window decorations from the root pane. This should rarely   * be necessary, but we do it anyway.   *   * @param rp the root pane   */  private void uninstallWindowDecorations(JRootPane rp)  {    rp.setBorder(null);    rp.getLayeredPane().remove(1);  }}

⌨️ 快捷键说明

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