1 package ch
.cyberduck
.menu
;
4 * ch.cyberduck.menu.MenuItemAction.java
11 * Copyright (c) 2003 David Kocher. All rights reserved.
12 * http://icu.unizh.ch/~dkocher/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * Bug fixes, suggestions and comments should be sent to:
29 * Permission is given to use or modify this code in your own code.
34 import javax
.swing
.event
.EventListenerList
;
36 import java
.awt
.event
.*;
39 MenuItemAction is an AbstractAction designed to control JMenuItems which
40 are shared amongst several JMenuBars.
42 For convenience, it uses ActionListeners just like JMenuItems do.
44 This implementation uses getMenuShortcutKeyMask to provide the correct
45 modifiers for the current platform.
47 It's recommended you not use mnemonics on Macintoshes, as that is counter to
48 the Mac User Interface
50 public class MenuItemAction
extends AbstractAction
{
52 static int sMenuMask
= Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask();
53 protected EventListenerList listenerList
= new EventListenerList();
56 * Creates a menuItemAction with text.
58 * @param text the text of the MenuItem.
60 MenuItemAction(String text
) {
65 * Creates a menuItemAction with text and an accelerator.
67 * @param text the text of the MenuItem.
68 * @param accel the key which will be combined with the
69 * default MenuShortcutKeyMask to create the accelerator
71 * @see #Toolkit.getMenuShortcutKeyMask
73 MenuItemAction(String text
, int accel
) {
78 * Creates a menuItemAction with text and an accelerator.
80 * @param text the text of the MenuItem.
81 * @param accel the key which will be combined with the
82 * default MenuShortcutKeyMask to create the accelerator
83 * @param extraMask the modifiers which will be combined with the default
85 * For best results, extraMask should not be ctrl or meta
86 * because the default might be one of those
87 * ctrl-meta shortcuts are not common on any platform
89 * @see #Toolkit.getMenuShortcutKeyMask
92 MenuItemAction(String text
, int accel
, int extraMask
) {
94 putValue(Action
.ACCELERATOR_KEY
, KeyStroke
.getKeyStroke(accel
, sMenuMask
| extraMask
));
97 /** Create a <code>JMenuItem</code> that uses this <code>Action</code>.
98 You could just create a new JMenuItem directly, but prior to 1.4,
99 JMenuItem.configurePropertiesFromAction ignores ACCELERATOR_KEY
101 If you set it after creation, remember that JMenuItem's default
102 PropertyChangeListener isn't looking for it either, so you'll need to add your own
104 public JMenuItem
createMenuItem() {
105 JMenuItem mi
= new JMenuItem(this);
106 KeyStroke accel
= (KeyStroke
)getValue(Action
.ACCELERATOR_KEY
);
108 mi
.setAccelerator(accel
);
113 * Adds an <code>ActionListener</code> to the Action.
114 * @param l the <code>ActionListener</code> to be added
116 public void addActionListener(ActionListener l
) {
117 listenerList
.add(ActionListener
.class, l
);
122 * Removes an <code>ActionListener</code> from the Action.
123 * @param l the listener to be removed
125 public void removeActionListener(ActionListener l
) {
126 listenerList
.remove(ActionListener
.class, l
);
130 * Notifies all listeners that have registered interest for
131 * notification on this event type.
133 * @param e the <code>ActionEvent</code> object
134 * @see EventListenerList
136 public void actionPerformed(ActionEvent event
) {
137 // Guaranteed to return a non-null array
138 Object
[] listeners
= listenerList
.getListenerList();
139 // Process the listeners last to first, notifying
140 // those that are interested in this event
141 for (int i
= listeners
.length
-2; i
>=0; i
-=2) {
142 if (listeners
[i
]==ActionListener
.class) {
143 ((ActionListener
)listeners
[i
+1]).actionPerformed(event
);