source: josm/trunk/src/org/openstreetmap/josm/gui/PopupMenuHandler.java @ 13265

Last change on this file since 13265 was 11386, checked in by Don-vip, 22 months ago

sonar - squid:S1066 - Collapsible "if" statements should be merged

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.util.Collection;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.Set;
8
9import javax.swing.Action;
10import javax.swing.JMenuItem;
11import javax.swing.JPopupMenu;
12import javax.swing.MenuElement;
13import javax.swing.event.PopupMenuListener;
14
15import org.openstreetmap.josm.actions.OsmPrimitiveAction;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17
18/**
19 * Handler to ease management of actions in different popup menus.
20 * @since 5821
21 */
22public class PopupMenuHandler {
23
24    // Set of enabled osm primitives actions
25    private final Set<OsmPrimitiveAction> primitiveActions = new HashSet<>();
26    // Managed menu
27    private final JPopupMenu menu;
28
29    /**
30     * Constructs a new {@code RelationActionMenuHandler} for the specified popup menu.
31     *
32     * @param menu The menu to be managed
33     */
34    public PopupMenuHandler(JPopupMenu menu) {
35        this.menu = menu;
36    }
37
38    /**
39     * Appends a new separator at the end of the menu.
40     * @see JPopupMenu#addSeparator
41     */
42    public void addSeparator() {
43        menu.addSeparator();
44    }
45
46    /**
47     * Appends a new menu item to the end of the menu which dispatches the specified <code>Action</code> object.
48     *
49     * @param a the <code>Action</code> to add to the menu
50     * @return the new menu item
51     * @see JPopupMenu#add(Action)
52     */
53    public JMenuItem addAction(Action a) {
54        if (a != null) {
55            if (a instanceof OsmPrimitiveAction) {
56                primitiveActions.add((OsmPrimitiveAction) a);
57            }
58            return menu.add(a);
59        }
60        return null;
61    }
62
63    /**
64     * Removes the menu item which dispatches the specified <code>Action</code> object.
65     *
66     * @param a the <code>Action</code> to remove from the menu
67     * @see JPopupMenu#remove(int)
68     */
69    public void removeAction(Action a) {
70        if (a != null) {
71            if (a instanceof OsmPrimitiveAction) {
72                primitiveActions.remove(a);
73            }
74            MenuElement[] elements = menu.getSubElements();
75            for (int i = 0; i < elements.length; i++) {
76                if (elements[i] instanceof JMenuItem && ((JMenuItem) elements[i]).getAction() == a) {
77                    menu.remove(i);
78                    return;
79                }
80            }
81        }
82    }
83
84    /**
85     *  Adds a <code>PopupMenu</code> listener.
86     *
87     *  @param l the <code>PopupMenuListener</code> to add
88     *  @see JPopupMenu#addPopupMenuListener
89     */
90    public void addListener(PopupMenuListener l) {
91        menu.addPopupMenuListener(l);
92    }
93
94    /**
95     * Removes a <code>PopupMenu</code> listener.
96     *
97     * @param l the <code>PopupMenuListener</code> to remove
98     *  @see JPopupMenu#removePopupMenuListener
99     */
100    public void removeListener(PopupMenuListener l) {
101        menu.removePopupMenuListener(l);
102    }
103
104    /**
105     * Returns all enabled primitive actions.
106     * @return All primitive actions that have been added.
107     * @see #addAction(Action)
108     */
109    public Collection<OsmPrimitiveAction> getPrimitiveActions() {
110        return Collections.unmodifiableCollection(primitiveActions);
111    }
112
113    /**
114     * Specifies the working set of primitives for all primitive actions.
115     * @param primitives The new working set of primitives. Can be null or empty
116     * @see OsmPrimitiveAction#setPrimitives
117     */
118    public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
119        for (OsmPrimitiveAction action : primitiveActions) {
120            action.setPrimitives(primitives);
121        }
122    }
123}
Note: See TracBrowser for help on using the repository browser.