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 * @see JRadioButtonMenuItem#JRadioButtonMenuItem(Action) 037 */ 038 public StayOpenRadioButtonMenuItem(Action a) { 039 super(a); 040 } 041 042 /** 043 * Overridden to reopen the menu. 044 * 045 * @param pressTime the time to "hold down" the button, in milliseconds 046 */ 047 @Override 048 public void doClick(int pressTime) { 049 super.doClick(pressTime); 050 MenuSelectionManager.defaultManager().setSelectedPath(path); 051 } 052}