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