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