source: josm/trunk/src/org/openstreetmap/josm/gui/util/StayOpenRadioButtonMenuItem.java@ 15339

Last change on this file since 15339 was 15339, checked in by Don-vip, 5 years ago

see #10435 - fix Findbugs violation VO_VOLATILE_REFERENCE_TO_ARRAY

  • Property svn:eol-style set to native
File size: 1.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import javax.swing.Action;
5import javax.swing.JRadioButtonMenuItem;
6import javax.swing.MenuElement;
7import 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 */
14public 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 * @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}
Note: See TracBrowser for help on using the repository browser.