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.JCheckBoxMenuItem;
|
---|
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 JCheckBoxMenuItem that doesn't close the menu when selected.
|
---|
13 | *
|
---|
14 | * @author Darryl https://tips4java.wordpress.com/2010/09/12/keeping-menus-open/
|
---|
15 | */
|
---|
16 | public class StayOpenCheckBoxMenuItem extends JCheckBoxMenuItem {
|
---|
17 |
|
---|
18 | private static volatile 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 | * Contructs a new {@code StayOpenCheckBoxMenuItem} whose properties are taken from the Action supplied.
|
---|
34 | * @param a action
|
---|
35 | */
|
---|
36 | public StayOpenCheckBoxMenuItem(Action a) {
|
---|
37 | super(a);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Overridden to reopen the menu.
|
---|
42 | *
|
---|
43 | * @param pressTime the time to "hold down" the button, in milliseconds
|
---|
44 | */
|
---|
45 | @Override
|
---|
46 | public void doClick(int pressTime) {
|
---|
47 | super.doClick(pressTime);
|
---|
48 | MenuSelectionManager.defaultManager().setSelectedPath(path);
|
---|
49 | }
|
---|
50 | }
|
---|