001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.util; 003 004import javax.swing.Action; 005import javax.swing.JRadioButtonMenuItem; 006import javax.swing.MenuElement; 007import javax.swing.MenuSelectionManager; 008 009/** 010 * An extension of JRadioButtonMenuItem that doesn't close the menu when selected. 011 * 012 * @author Darryl Burke https://tips4java.wordpress.com/2010/09/12/keeping-menus-open/ 013 */ 014public class StayOpenRadioButtonMenuItem extends JRadioButtonMenuItem { 015 016 private MenuElement[] path; 017 018 { 019 getModel().addChangeListener(e -> { 020 if (getModel().isArmed() && isShowing()) { 021 path = MenuSelectionManager.defaultManager().getSelectedPath(); 022 } 023 }); 024 } 025 026 /** 027 * Constructs a new {@code StayOpenRadioButtonMenuItem} with no set text or icon. 028 * @see JRadioButtonMenuItem#JRadioButtonMenuItem() 029 */ 030 public StayOpenRadioButtonMenuItem() { 031 super(); 032 } 033 034 /** 035 * Constructs a new {@code StayOpenRadioButtonMenuItem} whose properties are taken from the Action supplied. 036 * @param a associated action 037 * @see JRadioButtonMenuItem#JRadioButtonMenuItem(Action) 038 */ 039 public StayOpenRadioButtonMenuItem(Action a) { 040 super(a); 041 } 042 043 /** 044 * Overridden to reopen the menu. 045 * 046 * @param pressTime the time to "hold down" the button, in milliseconds 047 */ 048 @Override 049 public void doClick(int pressTime) { 050 super.doClick(pressTime); 051 MenuSelectionManager.defaultManager().setSelectedPath(path); 052 } 053}