[7937] | 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.OsmPrimitiveAction;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 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<OsmPrimitiveAction> primitiveActions = new HashSet<>();
|
---|
| 26 | // Managed menu
|
---|
| 27 | private final JPopupMenu menu;
|
---|
[8510] | 28 |
|
---|
[7937] | 29 | /**
|
---|
| 30 | * Constructs a new {@code RelationActionMenuHandler} for the specified popup menu.
|
---|
[8510] | 31 | *
|
---|
[7937] | 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.
|
---|
[8510] | 48 | *
|
---|
[7937] | 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.
|
---|
[8510] | 65 | *
|
---|
[7937] | 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();
|
---|
[8510] | 75 | for (int i = 0; i < elements.length; i++) {
|
---|
[7937] | 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 | }
|
---|
[8510] | 105 |
|
---|
[7937] | 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 | }
|
---|
[8510] | 114 |
|
---|
[7937] | 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 | }
|
---|