| 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 static volatile 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 | * @see JRadioButtonMenuItem#JRadioButtonMenuItem(Action)
|
|---|
| 37 | */
|
|---|
| 38 | public StayOpenRadioButtonMenuItem(Action a) {
|
|---|
| 39 | super(a);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Overridden to reopen the menu.
|
|---|
| 44 | *
|
|---|
| 45 | * @param pressTime the time to "hold down" the button, in milliseconds
|
|---|
| 46 | */
|
|---|
| 47 | @Override
|
|---|
| 48 | public void doClick(int pressTime) {
|
|---|
| 49 | super.doClick(pressTime);
|
|---|
| 50 | MenuSelectionManager.defaultManager().setSelectedPath(path);
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|