jdk/src/share/classes/javax/swing/plaf/basic/BasicPopupMenuUI.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3974 711cc3e0bd63
child 20169 d7fa6d7586c9
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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: 3974
diff changeset
     2
 * Copyright (c) 1997, 2008, 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: 3974
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: 3974
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: 3974
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3974
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3974
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 javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.KeyboardFocusManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * A Windows L&F implementation of PopupMenuUI.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * is a "combined" view/controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class BasicPopupMenuUI extends PopupMenuUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final StringBuilder MOUSE_GRABBER_KEY = new StringBuilder(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                   "javax.swing.plaf.basic.BasicPopupMenuUI.MouseGrabber");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final StringBuilder MENU_KEYBOARD_HELPER_KEY = new StringBuilder(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                   "javax.swing.plaf.basic.BasicPopupMenuUI.MenuKeyboardHelper");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected JPopupMenu popupMenu = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private transient PopupMenuListener popupMenuListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private MenuKeyListener menuKeyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static boolean checkedUnpostPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static boolean unpostPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return new BasicPopupMenuUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public BasicPopupMenuUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        BasicLookAndFeel.needsEventHelper = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        LookAndFeel laf = UIManager.getLookAndFeel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (laf instanceof BasicLookAndFeel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ((BasicLookAndFeel)laf).installAWTEventListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        popupMenu = (JPopupMenu) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (popupMenu.getLayout() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            popupMenu.getLayout() instanceof UIResource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            popupMenu.setLayout(new DefaultMenuLayout(popupMenu, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        LookAndFeel.installProperty(popupMenu, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        LookAndFeel.installBorder(popupMenu, "PopupMenu.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        LookAndFeel.installColorsAndFont(popupMenu,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                         "PopupMenu.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                         "PopupMenu.foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                         "PopupMenu.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (popupMenuListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            popupMenuListener = new BasicPopupMenuListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        popupMenu.addPopupMenuListener(popupMenuListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (menuKeyListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            menuKeyListener = new BasicMenuKeyListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        popupMenu.addMenuKeyListener(menuKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        AppContext context = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized (MOUSE_GRABBER_KEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            MouseGrabber mouseGrabber = (MouseGrabber)context.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                                     MOUSE_GRABBER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (mouseGrabber == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                mouseGrabber = new MouseGrabber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                context.put(MOUSE_GRABBER_KEY, mouseGrabber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        synchronized (MENU_KEYBOARD_HELPER_KEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            MenuKeyboardHelper helper =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    (MenuKeyboardHelper)context.get(MENU_KEYBOARD_HELPER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (helper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                helper = new MenuKeyboardHelper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                context.put(MENU_KEYBOARD_HELPER_KEY, helper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                msm.addChangeListener(helper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static InputMap getInputMap(JPopupMenu popup, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        InputMap windowInputMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Object[] bindings = (Object[])UIManager.get("PopupMenu.selectedWindowInputMapBindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (bindings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            windowInputMap = LookAndFeel.makeComponentInputMap(c, bindings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (!popup.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                Object[] km = (Object[])UIManager.get("PopupMenu.selectedWindowInputMapBindings.RightToLeft");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (km != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    InputMap rightToLeftInputMap = LookAndFeel.makeComponentInputMap(c, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    rightToLeftInputMap.setParent(windowInputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    windowInputMap = rightToLeftInputMap;
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
        return windowInputMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static ActionMap getActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return LazyActionMap.getActionMap(BasicPopupMenuUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                          "PopupMenu.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        map.put(new Actions(Actions.CANCEL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        map.put(new Actions(Actions.SELECT_NEXT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        map.put(new Actions(Actions.SELECT_PREVIOUS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        map.put(new Actions(Actions.SELECT_PARENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        map.put(new Actions(Actions.SELECT_CHILD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        map.put(new Actions(Actions.RETURN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        BasicLookAndFeel.installAudioActionMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        popupMenu = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        LookAndFeel.uninstallBorder(popupMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (popupMenuListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            popupMenu.removePopupMenuListener(popupMenuListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (menuKeyListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            popupMenu.removeMenuKeyListener(menuKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        SwingUtilities.replaceUIActionMap(popupMenu, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        SwingUtilities.replaceUIInputMap(popupMenu,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                  JComponent.WHEN_IN_FOCUSED_WINDOW, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static MenuElement getFirstPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        MenuElement[] p = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        MenuElement me = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        for(int i = 0 ; me == null && i < p.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (p[i] instanceof JPopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                me = p[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static JPopupMenu getLastPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        MenuElement[] p = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        JPopupMenu popup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for(int i = p.length - 1; popup == null && i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (p[i] instanceof JPopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                popup = (JPopupMenu)p[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   228
    static List<JPopupMenu> getPopups() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        MenuElement[] p = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   232
        List<JPopupMenu> list = new ArrayList<JPopupMenu>(p.length);
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   233
        for (MenuElement element : p) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   234
            if (element instanceof JPopupMenu) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   235
                list.add((JPopupMenu) element);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public boolean isPopupTrigger(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return ((e.getID()==MouseEvent.MOUSE_RELEASED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                && ((e.getModifiers() & MouseEvent.BUTTON3_MASK)!=0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private static boolean checkInvokerEqual(MenuElement present, MenuElement last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Component invokerPresent = present.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Component invokerLast = last.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (invokerPresent instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            invokerPresent = ((JPopupMenu)invokerPresent).getInvoker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (invokerLast instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            invokerLast = ((JPopupMenu)invokerLast).getInvoker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return (invokerPresent == invokerLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * This Listener fires the Action that provides the correct auditory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * feedback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private class BasicPopupMenuListener implements PopupMenuListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        public void popupMenuCanceled(PopupMenuEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            BasicLookAndFeel.playSound((JPopupMenu)e.getSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                       "PopupMenu.popupSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Handles mnemonic for children JMenuItems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private class BasicMenuKeyListener implements MenuKeyListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        MenuElement menuToOpen = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public void menuKeyTyped(MenuKeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (menuToOpen != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                // we have a submenu to open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                JPopupMenu subpopup = ((JMenu)menuToOpen).getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                MenuElement subitem = findEnabledChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        subpopup.getSubElements(), -1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   293
                ArrayList<MenuElement> lst = new ArrayList<MenuElement>(Arrays.asList(e.getPath()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                lst.add(menuToOpen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                lst.add(subpopup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (subitem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    lst.add(subitem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   299
                MenuElement newPath[] = new MenuElement[0];
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   300
                newPath = lst.toArray(newPath);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                MenuSelectionManager.defaultManager().setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            menuToOpen = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public void menuKeyPressed(MenuKeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            char keyChar = e.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // Handle the case for Escape or Enter...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (!Character.isLetterOrDigit(keyChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            MenuSelectionManager manager = e.getMenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            MenuElement path[] = e.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            MenuElement items[] = popupMenu.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            int currentIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            int matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            int firstMatch = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            int indexes[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            for (int j = 0; j < items.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (! (items[j] instanceof JMenuItem)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                JMenuItem item = (JMenuItem)items[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                int mnemonic = item.getMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (item.isEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    item.isVisible() && lower(keyChar) == lower(mnemonic)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    if (matches == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        firstMatch = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        if (indexes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            indexes = new int[items.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            indexes[0] = firstMatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        indexes[matches++] = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
3974
711cc3e0bd63 6827786: Mnemonic cycling for multiple equal mnemonic armed menu items stops when encountering a submenu
peterz
parents: 1639
diff changeset
   342
                if (item.isArmed() || item.isSelected()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    currentIndex = matches - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (matches == 0) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   348
                // no op
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } else if (matches == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                // Invoke the menu action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                JMenuItem item = (JMenuItem)items[firstMatch];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (item instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    // submenus are handled in menuKeyTyped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    menuToOpen = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                } else if (item.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    // we have a menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    item.doClick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                // Select the menu item with the matching mnemonic. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                // the same mnemonic has been invoked then select the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // menu item in the cycle.
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   365
                MenuElement newItem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                newItem = items[indexes[(currentIndex + 1) % matches]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                MenuElement newPath[] = new MenuElement[path.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                System.arraycopy(path, 0, newPath, 0, path.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                newPath[path.length] = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                manager.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public void menuKeyReleased(MenuKeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        private char lower(char keyChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return Character.toLowerCase(keyChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        private char lower(int mnemonic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return Character.toLowerCase((char) mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Types of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private static final String CANCEL = "cancel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        private static final String SELECT_NEXT = "selectNext";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        private static final String SELECT_PREVIOUS = "selectPrevious";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        private static final String SELECT_PARENT = "selectParent";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        private static final String SELECT_CHILD = "selectChild";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        private static final String RETURN = "return";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // Used for next/previous actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        private static final boolean FORWARD = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        private static final boolean BACKWARD = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // Used for parent/child actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        private static final boolean PARENT = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        private static final boolean CHILD = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (key == CANCEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            else if (key == SELECT_NEXT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                selectItem(FORWARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            else if (key == SELECT_PREVIOUS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                selectItem(BACKWARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            else if (key == SELECT_PARENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                selectParentChild(PARENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            else if (key == SELECT_CHILD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                selectParentChild(CHILD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            else if (key == RETURN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                doReturn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        private void doReturn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            KeyboardFocusManager fmgr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            Component focusOwner = fmgr.getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            MenuElement path[] = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            MenuElement lastElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            if(path.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                lastElement = path[path.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if(lastElement instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    MenuElement newPath[] = new MenuElement[path.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    System.arraycopy(path,0,newPath,0,path.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                } else if(lastElement instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    JMenuItem mi = (JMenuItem)lastElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    if (mi.getUI() instanceof BasicMenuItemUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        ((BasicMenuItemUI)mi.getUI()).doClick(msm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        msm.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        mi.doClick(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        private void selectParentChild(boolean direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            MenuElement path[] = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            int len = path.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (direction == PARENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                // selecting parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                int popupIndex = len-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (len > 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    // check if we have an open submenu. A submenu item may or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    // may not be selected, so submenu popup can be either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    // last or next to the last item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    (path[popupIndex] instanceof JPopupMenu ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                     path[--popupIndex] instanceof JPopupMenu) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    !((JMenu)path[popupIndex-1]).isTopLevelMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    // we have a submenu, just close it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    MenuElement newPath[] = new MenuElement[popupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    System.arraycopy(path, 0, newPath, 0, popupIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                // selecting child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                if (len > 0 && path[len-1] instanceof JMenu &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    !((JMenu)path[len-1]).isTopLevelMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    // we have a submenu, open it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    JMenu menu = (JMenu)path[len-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    JPopupMenu popup = menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    MenuElement[] subs = popup.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    MenuElement item = findEnabledChild(subs, -1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    MenuElement[] newPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    if (item == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        newPath = new MenuElement[len+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        newPath = new MenuElement[len+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        newPath[len+1] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    System.arraycopy(path, 0, newPath, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    newPath[len] = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // check if we have a toplevel menu selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            // If this is the case, we select another toplevel menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if (len > 1 && path[0] instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                MenuElement currentMenu = path[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                MenuElement nextMenu = findEnabledChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    path[0].getSubElements(), currentMenu, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                if (nextMenu != null && nextMenu != currentMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    MenuElement newSelection[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    if (len == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        // menu is selected but its popup not shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        newSelection = new MenuElement[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        newSelection[0] = path[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        newSelection[1] = nextMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        // menu is selected and its popup is shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        newSelection = new MenuElement[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        newSelection[0] = path[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        newSelection[1] = nextMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        newSelection[2] = ((JMenu)nextMenu).getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    msm.setSelectedPath(newSelection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        private void selectItem(boolean direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            MenuElement path[] = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if (path.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            int len = path.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            if (len == 1 && path[0] instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                JPopupMenu popup = (JPopupMenu) path[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                MenuElement[] newPath = new MenuElement[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                newPath[0] = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                newPath[1] = findEnabledChild(popup.getSubElements(), -1, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            } else if (len == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    path[0] instanceof JMenuBar && path[1] instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                // a toplevel menu is selected, but its popup not shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // Show the popup and select the first item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                JPopupMenu popup = ((JMenu)path[1]).getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                MenuElement next =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    findEnabledChild(popup.getSubElements(), -1, FORWARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                MenuElement[] newPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    // an enabled item found -- include it in newPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    newPath = new MenuElement[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    newPath[3] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    // menu has no enabled items -- still must show the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    newPath = new MenuElement[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                System.arraycopy(path, 0, newPath, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                newPath[2] = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            } else if (path[len-1] instanceof JPopupMenu &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                       path[len-2] instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                // a menu (not necessarily toplevel) is open and its popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                // shown. Select the appropriate menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                JMenu menu = (JMenu)path[len-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                JPopupMenu popup = menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                MenuElement next =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    findEnabledChild(popup.getSubElements(), -1, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    MenuElement[] newPath = new MenuElement[len+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    System.arraycopy(path, 0, newPath, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    newPath[len] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    // all items in the popup are disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    // We're going to find the parent popup menu and select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    // its next item. If there's no parent popup menu (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    // current menu is toplevel), do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    if (len > 2 && path[len-3] instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        popup = ((JPopupMenu)path[len-3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        next = findEnabledChild(popup.getSubElements(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                                menu, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        if (next != null && next != menu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                            MenuElement[] newPath = new MenuElement[len-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                            System.arraycopy(path, 0, newPath, 0, len-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                            newPath[len-2] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                            msm.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                // just select the next item, no path expansion needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                MenuElement subs[] = path[len-2].getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                MenuElement nextChild =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    findEnabledChild(subs, path[len-1], direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (nextChild == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    nextChild = findEnabledChild(subs, -1, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                if (nextChild != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    path[len-1] = nextChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    msm.setSelectedPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        private void cancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            // 4234793: This action should call JPopupMenu.firePopupMenuCanceled but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // a protected method. The real solution could be to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // firePopupMenuCanceled public and call it directly.
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   627
            JPopupMenu lastPopup = getLastPopup();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (lastPopup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                lastPopup.putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            String mode = UIManager.getString("Menu.cancelMode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            if ("hideMenuTree".equals(mode)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                MenuSelectionManager.defaultManager().clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                shortenSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        private void shortenSelectedPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            MenuElement path[] = MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (path.length <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                MenuSelectionManager.defaultManager().clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            // unselect MenuItem and its Popup by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            int value = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            MenuElement lastElement = path[path.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            JPopupMenu lastPopup = getLastPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (lastElement == lastPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                MenuElement previousElement = path[path.length - 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                if (previousElement instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    JMenu lastMenu = (JMenu) previousElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    if (lastMenu.isEnabled() && lastPopup.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        // unselect the last visible popup only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        value = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        // unselect invisible popup and two visible elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        value = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (path.length - value <= 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    && !UIManager.getBoolean("Menu.preserveTopLevelSelection")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                // clear selection for the topLevelMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                value = path.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            MenuElement newPath[] = new MenuElement[path.length - value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            System.arraycopy(path, 0, newPath, 0, path.length - value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            MenuSelectionManager.defaultManager().setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    private static MenuElement nextEnabledChild(MenuElement e[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                                int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        for (int i=fromIndex; i<=toIndex; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            if (e[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                Component comp = e[i].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                if ( comp != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        && (comp.isEnabled() || UIManager.getBoolean("MenuItem.disabledAreNavigable"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        && comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    return e[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    private static MenuElement previousEnabledChild(MenuElement e[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                                int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        for (int i=fromIndex; i>=toIndex; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (e[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                Component comp = e[i].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                if ( comp != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        && (comp.isEnabled() || UIManager.getBoolean("MenuItem.disabledAreNavigable"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                        && comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    return e[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    static MenuElement findEnabledChild(MenuElement e[], int fromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                                boolean forward) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   705
        MenuElement result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            result = nextEnabledChild(e, fromIndex+1, e.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (result == null) result = nextEnabledChild(e, 0, fromIndex-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            result = previousEnabledChild(e, fromIndex-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (result == null) result = previousEnabledChild(e, e.length-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                                              fromIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    static MenuElement findEnabledChild(MenuElement e[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                   MenuElement elem, boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        for (int i=0; i<e.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (e[i] == elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                return findEnabledChild(e, i, forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    static class MouseGrabber implements ChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        AWTEventListener, ComponentListener, WindowListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        Window grabbedWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        MenuElement[] lastPathSelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        public MouseGrabber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            msm.addChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            this.lastPathSelected = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if(this.lastPathSelected.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                grabWindow(this.lastPathSelected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        void uninstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            synchronized (MOUSE_GRABBER_KEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                MenuSelectionManager.defaultManager().removeChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                ungrabWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                AppContext.getAppContext().remove(MOUSE_GRABBER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        void grabWindow(MenuElement[] newPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            // A grab needs to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            final Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            java.security.AccessController.doPrivileged(
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   754
                new java.security.PrivilegedAction<Object>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                        tk.addAWTEventListener(MouseGrabber.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                AWTEvent.MOUSE_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                AWTEvent.MOUSE_MOTION_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                AWTEvent.MOUSE_WHEEL_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                AWTEvent.WINDOW_EVENT_MASK | sun.awt.SunToolkit.GRAB_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            Component invoker = newPath[0].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            if (invoker instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                invoker = ((JPopupMenu)invoker).getInvoker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            grabbedWindow = invoker instanceof Window?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    (Window)invoker :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    SwingUtilities.getWindowAncestor(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if(grabbedWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                if(tk instanceof sun.awt.SunToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    ((sun.awt.SunToolkit)tk).grab(grabbedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    grabbedWindow.addComponentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    grabbedWindow.addWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        void ungrabWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            final Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            // The grab should be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
             java.security.AccessController.doPrivileged(
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   787
                new java.security.PrivilegedAction<Object>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        tk.removeAWTEventListener(MouseGrabber.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            realUngrabWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        void realUngrabWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if(grabbedWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                if(tk instanceof sun.awt.SunToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    ((sun.awt.SunToolkit)tk).ungrab(grabbedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    grabbedWindow.removeComponentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    grabbedWindow.removeWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                grabbedWindow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            MenuSelectionManager msm = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            MenuElement[] p = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            if (lastPathSelected.length == 0 && p.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                grabWindow(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            if (lastPathSelected.length != 0 && p.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                ungrabWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            lastPathSelected = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public void eventDispatched(AWTEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if(ev instanceof sun.awt.UngrabEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                // Popup should be canceled in case of ungrab event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                cancelPopupMenu( );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (!(ev instanceof MouseEvent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                // We are interested in MouseEvents only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            MouseEvent me = (MouseEvent) ev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            Component src = me.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            switch (me.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                if (isInPopup(src) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    (src instanceof JMenu && ((JMenu)src).isSelected())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                if (!(src instanceof JComponent) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                   ! (((JComponent)src).getClientProperty("doNotCancelPopup")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                         == BasicComboBoxUI.HIDE_POPUP_KEY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    // Cancel popup only if this property was not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    // If this property is set to TRUE component wants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    // to deal with this event by himself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    // Ask UIManager about should we consume event that closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    // popup. This made to match native apps behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    boolean consumeEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        UIManager.getBoolean("PopupMenu.consumeEventOnClose");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    // Consume the event so that normal processing stops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    if(consumeEvent && !(src instanceof MenuElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        me.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                if(!(src instanceof MenuElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    // Do not forward event to MSM, let component handle it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    if (isInPopup(src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                if(src instanceof JMenu || !(src instanceof JMenuItem)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    MenuSelectionManager.defaultManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        processMouseEvent(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                if(!(src instanceof MenuElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    // For the MOUSE_DRAGGED event the src is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    // the Component in which mouse button was pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    // If the src is in popupMenu,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    // do not forward event to MSM, let component handle it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    if (isInPopup(src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                MenuSelectionManager.defaultManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    processMouseEvent(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            case MouseEvent.MOUSE_WHEEL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                if (isInPopup(src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        boolean isInPopup(Component src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            for (Component c=src; c!=null; c=c.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                if (c instanceof Applet || c instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                } else if (c instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        void cancelPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            // We should ungrab window if a user code throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            // an unexpected runtime exception. See 6495920.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                // 4234793: This action should call firePopupMenuCanceled but it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                // a protected method. The real solution could be to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                // firePopupMenuCanceled public and call it directly.
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   913
                List<JPopupMenu> popups = getPopups();
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   914
                for (JPopupMenu popup : popups) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                    popup.putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                MenuSelectionManager.defaultManager().clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            } catch (RuntimeException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                realUngrabWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            } catch (Error err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                realUngrabWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                throw err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        public void componentResized(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        public void componentMoved(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        public void componentShown(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        public void componentHidden(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        public void windowClosed(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        public void windowIconified(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        public void windowDeactivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            cancelPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        public void windowOpened(WindowEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        public void windowDeiconified(WindowEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        public void windowActivated(WindowEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * This helper is added to MenuSelectionManager as a ChangeListener to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * listen to menu selection changes. When a menu is activated, it passes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * focus to its parent JRootPane, and installs an ActionMap/InputMap pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * on that JRootPane. Those maps are necessary in order for menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * navigation to work. When menu is being deactivated, it restores focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * to the component that has had it before menu activation, and uninstalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * the maps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * This helper is also installed as a KeyListener on root pane when menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * is active. It forwards key events to MenuSelectionManager for mnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * keys handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    static class MenuKeyboardHelper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        implements ChangeListener, KeyListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        private Component lastFocused = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        private MenuElement[] lastPathSelected = new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        private JPopupMenu lastPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        private JRootPane invokerRootPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        private ActionMap menuActionMap = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        private InputMap menuInputMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        private boolean focusTraversalKeysEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         * Fix for 4213634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         * If this is false, KEY_TYPED and KEY_RELEASED events are NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         * processed. This is needed to avoid activating a menuitem when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         * the menu and menuitem share the same mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        private boolean receivedKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        void removeItems() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            if (lastFocused != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                if(!lastFocused.requestFocusInWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    // Workarounr for 4810575.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    // If lastFocused is not in currently focused window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    // requestFocusInWindow will fail. In this case we must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    // request focus by requestFocus() if it was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    // transferred from our popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    Window cfw = KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                                 .getCurrentKeyboardFocusManager()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                                  .getFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    if(cfw != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                       "###focusableSwingPopup###".equals(cfw.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                        lastFocused.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                lastFocused = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            if (invokerRootPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                invokerRootPane.removeKeyListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                invokerRootPane.setFocusTraversalKeysEnabled(focusTraversalKeysEnabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                removeUIInputMap(invokerRootPane, menuInputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                removeUIActionMap(invokerRootPane, menuActionMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                invokerRootPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            receivedKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        private FocusListener rootPaneFocusListener = new FocusAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                public void focusGained(FocusEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    Component opposite = ev.getOppositeComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    if (opposite != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                        lastFocused = opposite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    ev.getComponent().removeFocusListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         * Return the last JPopupMenu in <code>path</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
         * or <code>null</code> if none found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        JPopupMenu getActivePopup(MenuElement[] path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            for (int i=path.length-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                MenuElement elem = path[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                if (elem instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                    return (JPopupMenu)elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        void addUIInputMap(JComponent c, InputMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            InputMap lastNonUI = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            InputMap parent = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            while (parent != null && !(parent instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                lastNonUI = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if (lastNonUI == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                lastNonUI.setParent(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            map.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        void addUIActionMap(JComponent c, ActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            ActionMap lastNonUI = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            ActionMap parent = c.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            while (parent != null && !(parent instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                lastNonUI = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            if (lastNonUI == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                c.setActionMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                lastNonUI.setParent(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            map.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        void removeUIInputMap(JComponent c, InputMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            InputMap im = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            InputMap parent = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            while (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                if (parent == map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    if (im == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                                      map.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                        im.setParent(map.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                im = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        void removeUIActionMap(JComponent c, ActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            ActionMap im = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            ActionMap parent = c.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            while (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                if (parent == map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    if (im == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                        c.setActionMap(map.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        im.setParent(map.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                im = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        public void stateChanged(ChangeEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            if (!(UIManager.getLookAndFeel() instanceof BasicLookAndFeel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                uninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            MenuElement[] p = msm.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            JPopupMenu popup = getActivePopup(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            if (popup != null && !popup.isFocusable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                // Do nothing for non-focusable popups
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            if (lastPathSelected.length != 0 && p.length != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                if (!checkInvokerEqual(p[0],lastPathSelected[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    removeItems();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    lastPathSelected = new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            if (lastPathSelected.length == 0 && p.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                // menu posted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                JComponent invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                if (popup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    if (p.length == 2 && p[0] instanceof JMenuBar &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                        p[1] instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                        // a menu has been selected but not open
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                        invoker = (JComponent)p[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                        popup = ((JMenu)invoker).getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    Component c = popup.getInvoker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    if(c instanceof JFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                        invoker = ((JFrame)c).getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    } else if(c instanceof JDialog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                        invoker = ((JDialog)c).getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    } else if(c instanceof JApplet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                        invoker = ((JApplet)c).getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                        while (!(c instanceof JComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                            if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                            c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                        invoker = (JComponent)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                // remember current focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                lastFocused = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                // request focus on root pane and install keybindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                // used for menu navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                invokerRootPane = SwingUtilities.getRootPane(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                if (invokerRootPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                    invokerRootPane.addFocusListener(rootPaneFocusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    invokerRootPane.requestFocus(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    invokerRootPane.addKeyListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                    focusTraversalKeysEnabled = invokerRootPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                                      getFocusTraversalKeysEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    invokerRootPane.setFocusTraversalKeysEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    menuInputMap = getInputMap(popup, invokerRootPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    addUIInputMap(invokerRootPane, menuInputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                    addUIActionMap(invokerRootPane, menuActionMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            } else if (lastPathSelected.length != 0 && p.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                // menu hidden -- return focus to where it had been before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                // and uninstall menu keybindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                   removeItems();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                if (popup != lastPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                    receivedKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            // Remember the last path selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            lastPathSelected = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            lastPopup = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        public void keyPressed(KeyEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            receivedKeyPressed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            MenuSelectionManager.defaultManager().processKeyEvent(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        public void keyReleased(KeyEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            if (receivedKeyPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                receivedKeyPressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                MenuSelectionManager.defaultManager().processKeyEvent(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        public void keyTyped(KeyEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            if (receivedKeyPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                MenuSelectionManager.defaultManager().processKeyEvent(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        void uninstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            synchronized (MENU_KEYBOARD_HELPER_KEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                MenuSelectionManager.defaultManager().removeChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                AppContext.getAppContext().remove(MENU_KEYBOARD_HELPER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
}