1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import javax.swing.Action;
|
---|
5 | import javax.swing.JRadioButtonMenuItem;
|
---|
6 | import javax.swing.MenuElement;
|
---|
7 | import javax.swing.MenuSelectionManager;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * An extension of JRadioButtonMenuItem that doesn't close the menu when selected.
|
---|
11 | *
|
---|
12 | * @author Darryl Burke https://tips4java.wordpress.com/2010/09/12/keeping-menus-open/
|
---|
13 | */
|
---|
14 | public class StayOpenRadioButtonMenuItem extends JRadioButtonMenuItem {
|
---|
15 |
|
---|
16 | private MenuElement[] path;
|
---|
17 |
|
---|
18 | {
|
---|
19 | getModel().addChangeListener(e -> {
|
---|
20 | if (getModel().isArmed() && isShowing()) {
|
---|
21 | path = MenuSelectionManager.defaultManager().getSelectedPath();
|
---|
22 | }
|
---|
23 | });
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code StayOpenRadioButtonMenuItem} with no set text or icon.
|
---|
28 | * @see JRadioButtonMenuItem#JRadioButtonMenuItem()
|
---|
29 | */
|
---|
30 | public StayOpenRadioButtonMenuItem() {
|
---|
31 | super();
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code StayOpenRadioButtonMenuItem} whose properties are taken from the Action supplied.
|
---|
36 | * @param a associated action
|
---|
37 | * @see JRadioButtonMenuItem#JRadioButtonMenuItem(Action)
|
---|
38 | */
|
---|
39 | public StayOpenRadioButtonMenuItem(Action a) {
|
---|
40 | super(a);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Overridden to reopen the menu.
|
---|
45 | *
|
---|
46 | * @param pressTime the time to "hold down" the button, in milliseconds
|
---|
47 | */
|
---|
48 | @Override
|
---|
49 | public void doClick(int pressTime) {
|
---|
50 | super.doClick(pressTime);
|
---|
51 | MenuSelectionManager.defaultManager().setSelectedPath(path);
|
---|
52 | }
|
---|
53 | }
|
---|