| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions.relation;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Component;
|
|---|
| 7 | import java.awt.Rectangle;
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.ActionListener;
|
|---|
| 10 | import java.awt.event.KeyEvent;
|
|---|
| 11 | import java.util.Collections;
|
|---|
| 12 | import java.util.List;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.AbstractAction;
|
|---|
| 15 | import javax.swing.JMenuItem;
|
|---|
| 16 | import javax.swing.JPopupMenu;
|
|---|
| 17 | import javax.swing.KeyStroke;
|
|---|
| 18 | import javax.swing.plaf.basic.BasicArrowButton;
|
|---|
| 19 |
|
|---|
| 20 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 21 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 22 | import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 25 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 26 | import org.openstreetmap.josm.gui.SideButton;
|
|---|
| 27 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 29 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 30 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 31 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Action for accessing recent relations.
|
|---|
| 35 | * @since 9668
|
|---|
| 36 | */
|
|---|
| 37 | public class RecentRelationsAction extends JosmAction implements CommandQueueListener {
|
|---|
| 38 |
|
|---|
| 39 | private final SideButton editButton;
|
|---|
| 40 | private final BasicArrowButton arrow;
|
|---|
| 41 | private final Shortcut shortcut;
|
|---|
| 42 | private final LaunchEditorAction launchAction;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Constructs a new <code>RecentRelationsAction</code>.
|
|---|
| 46 | * @param editButton edit button
|
|---|
| 47 | */
|
|---|
| 48 | public RecentRelationsAction(SideButton editButton) {
|
|---|
| 49 | super(RecentRelationsAction.class.getName(), null, null, null, false, true);
|
|---|
| 50 | this.editButton = editButton;
|
|---|
| 51 | arrow = editButton.createArrow(this);
|
|---|
| 52 | arrow.setToolTipText(tr("List of recent relations"));
|
|---|
| 53 | UndoRedoHandler.getInstance().addCommandQueueListener(this);
|
|---|
| 54 | enableArrow();
|
|---|
| 55 | shortcut = Shortcut.registerShortcut("relationeditor:editrecentrelation",
|
|---|
| 56 | tr("Relation Editor: {0}", tr("Open recent relation")), KeyEvent.VK_ESCAPE, Shortcut.SHIFT);
|
|---|
| 57 | launchAction = new LaunchEditorAction();
|
|---|
| 58 | MainApplication.registerActionShortcut(launchAction, shortcut);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Enables arrow button.
|
|---|
| 63 | */
|
|---|
| 64 | public void enableArrow() {
|
|---|
| 65 | if (arrow != null) {
|
|---|
| 66 | arrow.setVisible(getLastRelation() != null);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Returns the last relation.
|
|---|
| 72 | * @return the last relation
|
|---|
| 73 | */
|
|---|
| 74 | public static Relation getLastRelation() {
|
|---|
| 75 | List<Relation> recentRelations = getRecentRelationsOnActiveLayer();
|
|---|
| 76 | if (Utils.isEmpty(recentRelations))
|
|---|
| 77 | return null;
|
|---|
| 78 | return recentRelations.stream().filter(RecentRelationsAction::isRelationListable)
|
|---|
| 79 | .findFirst().orElse(null);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Determines if the given relation is listable in last relations.
|
|---|
| 84 | * @param relation relation
|
|---|
| 85 | * @return {@code true} if relation is non null, not deleted, and in current dataset
|
|---|
| 86 | */
|
|---|
| 87 | public static boolean isRelationListable(Relation relation) {
|
|---|
| 88 | return relation != null &&
|
|---|
| 89 | !relation.isDeleted() &&
|
|---|
| 90 | MainApplication.getLayerManager().getEditDataSet().containsRelation(relation);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | @Override
|
|---|
| 94 | public void actionPerformed(ActionEvent e) {
|
|---|
| 95 | RecentRelationsPopupMenu.launch(editButton, shortcut.getKeyStroke());
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | @Override
|
|---|
| 99 | public void commandChanged(int queueSize, int redoSize) {
|
|---|
| 100 | enableArrow();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | @Override
|
|---|
| 104 | protected void updateEnabledState() {
|
|---|
| 105 | enableArrow();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | @Override
|
|---|
| 109 | public void destroy() {
|
|---|
| 110 | MainApplication.unregisterActionShortcut(launchAction, shortcut);
|
|---|
| 111 | UndoRedoHandler.getInstance().removeCommandQueueListener(this);
|
|---|
| 112 | super.destroy();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Returns the list of recent relations on active layer.
|
|---|
| 117 | * @return the list of recent relations on active layer
|
|---|
| 118 | */
|
|---|
| 119 | public static List<Relation> getRecentRelationsOnActiveLayer() {
|
|---|
| 120 | if (!MainApplication.isDisplayingMapView())
|
|---|
| 121 | return Collections.emptyList();
|
|---|
| 122 | Layer activeLayer = MainApplication.getLayerManager().getActiveLayer();
|
|---|
| 123 | if (!(activeLayer instanceof OsmDataLayer)) {
|
|---|
| 124 | return Collections.emptyList();
|
|---|
| 125 | } else {
|
|---|
| 126 | return ((OsmDataLayer) activeLayer).getRecentRelations();
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static class LaunchEditorAction extends AbstractAction {
|
|---|
| 131 | @Override
|
|---|
| 132 | public void actionPerformed(ActionEvent e) {
|
|---|
| 133 | EditRelationAction.launchEditor(getLastRelation());
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | static class RecentRelationsPopupMenu extends JPopupMenu {
|
|---|
| 138 | /**
|
|---|
| 139 | * Constructs a new {@code RecentRelationsPopupMenu}.
|
|---|
| 140 | * @param recentRelations list of recent relations
|
|---|
| 141 | * @param keystroke key stroke for the first menu item
|
|---|
| 142 | */
|
|---|
| 143 | RecentRelationsPopupMenu(List<Relation> recentRelations, KeyStroke keystroke) {
|
|---|
| 144 | boolean first = true;
|
|---|
| 145 | for (Relation relation: recentRelations) {
|
|---|
| 146 | if (!isRelationListable(relation))
|
|---|
| 147 | continue;
|
|---|
| 148 | JMenuItem menuItem = new RecentRelationsMenuItem(relation);
|
|---|
| 149 | if (first) {
|
|---|
| 150 | menuItem.setAccelerator(keystroke);
|
|---|
| 151 | first = false;
|
|---|
| 152 | }
|
|---|
| 153 | menuItem.setIcon(ImageProvider.getPadded(relation, ImageProvider.ImageSizes.MENU.getImageDimension()));
|
|---|
| 154 | add(menuItem);
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | static void launch(Component parent, KeyStroke keystroke) {
|
|---|
| 159 | if (parent.isShowing()) {
|
|---|
| 160 | Rectangle r = parent.getBounds();
|
|---|
| 161 | new RecentRelationsPopupMenu(getRecentRelationsOnActiveLayer(), keystroke).show(parent, r.x, r.y + r.height);
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * A specialized {@link JMenuItem} for presenting one entry of the relation history
|
|---|
| 168 | */
|
|---|
| 169 | static class RecentRelationsMenuItem extends JMenuItem implements ActionListener {
|
|---|
| 170 | private final transient Relation relation;
|
|---|
| 171 |
|
|---|
| 172 | RecentRelationsMenuItem(Relation relation) {
|
|---|
| 173 | super(relation.getDisplayName(DefaultNameFormatter.getInstance()));
|
|---|
| 174 | this.relation = relation;
|
|---|
| 175 | addActionListener(this);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | @Override
|
|---|
| 179 | public void actionPerformed(ActionEvent e) {
|
|---|
| 180 | EditRelationAction.launchEditor(relation);
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|