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