source: josm/trunk/src/org/openstreetmap/josm/gui/util/StayOpenMenuItem.java@ 5094

Last change on this file since 5094 was 5094, checked in by simon04, 12 years ago

see #6895 - add license comment to recently added files

File size: 1.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import javax.swing.Action;
5import javax.swing.Icon;
6import javax.swing.JMenuItem;
7import javax.swing.MenuElement;
8import javax.swing.MenuSelectionManager;
9import javax.swing.event.ChangeEvent;
10import javax.swing.event.ChangeListener;
11
12/**
13 * An extension of JMenuItem that doesn't close the menu when selected.
14 *
15 * @author Darryl http://tips4java.wordpress.com/2010/09/12/keeping-menus-open/
16 */
17public class StayOpenMenuItem extends JMenuItem {
18
19 private static MenuElement[] path;
20
21 {
22 getModel().addChangeListener(new ChangeListener() {
23
24 @Override
25 public void stateChanged(ChangeEvent e) {
26 if (getModel().isArmed() && isShowing()) {
27 path = MenuSelectionManager.defaultManager().getSelectedPath();
28 }
29 }
30 });
31 }
32
33 /**
34 * @see JMenuItem#JMenuItem()
35 */
36 public StayOpenMenuItem() {
37 super();
38 }
39
40 /**
41 * @see JMenuItem#JMenuItem(javax.swing.Action)
42 */
43 public StayOpenMenuItem(Action a) {
44 super(a);
45 }
46
47 /**
48 * @see JMenuItem#JMenuItem(javax.swing.Icon)
49 */
50 public StayOpenMenuItem(Icon icon) {
51 super(icon);
52 }
53
54 /**
55 * @see JMenuItem#JMenuItem(java.lang.String)
56 */
57 public StayOpenMenuItem(String text) {
58 super(text);
59 }
60
61 /**
62 * @see JMenuItem#JMenuItem(java.lang.String, javax.swing.Icon)
63 */
64 public StayOpenMenuItem(String text, Icon icon) {
65 super(text, icon);
66 }
67
68 /**
69 * @see JMenuItem#JMenuItem(java.lang.String, int)
70 */
71 public StayOpenMenuItem(String text, int mnemonic) {
72 super(text, mnemonic);
73 }
74
75 /**
76 * Overridden to reopen the menu.
77 *
78 * @param pressTime the time to "hold down" the button, in milliseconds
79 */
80 @Override
81 public void doClick(int pressTime) {
82 super.doClick(pressTime);
83 MenuSelectionManager.defaultManager().setSelectedPath(path);
84 }
85}
Note: See TracBrowser for help on using the repository browser.