[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[343] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2633] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[343] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
[3504] | 9 | import java.util.ArrayList;
|
---|
[343] | 10 | import java.util.Collection;
|
---|
| 11 | import java.util.Collections;
|
---|
| 12 | import java.util.LinkedList;
|
---|
[1862] | 13 | import java.util.List;
|
---|
[343] | 14 |
|
---|
| 15 | import javax.swing.JOptionPane;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 19 | import org.openstreetmap.josm.command.Command;
|
---|
| 20 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
[5724] | 21 | import org.openstreetmap.josm.corrector.ReverseWayNoTagCorrector;
|
---|
[729] | 22 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
[1862] | 23 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[343] | 24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 25 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[6130] | 26 | import org.openstreetmap.josm.gui.Notification;
|
---|
[1084] | 27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[8919] | 28 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
[3504] | 29 | import org.openstreetmap.josm.tools.Utils;
|
---|
[343] | 30 |
|
---|
[1844] | 31 | public final class ReverseWayAction extends JosmAction {
|
---|
[343] | 32 |
|
---|
[3504] | 33 | public static class ReverseWayResult {
|
---|
[9067] | 34 | private final Way newWay;
|
---|
| 35 | private final Collection<Command> tagCorrectionCommands;
|
---|
| 36 | private final Command reverseCommand;
|
---|
[3504] | 37 |
|
---|
| 38 | public ReverseWayResult(Way newWay, Collection<Command> tagCorrectionCommands, Command reverseCommand) {
|
---|
| 39 | this.newWay = newWay;
|
---|
| 40 | this.tagCorrectionCommands = tagCorrectionCommands;
|
---|
| 41 | this.reverseCommand = reverseCommand;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public Way getNewWay() {
|
---|
| 45 | return newWay;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public Collection<Command> getCommands() {
|
---|
[7005] | 49 | List<Command> c = new ArrayList<>();
|
---|
[3504] | 50 | c.addAll(tagCorrectionCommands);
|
---|
| 51 | c.add(reverseCommand);
|
---|
| 52 | return c;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public Command getAsSequenceCommand() {
|
---|
| 56 | return new SequenceCommand(tr("Reverse way"), getCommands());
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public Command getReverseCommand() {
|
---|
| 60 | return reverseCommand;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public Collection<Command> getTagCorrectionCommands() {
|
---|
| 64 | return tagCorrectionCommands;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[1169] | 68 | public ReverseWayAction() {
|
---|
[1218] | 69 | super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
|
---|
[4982] | 70 | Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.DIRECT), true);
|
---|
[3504] | 71 | putValue("help", ht("/Action/ReverseWays"));
|
---|
[1169] | 72 | }
|
---|
[343] | 73 |
|
---|
[6084] | 74 | @Override
|
---|
[1169] | 75 | public void actionPerformed(ActionEvent e) {
|
---|
[8443] | 76 | if (!isEnabled())
|
---|
[1820] | 77 | return;
|
---|
| 78 | if (getCurrentDataSet() == null)
|
---|
| 79 | return;
|
---|
| 80 |
|
---|
[2633] | 81 | final Collection<Way> sel = getCurrentDataSet().getSelectedWays();
|
---|
[1169] | 82 | if (sel.isEmpty()) {
|
---|
[6130] | 83 | new Notification(
|
---|
| 84 | tr("Please select at least one way."))
|
---|
| 85 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
| 86 | .setDuration(Notification.TIME_SHORT)
|
---|
| 87 | .show();
|
---|
[1169] | 88 | return;
|
---|
| 89 | }
|
---|
[729] | 90 |
|
---|
[1169] | 91 | boolean propertiesUpdated = false;
|
---|
[7005] | 92 | Collection<Command> c = new LinkedList<>();
|
---|
[1169] | 93 | for (Way w : sel) {
|
---|
[3504] | 94 | ReverseWayResult revResult;
|
---|
| 95 | try {
|
---|
| 96 | revResult = reverseWay(w);
|
---|
| 97 | } catch (UserCancelException ex) {
|
---|
| 98 | return;
|
---|
[1169] | 99 | }
|
---|
[3504] | 100 | c.addAll(revResult.getCommands());
|
---|
| 101 | propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty();
|
---|
[1169] | 102 | }
|
---|
| 103 | Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
|
---|
[1814] | 104 | if (propertiesUpdated) {
|
---|
[2348] | 105 | getCurrentDataSet().fireSelectionChanged();
|
---|
[1814] | 106 | }
|
---|
[1169] | 107 | Main.map.repaint();
|
---|
| 108 | }
|
---|
[1820] | 109 |
|
---|
[3504] | 110 | /**
|
---|
| 111 | * @param w the way
|
---|
| 112 | * @return the reverse command and the tag correction commands
|
---|
[5724] | 113 | * @throws UserCancelException if user cancels a reverse warning dialog
|
---|
[3504] | 114 | */
|
---|
| 115 | public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
|
---|
[5724] | 116 | ReverseWayNoTagCorrector.checkAndConfirmReverseWay(w);
|
---|
[3504] | 117 | Way wnew = new Way(w);
|
---|
| 118 | List<Node> nodesCopy = wnew.getNodes();
|
---|
| 119 | Collections.reverse(nodesCopy);
|
---|
| 120 | wnew.setNodes(nodesCopy);
|
---|
| 121 |
|
---|
| 122 | Collection<Command> corrCmds = Collections.<Command>emptyList();
|
---|
| 123 | if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
|
---|
| 124 | corrCmds = (new ReverseWayTagCorrector()).execute(w, wnew);
|
---|
| 125 | }
|
---|
| 126 | return new ReverseWayResult(wnew, corrCmds, new ChangeCommand(w, wnew));
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[1844] | 129 | @Override
|
---|
| 130 | protected void updateEnabledState() {
|
---|
[2256] | 131 | if (getCurrentDataSet() == null) {
|
---|
| 132 | setEnabled(false);
|
---|
| 133 | } else {
|
---|
| 134 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
| 135 | }
|
---|
[1820] | 136 | }
|
---|
[2256] | 137 |
|
---|
| 138 | @Override
|
---|
| 139 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
[3504] | 140 | setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
|
---|
[2256] | 141 | }
|
---|
[343] | 142 | }
|
---|