1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import javax.swing.Action;
|
---|
10 | import javax.swing.JMenuItem;
|
---|
11 | import javax.swing.JPopupMenu;
|
---|
12 | import javax.swing.MenuElement;
|
---|
13 | import javax.swing.event.PopupMenuListener;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.actions.IPrimitiveAction;
|
---|
16 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Handler to ease management of actions in different popup menus.
|
---|
20 | * @since 5821
|
---|
21 | */
|
---|
22 | public class PopupMenuHandler {
|
---|
23 |
|
---|
24 | // Set of enabled osm primitives actions
|
---|
25 | private final Set<IPrimitiveAction> 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 IPrimitiveAction) {
|
---|
56 | primitiveActions.add((IPrimitiveAction) 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 IPrimitiveAction) {
|
---|
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 | * @since 13957 (signature)
|
---|
109 | */
|
---|
110 | public Collection<IPrimitiveAction> getPrimitiveActions() {
|
---|
111 | return Collections.unmodifiableCollection(primitiveActions);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Specifies the working set of primitives for all primitive actions.
|
---|
116 | * @param primitives The new working set of primitives. Can be null or empty
|
---|
117 | * @see IPrimitiveAction#setPrimitives
|
---|
118 | * @since 13957 (signature)
|
---|
119 | */
|
---|
120 | public void setPrimitives(Collection<? extends IPrimitive> primitives) {
|
---|
121 | for (IPrimitiveAction action : primitiveActions) {
|
---|
122 | action.setPrimitives(primitives);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|