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

Last change on this file since 10809 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 3.7 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) {
77 if (((JMenuItem) elements[i]).getAction() == a) {
78 menu.remove(i);
79 return;
80 }
81 }
82 }
83 }
84 }
85
86 /**
87 * Adds a <code>PopupMenu</code> listener.
88 *
89 * @param l the <code>PopupMenuListener</code> to add
90 * @see JPopupMenu#addPopupMenuListener
91 */
92 public void addListener(PopupMenuListener l) {
93 menu.addPopupMenuListener(l);
94 }
95
96 /**
97 * Removes a <code>PopupMenu</code> listener.
98 *
99 * @param l the <code>PopupMenuListener</code> to remove
100 * @see JPopupMenu#removePopupMenuListener
101 */
102 public void removeListener(PopupMenuListener l) {
103 menu.removePopupMenuListener(l);
104 }
105
106 /**
107 * Returns all enabled primitive actions.
108 * @return All primitive actions that have been added.
109 * @see #addAction(Action)
110 */
111 public Collection<OsmPrimitiveAction> getPrimitiveActions() {
112 return Collections.unmodifiableCollection(primitiveActions);
113 }
114
115 /**
116 * Specifies the working set of primitives for all primitive actions.
117 * @param primitives The new working set of primitives. Can be null or empty
118 * @see OsmPrimitiveAction#setPrimitives
119 */
120 public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
121 for (OsmPrimitiveAction action : primitiveActions) {
122 action.setPrimitives(primitives);
123 }
124 }
125}
Note: See TracBrowser for help on using the repository browser.