001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import javax.swing.Action;
005import javax.swing.JList;
006import javax.swing.JMenuItem;
007import javax.swing.JPopupMenu;
008import javax.swing.event.ListSelectionListener;
009
010/**
011 * A popup menu for one or more lists. If actions are added to this menu, a ListSelectionListener is registered automatically.
012 * @author Vincent
013 */
014public class ListPopupMenu extends JPopupMenu {
015
016    private final JList<?>[] lists;
017
018    /**
019     * Create a new ListPopupMenu
020     * @param lists The lists to which listeners should be appended
021     */
022    public ListPopupMenu(JList<?>... lists) {
023        this.lists = lists;
024    }
025
026    @Override
027    public JMenuItem add(Action a) {
028        if (lists != null && a instanceof ListSelectionListener) {
029            for (JList<?> list : lists) {
030                list.addListSelectionListener((ListSelectionListener) a);
031            }
032        }
033        return super.add(a);
034    }
035}