jdk/src/share/classes/javax/swing/plaf/basic/BasicMenuUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1290 da8902cd496c
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A default L&F implementation of MenuUI.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * is a "combined" view/controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class BasicMenuUI extends BasicMenuItemUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected ChangeListener         changeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected MenuListener           menuListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int lastMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Uses as the parent of the windowInputMap when selected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private InputMap selectedWindowInputMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* diagnostic aids -- should be false for production builds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final boolean TRACE =   false; // trace creates and disposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final boolean VERBOSE = false; // show reuse hits/misses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final boolean DEBUG =   false;  // show bad params, misc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static boolean crossMenuMnemonic = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return new BasicMenuUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        BasicMenuItemUI.loadActionMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        map.put(new Actions(Actions.SELECT, null, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        super.installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        updateDefaultBackgroundColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ((JMenu)menuItem).setDelay(200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        crossMenuMnemonic = UIManager.getBoolean("Menu.crossMenuMnemonic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return "Menu";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (changeListener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            changeListener = createChangeListener(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (changeListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            menuItem.addChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (menuListener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            menuListener = createMenuListener(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (menuListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ((JMenu)menuItem).addMenuListener(menuListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super.installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        updateMnemonicBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    void installLazyActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        LazyActionMap.installLazyActionMap(menuItem, BasicMenuUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                           getPropertyPrefix() + ".actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void updateMnemonicBinding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int mnemonic = menuItem.getModel().getMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int[] shortcutKeys = (int[])DefaultLookup.get(menuItem, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                   "Menu.shortcutKeys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (shortcutKeys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            shortcutKeys = new int[] {KeyEvent.ALT_MASK};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (mnemonic == lastMnemonic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        InputMap windowInputMap = SwingUtilities.getUIInputMap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                       menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (lastMnemonic != 0 && windowInputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            for (int i=0; i<shortcutKeys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                windowInputMap.remove(KeyStroke.getKeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                      (lastMnemonic, shortcutKeys[i], false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (mnemonic != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (windowInputMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                windowInputMap = createInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                              WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                SwingUtilities.replaceUIInputMap(menuItem, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                       WHEN_IN_FOCUSED_WINDOW, windowInputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            for (int i=0; i<shortcutKeys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                         shortcutKeys[i], false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                   "selectMenu");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        lastMnemonic = mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super.uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        lastMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected MouseInputListener createMouseInputListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected MenuListener createMenuListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected ChangeListener createChangeListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected PropertyChangeListener createPropertyChangeListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    BasicMenuItemUI.Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        menuItem.setArmed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        menuItem.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        menuItem.resetKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        super.uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (changeListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            menuItem.removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (menuListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            ((JMenu)menuItem).removeMenuListener(menuListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        changeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        menuListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected MenuKeyListener createMenuKeyListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return (MenuKeyListener)getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (((JMenu)menuItem).isTopLevelMenu() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            Dimension d = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return new Dimension(d.width, Short.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    protected void setupPostTimer(JMenu menu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Timer timer = new Timer(menu.getDelay(), new Actions(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                    Actions.SELECT, menu,false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        timer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static void appendPath(MenuElement[] path, MenuElement elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        MenuElement newPath[] = new MenuElement[path.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        System.arraycopy(path, 0, newPath, 0, path.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        newPath[path.length] = elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        MenuSelectionManager.defaultManager().setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private static final String SELECT = "selectMenu";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // NOTE: This will be null if the action is registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // ActionMap. For the timer use it will be non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        private JMenu menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        private boolean force=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Actions(String key, JMenu menu, boolean shouldForce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.menu = menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            this.force = shouldForce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        private JMenu getMenu(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (e.getSource() instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return (JMenu)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            JMenu menu = getMenu(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (!crossMenuMnemonic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                JPopupMenu pm = BasicPopupMenuUI.getLastPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (pm != null && pm != menu.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if(force) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                Container cnt = menu.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if(cnt != null && cnt instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    MenuElement me[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    MenuElement subElements[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    subElements = menu.getPopupMenu().getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    if(subElements.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        me = new MenuElement[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        me[0] = (MenuElement) cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        me[1] = (MenuElement) menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        me[2] = (MenuElement) menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        me[3] = subElements[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        me = new MenuElement[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        me[0] = (MenuElement)cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        me[1] = menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        me[2] = (MenuElement) menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    defaultManager.setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                MenuElement path[] = defaultManager.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if(path.length > 0 && path[path.length-1] == menu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    appendPath(path, menu.getPopupMenu());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public boolean isEnabled(Object c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (c instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return ((JMenu)c).isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Set the background color depending on whether this is a toplevel menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * in a menubar or a submenu of another menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private void updateDefaultBackgroundColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (!UIManager.getBoolean("Menu.useMenuBarBackgroundForTopLevel")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        JMenu menu = (JMenu)menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (menu.getBackground() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (menu.isTopLevelMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                menu.setBackground(UIManager.getColor("MenuBar.background"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                menu.setBackground(UIManager.getColor(getPropertyPrefix() + ".background"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Instantiated and used by a menu item to handle the current menu selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * from mouse events. A MouseInputHandler processes and forwards all mouse events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * to a shared instance of the MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * This class is protected so that it can be subclassed by other look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * feels to implement their own mouse handling behavior. All overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * methods should call the parent methods so that the menu selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * is correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see javax.swing.MenuSelectionManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    protected class MouseInputHandler implements MouseInputListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // NOTE: This class exists only for backward compatability. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            getHandler().mouseClicked(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * Invoked when the mouse has been clicked on the menu. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * method clears or sets the selection path of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * Invoked when the mouse has been released on the menu. Delegates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         * mouse event to the MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * Invoked when the cursor enters the menu. This method sets the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * path for the MenuSelectionManager and handles the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * in which a menu item is used to pop up an additional menu, as in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * hierarchical menu system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * @param e the mouse event; not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            getHandler().mouseEntered(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            getHandler().mouseExited(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         * Invoked when a mouse button is pressed on the menu and then dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * Delegates the mouse event to the MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * @see java.awt.event.MouseMotionListener#mouseDragged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            getHandler().mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            getHandler().mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * As of Java 2 platform 1.4, this previously undocumented class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * is now obsolete. KeyBindings are now managed by the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public class ChangeHandler implements ChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        public JMenu    menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public BasicMenuUI ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public boolean  isSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public Component wasFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        public ChangeHandler(JMenu m, BasicMenuUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            menu = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            this.ui = ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        public void stateChanged(ChangeEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private class Handler extends BasicMenuItemUI.Handler implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            MenuKeyListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (e.getPropertyName() == AbstractButton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                             MNEMONIC_CHANGED_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                updateMnemonicBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if (e.getPropertyName().equals("ancestor")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    updateDefaultBackgroundColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                super.propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        // MouseInputListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * Invoked when the mouse has been clicked on the menu. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * method clears or sets the selection path of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         * MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            JMenu menu = (JMenu)menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (!menu.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            MenuSelectionManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if(menu.isTopLevelMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if(menu.isSelected() && menu.getPopupMenu().isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    Container cnt = menu.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    if(cnt != null && cnt instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        MenuElement me[] = new MenuElement[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        me[0]=(MenuElement)cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        me[1]=menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        manager.setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            MenuElement selectedPath[] = manager.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (selectedPath.length > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                selectedPath[selectedPath.length-1] != menu.getPopupMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                if(menu.isTopLevelMenu() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                   menu.getDelay() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    appendPath(selectedPath, menu.getPopupMenu());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    setupPostTimer(menu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * Invoked when the mouse has been released on the menu. Delegates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * mouse event to the MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            JMenu menu = (JMenu)menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (!menu.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            MenuSelectionManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            manager.processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            if (!e.isConsumed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * Invoked when the cursor enters the menu. This method sets the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * path for the MenuSelectionManager and handles the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * in which a menu item is used to pop up an additional menu, as in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * hierarchical menu system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * @param e the mouse event; not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            JMenu menu = (JMenu)menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // only disable the menu highlighting if it's disabled and the property isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            // true. This allows disabled rollovers to work in WinL&F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (!menu.isEnabled() && !UIManager.getBoolean("MenuItem.disabledAreNavigable")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            MenuSelectionManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            MenuElement selectedPath[] = manager.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (!menu.isTopLevelMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                if(!(selectedPath.length > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                     selectedPath[selectedPath.length-1] ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                     menu.getPopupMenu())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    if(menu.getDelay() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        appendPath(getPath(), menu.getPopupMenu());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        manager.setSelectedPath(getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        setupPostTimer(menu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    }
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
                if(selectedPath.length > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                   selectedPath[0] == menu.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    MenuElement newPath[] = new MenuElement[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    // A top level menu's parent is by definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    // a JMenuBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    newPath[0] = (MenuElement)menu.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    newPath[1] = menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    if (BasicPopupMenuUI.getLastPopup() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        newPath[2] = menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    manager.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         * Invoked when a mouse button is pressed on the menu and then dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * Delegates the mouse event to the MenuSelectionManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * @see java.awt.event.MouseMotionListener#mouseDragged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            JMenu menu = (JMenu)menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (!menu.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            MenuSelectionManager.defaultManager().processMouseEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // MenuDragHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        public void menuDragMouseEntered(MenuDragMouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        public void menuDragMouseDragged(MenuDragMouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (menuItem.isEnabled() == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            MenuSelectionManager manager = e.getMenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            MenuElement path[] = e.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if(p.x >= 0 && p.x < menuItem.getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
               p.y >= 0 && p.y < menuItem.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                JMenu menu = (JMenu)menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                MenuElement selectedPath[] = manager.getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                if(!(selectedPath.length > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                     selectedPath[selectedPath.length-1] ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                     menu.getPopupMenu())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    if(menu.isTopLevelMenu() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                       menu.getDelay() == 0  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                       e.getID() == MouseEvent.MOUSE_DRAGGED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        appendPath(path, menu.getPopupMenu());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        manager.setSelectedPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        setupPostTimer(menu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            } else if(e.getID() == MouseEvent.MOUSE_RELEASED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                Component comp = manager.componentForPoint(e.getComponent(), e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                if (comp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        public void menuDragMouseExited(MenuDragMouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        public void menuDragMouseReleased(MenuDragMouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // MenuKeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * Open the Menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        public void menuKeyTyped(MenuKeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (!crossMenuMnemonic && BasicPopupMenuUI.getLastPopup() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // when crossMenuMnemonic is not set, we don't open a toplevel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // menu if another toplevel menu is already open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            char key = Character.toLowerCase((char)menuItem.getMnemonic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            MenuElement path[] = e.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            MenuSelectionManager manager = e.getMenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            if (key == Character.toLowerCase(e.getKeyChar())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                JPopupMenu popupMenu = ((JMenu)menuItem).getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                ArrayList newList = new ArrayList(Arrays.asList(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                newList.add(popupMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                MenuElement subs[] = popupMenu.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                MenuElement sub =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        BasicPopupMenuUI.findEnabledChild(subs, -1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                if(sub != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    newList.add(sub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                MenuElement newPath[] = new MenuElement[0];;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                newPath = (MenuElement[]) newList.toArray(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                manager.setSelectedPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            } else if (((JMenu)menuItem).isTopLevelMenu()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    && BasicPopupMenuUI.getLastPopup() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                manager.clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        public void menuKeyPressed(MenuKeyEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        public void menuKeyReleased(MenuKeyEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
}