jdk/src/share/classes/javax/swing/MenuSelectionManager.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 459 d555ba8bbec1
child 1301 15e81207e1f2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 459
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
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
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private Vector selection = new Vector();
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++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if(i < currentSelectionCount && (MenuElement)selection.elementAt(i) == path[i])
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--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            MenuElement me = (MenuElement)selection.elementAt(i);
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++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            res[i] = (MenuElement) selection.elementAt(i);
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() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return (ChangeListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ChangeListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (listeners[i]==ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (changeEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * When a MenuElement receives an event from a MouseListener, it should never process the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * directly. Instead all MenuElements should call this method with the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param event  a MouseEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void processMouseEvent(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int screenX,screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Point p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int i,c,j,d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Component mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        Rectangle r2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int cWidth,cHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        MenuElement menuElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        MenuElement subElements[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        MenuElement path[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        Vector tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int selectionSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        p = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
459
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   221
        Component source = event.getComponent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
459
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   223
        if ((source != null) && !source.isShowing()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // This can happen if a mouseReleased removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // containing component -- bug 4146684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int type = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int modifiers = event.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if ((type==MouseEvent.MOUSE_ENTERED||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             type==MouseEvent.MOUSE_EXITED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            && ((modifiers & (InputEvent.BUTTON1_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                              InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
459
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   239
        if (source != null) {
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   240
            SwingUtilities.convertPointToScreen(p, source);
d555ba8bbec1 6690791: Even more ClassCasetException with TrayIcon
mlapshin
parents: 2
diff changeset
   241
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        screenX = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        screenY = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        tmp = (Vector)selection.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        selectionSize = tmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for (i=selectionSize - 1;i >= 0 && success == false; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            menuElement = (MenuElement) tmp.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            subElements = menuElement.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            for (j = 0, d = subElements.length;j < d && success == false; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if (subElements[j] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                mc = subElements[j].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if(!mc.isShowing())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if(mc instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    cWidth  = ((JComponent)mc).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    cHeight = ((JComponent)mc).getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    r2 = mc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    cWidth  = r2.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    cHeight = r2.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                p.x = screenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                p.y = screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                SwingUtilities.convertPointFromScreen(p,mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                /** Send the event to visible menu element if menu element currently in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                 *  the selected path or contains the event location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                   (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    int k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    if(path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        path = new MenuElement[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        for(k=0;k<=i;k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                            path[k] = (MenuElement)tmp.elementAt(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    path[i+1] = subElements[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    MenuElement currentSelection[] = getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    // Enter/exit detection -- needs tuning...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    if (currentSelection[currentSelection.length-1] !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        path[i+1] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        (currentSelection.length < 2 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                         currentSelection[currentSelection.length-2] !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                         path[i+1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        Component oldMC = currentSelection[currentSelection.length-1].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        MouseEvent exitEvent = new MouseEvent(oldMC, MouseEvent.MOUSE_EXITED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                                              event.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                                              event.getModifiers(), p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                                              event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                                              event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                                              event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                                              event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                                              MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        currentSelection[currentSelection.length-1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            processMouseEvent(exitEvent, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        MouseEvent enterEvent = new MouseEvent(mc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                                               MouseEvent.MOUSE_ENTERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                                               event.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                                               event.getModifiers(), p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                                               event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                                               event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                                               event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                                               event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                                               MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        subElements[j].processMouseEvent(enterEvent, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    MouseEvent mouseEvent = new MouseEvent(mc, event.getID(),event. getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                                           event.getModifiers(), p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                           event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                           event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                           event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                                           event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                                           MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    subElements[j].processMouseEvent(mouseEvent, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    success = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    event.consume();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void printMenuElementArray(MenuElement path[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        printMenuElementArray(path, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private void printMenuElementArray(MenuElement path[], boolean dumpStack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        System.out.println("Path is(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        for(i=0,j=path.length; i<j ;i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            for (int k=0; k<=i; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                System.out.print("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            MenuElement me = (MenuElement) path[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if(me instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                System.out.println(((JMenuItem)me).getText() + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } else if (me instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                System.out.println("JMenuBar, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            } else if(me instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                System.out.println("JPopupMenu, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            } else if (me == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                System.out.println("NULL , ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                System.out.println("" + me + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        System.out.println(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (dumpStack == true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Returns the component in the currently selected path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * which contains sourcePoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param source The component in whose coordinate space sourcePoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *        is given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param sourcePoint The point which is being tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return The component in the currently selected path which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *         contains sourcePoint (relative to the source component's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         coordinate space.  If sourcePoint is not inside a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         on the currently selected path, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public Component componentForPoint(Component source, Point sourcePoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        int screenX,screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Point p = sourcePoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int i,c,j,d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        Component mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        Rectangle r2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int cWidth,cHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        MenuElement menuElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        MenuElement subElements[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        Vector tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int selectionSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        SwingUtilities.convertPointToScreen(p,source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        screenX = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        screenY = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        tmp = (Vector)selection.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        selectionSize = tmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        for(i=selectionSize - 1 ; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            menuElement = (MenuElement) tmp.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            subElements = menuElement.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            for(j = 0, d = subElements.length ; j < d ; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (subElements[j] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                mc = subElements[j].getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if(!mc.isShowing())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                if(mc instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    cWidth  = ((JComponent)mc).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    cHeight = ((JComponent)mc).getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    r2 = mc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    cWidth  = r2.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    cHeight = r2.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                p.x = screenX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                p.y = screenY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                SwingUtilities.convertPointFromScreen(p,mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                /** Return the deepest component on the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                 *  path in whose bounds the event's point occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    return mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * When a MenuElement receives an event from a KeyListener, it should never process the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * directly. Instead all MenuElements should call this method with the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param e  a KeyEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void processKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        MenuElement[] sel2 = new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        sel2 = (MenuElement[])selection.toArray(sel2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        int selSize = sel2.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        MenuElement[] path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (selSize < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        for (int i=selSize-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            MenuElement elem = sel2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            MenuElement[] subs = elem.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            for (int j=0; j<subs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (subs[j] == null || !subs[j].getComponent().isShowing()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    || !subs[j].getComponent().isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if(path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    path = new MenuElement[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    System.arraycopy(sel2, 0, path, 0, i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                path[i+1] = subs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                subs[j].processKeyEvent(e, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    return;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // finally dispatch event to the first component in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        path = new MenuElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        path[0] = sel2[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        path[0].processKeyEvent(e, path, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Return true if c is part of the currently used menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public boolean isComponentPartOfCurrentMenu(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if(selection.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            MenuElement me = (MenuElement)selection.elementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return isComponentPartOfCurrentMenu(me,c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    private boolean isComponentPartOfCurrentMenu(MenuElement root,Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        MenuElement children[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int i,d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (root == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if(root.getComponent() == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            children = root.getSubElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            for(i=0,d=children.length;i<d;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                if(isComponentPartOfCurrentMenu(children[i],c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
}