jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsRootPaneUI.java
author ant
Fri, 31 Aug 2012 16:31:29 +0400
changeset 13652 42544e68dc39
parent 5506 202f599c92aa
child 14310 708173457cc0
permissions -rw-r--r--
6981400: Tabbing between textfield do not work properly when ALT+TAB 7157015: [macosx] Situation when KeyEventDispatcher doesn't work on AWT but does on Swing. 7121442: Regression : Reopen CR 6458497 still reproducible using JDK 7. Reviewed-by: art, leonidr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2006, 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 java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.KeyEventPostProcessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Window;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
    33
import java.awt.Toolkit;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
    34
import sun.awt.SunToolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.AbstractAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.ActionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.InputMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.KeyStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.JComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.JLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.JRootPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.UIManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.swing.AbstractButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.swing.JFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.swing.JMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.swing.JMenuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.swing.MenuElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.swing.MenuSelectionManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.swing.plaf.ActionMapUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.swing.plaf.InputMapUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.swing.plaf.basic.BasicRootPaneUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.swing.plaf.basic.ComboPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Windows implementation of RootPaneUI, there is one shared between all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * JRootPane instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class WindowsRootPaneUI extends BasicRootPaneUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final static WindowsRootPaneUI windowsRootPaneUI = new WindowsRootPaneUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final AltProcessor altProcessor = new AltProcessor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return windowsRootPaneUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static class AltProcessor implements KeyEventPostProcessor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        static boolean altKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        static boolean menuCanceledOnPress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        static JRootPane root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        static Window winAncestor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        void altPressed(KeyEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            MenuSelectionManager msm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            MenuElement[] path = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                msm.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                menuCanceledOnPress = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ev.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else if(path.length > 0) { // We are in ComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                menuCanceledOnPress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                WindowsLookAndFeel.setMnemonicHidden(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                ev.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                menuCanceledOnPress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                WindowsLookAndFeel.setMnemonicHidden(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                JMenuBar mbar = root != null ? root.getJMenuBar() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if(mbar == null && winAncestor instanceof JFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    mbar = ((JFrame)winAncestor).getJMenuBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                JMenu menu = mbar != null ? mbar.getMenu(0) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if(menu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    ev.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        void altReleased(KeyEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (menuCanceledOnPress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                WindowsLookAndFeel.setMnemonicHidden(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            MenuSelectionManager msm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (msm.getSelectedPath().length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // if no menu is active, we try activating the menubar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                JMenuBar mbar = root != null ? root.getJMenuBar() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if(mbar == null && winAncestor instanceof JFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    mbar = ((JFrame)winAncestor).getJMenuBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                JMenu menu = mbar != null ? mbar.getMenu(0) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   130
                // It might happen that the altRelease event is processed
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   131
                // with a reasonable delay since it has been generated.
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   132
                // Here we check the last deactivation time of the containing
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   133
                // window. If this time appears to be greater than the altRelease
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   134
                // event time the event is skipped to avoid unexpected menu
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   135
                // activation. See 7121442.
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   136
                boolean skip = false;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   137
                Toolkit tk = Toolkit.getDefaultToolkit();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   138
                if (tk instanceof SunToolkit) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   139
                    skip = ev.getWhen() <= ((SunToolkit)tk).getWindowDeactivationTime(winAncestor);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   140
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   141
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 5506
diff changeset
   142
                if (menu != null && !skip) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    MenuElement[] path = new MenuElement[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    path[0] = mbar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    path[1] = menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    msm.setSelectedPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                } else if(!WindowsLookAndFeel.isMnemonicHidden()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    WindowsLookAndFeel.setMnemonicHidden(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if((msm.getSelectedPath())[0] instanceof ComboPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    WindowsLookAndFeel.setMnemonicHidden(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public boolean postProcessKeyEvent(KeyEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if(ev.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // do not manage consumed event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (ev.getKeyCode() == KeyEvent.VK_ALT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                root = SwingUtilities.getRootPane(ev.getComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                winAncestor = (root == null ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        SwingUtilities.getWindowAncestor(root));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (ev.getID() == KeyEvent.KEY_PRESSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    if (!altKeyPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        altPressed(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    altKeyPressed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    if (altKeyPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        altReleased(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        MenuSelectionManager msm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        MenuElement[] path = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        if (path.length <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            WindowsLookAndFeel.setMnemonicHidden(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    altKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                winAncestor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                altKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}