001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.util;
003
004import java.awt.Graphics;
005
006import javax.swing.JComponent;
007import javax.swing.MenuSelectionManager;
008import javax.swing.UIManager;
009import javax.swing.plaf.ComponentUI;
010import javax.swing.plaf.basic.BasicCheckBoxMenuItemUI;
011
012/**
013 * A CheckBoxMenuItem UI delegate that doesn't close the menu when selected.
014 * @author Darryl Burke https://stackoverflow.com/a/3759675/2257172
015 * @since 15288
016 */
017public class StayOpenCheckBoxMenuItemUI extends BasicCheckBoxMenuItemUI {
018
019    @Override
020    protected void doClick(MenuSelectionManager msm) {
021        menuItem.doClick(0);
022    }
023
024    @Override
025    public void update(Graphics g, JComponent c) {
026        ComponentUI ui = UIManager.getUI(c);
027        if (ui != null) {
028            this.uninstallUI(c);
029            try {
030                ui.installUI(c);
031                try {
032                    ui.update(g, c);
033                } finally {
034                    ui.uninstallUI(c);
035                }
036            } finally {
037                this.installUI(c);
038            }
039        } else {
040            super.update(g, c);
041        }
042    }
043
044    /**
045     * Creates a new {@code StayOpenCheckBoxMenuItemUI}.
046     * @param c not used
047     * @return newly created {@code StayOpenCheckBoxMenuItemUI}
048     */
049    public static ComponentUI createUI(JComponent c) {
050        return new StayOpenCheckBoxMenuItemUI();
051    }
052}