jdk/src/share/classes/javax/swing/MenuSelectionManager.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 11268 f0e59c4852de
child 22584 eed64ee05369
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1301
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1301
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: 1301
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: 1301
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1301
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1301
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A MenuSelectionManager owns the selection in menu hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class MenuSelectionManager {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
    40
    private Vector<MenuElement> selection = new Vector<MenuElement>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /* diagnostic aids -- should be false for production builds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final boolean TRACE =   false; // trace creates and disposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final boolean VERBOSE = false; // show reuse hits/misses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final boolean DEBUG =   false;  // show bad params, misc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final StringBuilder MENU_SELECTION_MANAGER_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                       new StringBuilder("javax.swing.MenuSelectionManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Returns the default menu selection manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @return a MenuSelectionManager object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static MenuSelectionManager defaultManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        synchronized (MENU_SELECTION_MANAGER_KEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            AppContext context = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            MenuSelectionManager msm = (MenuSelectionManager)context.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                                 MENU_SELECTION_MANAGER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (msm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                msm = new MenuSelectionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                context.put(MENU_SELECTION_MANAGER_KEY, msm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return msm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Only one ChangeEvent is needed per button model instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * event's only state is the source property.  The source of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * generated is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Changes the selection in the menu hierarchy.  The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * in the array are sorted in order from the root menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * element to the currently selected menu element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Note that this method is public but is used by the look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * feel engine and should not be called by client applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param path  an array of <code>MenuElement</code> objects specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *        the selected path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void setSelectedPath(MenuElement[] path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int currentSelectionCount = selection.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int firstDifference = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if(path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            path = new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.out.print("Previous:  "); printMenuElementArray(getSelectedPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.print("New:  "); printMenuElementArray(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for(i=0,c=path.length;i<c;i++) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   103
            if (i < currentSelectionCount && selection.elementAt(i) == path[i])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                firstDifference++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for(i=currentSelectionCount - 1 ; i >= firstDifference ; i--) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   110
            MenuElement me = selection.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            selection.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            me.menuSelectionChanged(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for(i = firstDifference, c = path.length ; i < c ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (path[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                selection.addElement(path[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                path[i].menuSelectionChanged(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the path to the currently selected menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return an array of MenuElement objects representing the selected path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public MenuElement[] getSelectedPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        MenuElement res[] = new MenuElement[selection.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for(i=0,c=selection.size();i<c;i++)
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   134
            res[i] = selection.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Tell the menu selection to close and unselect all the menu components. Call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * when a choice has been made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void clearSelectedPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (selection.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            setSelectedPath(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Adds a ChangeListener to the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param l the listener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        listenerList.add(ChangeListener.class, l);
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
     * Removes a ChangeListener from the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param l the listener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * to this MenuSelectionManager with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public ChangeListener[] getChangeListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   175
        return listenerList.getListeners(ChangeListener.class);
2
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
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (listeners[i]==ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (changeEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * When a MenuElement receives an event from a MouseListener, it should never process the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * directly. Instead all MenuElements should call this method with the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param event  a MouseEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public void processMouseEvent(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int screenX,screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Point p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int i,c,j,d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Component mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Rectangle r2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int cWidth,cHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        MenuElement menuElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        MenuElement subElements[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        MenuElement path[];
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   216
        Vector<MenuElement> tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int selectionSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        p = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
459
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   220
        Component source = event.getComponent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
459
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   222
        if ((source != null) && !source.isShowing()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // This can happen if a mouseReleased removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // containing component -- bug 4146684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int type = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int modifiers = event.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if ((type==MouseEvent.MOUSE_ENTERED||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
             type==MouseEvent.MOUSE_EXITED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            && ((modifiers & (InputEvent.BUTTON1_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                              InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
459
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   238
        if (source != null) {
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   239
            SwingUtilities.convertPointToScreen(p, source);
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   240
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        screenX = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        screenY = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   245
        tmp = (Vector<MenuElement>)selection.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        selectionSize = tmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (i=selectionSize - 1;i >= 0 && success == false; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            menuElement = (MenuElement) tmp.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            subElements = menuElement.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            for (j = 0, d = subElements.length;j < d && success == false; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (subElements[j] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                mc = subElements[j].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if(!mc.isShowing())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if(mc instanceof JComponent) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   260
                    cWidth  = mc.getWidth();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   261
                    cHeight = mc.getHeight();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    r2 = mc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    cWidth  = r2.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    cHeight = r2.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                p.x = screenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                p.y = screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                SwingUtilities.convertPointFromScreen(p,mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                /** Send the event to visible menu element if menu element currently in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                 *  the selected path or contains the event location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                   (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    int k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    if(path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        path = new MenuElement[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        for(k=0;k<=i;k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            path[k] = (MenuElement)tmp.elementAt(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    path[i+1] = subElements[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    MenuElement currentSelection[] = getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    // Enter/exit detection -- needs tuning...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    if (currentSelection[currentSelection.length-1] !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        path[i+1] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        (currentSelection.length < 2 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                         currentSelection[currentSelection.length-2] !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                         path[i+1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        Component oldMC = currentSelection[currentSelection.length-1].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        MouseEvent exitEvent = new MouseEvent(oldMC, MouseEvent.MOUSE_EXITED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                                              event.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                                              event.getModifiers(), p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                                              event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                                              event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                                              event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                                              event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                                              MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        currentSelection[currentSelection.length-1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            processMouseEvent(exitEvent, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        MouseEvent enterEvent = new MouseEvent(mc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                                               MouseEvent.MOUSE_ENTERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                                               event.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                                               event.getModifiers(), p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                                               event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                                               event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                                               event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                                               event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                                               MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        subElements[j].processMouseEvent(enterEvent, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    MouseEvent mouseEvent = new MouseEvent(mc, event.getID(),event. getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                           event.getModifiers(), p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                                           event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                           event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                           event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                           event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                                           MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    subElements[j].processMouseEvent(mouseEvent, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    success = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    event.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private void printMenuElementArray(MenuElement path[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        printMenuElementArray(path, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private void printMenuElementArray(MenuElement path[], boolean dumpStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        System.out.println("Path is(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        for(i=0,j=path.length; i<j ;i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            for (int k=0; k<=i; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                System.out.print("  ");
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   340
            MenuElement me = path[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if(me instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                System.out.println(((JMenuItem)me).getText() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            } else if (me instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                System.out.println("JMenuBar, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } else if(me instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                System.out.println("JPopupMenu, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            } else if (me == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                System.out.println("NULL , ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                System.out.println("" + me + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        System.out.println(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (dumpStack == true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns the component in the currently selected path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * which contains sourcePoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param source The component in whose coordinate space sourcePoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *        is given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param sourcePoint The point which is being tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @return The component in the currently selected path which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         contains sourcePoint (relative to the source component's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *         coordinate space.  If sourcePoint is not inside a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         on the currently selected path, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public Component componentForPoint(Component source, Point sourcePoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int screenX,screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Point p = sourcePoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int i,c,j,d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Component mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        Rectangle r2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int cWidth,cHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        MenuElement menuElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        MenuElement subElements[];
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   380
        Vector<MenuElement> tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int selectionSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        SwingUtilities.convertPointToScreen(p,source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        screenX = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        screenY = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   388
        tmp = (Vector<MenuElement>)selection.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        selectionSize = tmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        for(i=selectionSize - 1 ; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            menuElement = (MenuElement) tmp.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            subElements = menuElement.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            for(j = 0, d = subElements.length ; j < d ; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (subElements[j] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                mc = subElements[j].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                if(!mc.isShowing())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if(mc instanceof JComponent) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   401
                    cWidth  = mc.getWidth();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   402
                    cHeight = mc.getHeight();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    r2 = mc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    cWidth  = r2.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    cHeight = r2.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                p.x = screenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                p.y = screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                SwingUtilities.convertPointFromScreen(p,mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                /** Return the deepest component on the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                 *  path in whose bounds the event's point occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    return mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return null;
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
     * When a MenuElement receives an event from a KeyListener, it should never process the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * directly. Instead all MenuElements should call this method with the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param e  a KeyEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public void processKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        MenuElement[] sel2 = new MenuElement[0];
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   431
        sel2 = selection.toArray(sel2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        int selSize = sel2.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        MenuElement[] path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (selSize < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        for (int i=selSize-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            MenuElement elem = sel2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            MenuElement[] subs = elem.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            for (int j=0; j<subs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (subs[j] == null || !subs[j].getComponent().isShowing()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    || !subs[j].getComponent().isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if(path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    path = new MenuElement[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    System.arraycopy(sel2, 0, path, 0, i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                path[i+1] = subs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                subs[j].processKeyEvent(e, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // finally dispatch event to the first component in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        path = new MenuElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        path[0] = sel2[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        path[0].processKeyEvent(e, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Return true if c is part of the currently used menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public boolean isComponentPartOfCurrentMenu(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if(selection.size() > 0) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   476
            MenuElement me = selection.elementAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return isComponentPartOfCurrentMenu(me,c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private boolean isComponentPartOfCurrentMenu(MenuElement root,Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        MenuElement children[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        int i,d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (root == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if(root.getComponent() == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            children = root.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            for(i=0,d=children.length;i<d;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if(isComponentPartOfCurrentMenu(children[i],c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
}