jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsMenuBarUI.java
author darcy
Mon, 10 Mar 2014 13:54:25 -0700
changeset 23632 cd7a34d12e8c
parent 5506 202f599c92aa
child 25135 cd4631f0afdc
permissions -rw-r--r--
8033908: Fix serial lint warnings in com.sun.java.swing.plaf Reviewed-by: serb, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23632
cd7a34d12e8c 8033908: Fix serial lint warnings in com.sun.java.swing.plaf
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.swing.plaf.windows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.plaf.ActionMapUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.event.HierarchyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.event.HierarchyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.WindowListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.event.WindowStateListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.java.swing.plaf.windows.TMSchema.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.java.swing.plaf.windows.XPStyle.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Windows rendition of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * future Swing releases.  The current serialization support is appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * for short term storage or RMI between applications running the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * version of Swing.  A future release of Swing will provide support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * long term persistence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class WindowsMenuBarUI extends BasicMenuBarUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* to be accessed on the EDT only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private WindowListener windowListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private HierarchyListener hierarchyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Window window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return new WindowsMenuBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        uninstallWindowListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (hierarchyListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            menuBar.removeHierarchyListener(hierarchyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            hierarchyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private void installWindowListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (windowListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            Component component = menuBar.getTopLevelAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (component instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                window = (Window) component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                windowListener = new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    public void windowActivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        menuBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    public void windowDeactivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        menuBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                ((Window) component).addWindowListener(windowListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void uninstallWindowListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (windowListener != null && window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            window.removeWindowListener(windowListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        windowListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (WindowsLookAndFeel.isOnVista()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            installWindowListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            hierarchyListener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                new HierarchyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    public void hierarchyChanged(HierarchyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        if ((e.getChangeFlags()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            if (menuBar.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                installWindowListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                uninstallWindowListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            menuBar.addHierarchyListener(hierarchyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        super.installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ActionMap map = SwingUtilities.getUIActionMap(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            map = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            SwingUtilities.replaceUIActionMap(menuBar, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        map.put("takeFocus", new TakeFocus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Action that activates the menu (e.g. when F10 is pressed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Unlike BasicMenuBarUI.TakeFocus, this Action will not show menu popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
23632
cd7a34d12e8c 8033908: Fix serial lint warnings in com.sun.java.swing.plaf
darcy
parents: 5506
diff changeset
   137
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static class TakeFocus extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            JMenuBar menuBar = (JMenuBar)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            JMenu menu = menuBar.getMenu(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (menu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                MenuSelectionManager msm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                MenuElement path[] = new MenuElement[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                path[0] = (MenuElement)menuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                path[1] = (MenuElement)menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                msm.setSelectedPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // show mnemonics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                WindowsLookAndFeel.setMnemonicHidden(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                WindowsLookAndFeel.repaintRootPane(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (WindowsMenuItemUI.isVistaPainting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            XPStyle xp = XPStyle.getXP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Skin skin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            skin = xp.getSkin(c, Part.MP_BARBACKGROUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            int width = c.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            int height = c.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            State state =  isActive(c) ? State.ACTIVE : State.INACTIVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            skin.paintSkin(g, 0, 0, width, height, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            super.paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Checks if component belongs to an active window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param c component to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return true if component belongs to an active window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static boolean isActive(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        JRootPane rootPane = c.getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (rootPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            Component component = rootPane.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (component instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return ((Window) component).isActive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}