1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
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;
|
---|
21 | import org.openstreetmap.josm.corrector.ReverseWayNoTagCorrector;
|
---|
22 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
23 | import org.openstreetmap.josm.data.osm.Node;
|
---|
24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
25 | import org.openstreetmap.josm.data.osm.Way;
|
---|
26 | import org.openstreetmap.josm.gui.Notification;
|
---|
27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
28 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
29 | import org.openstreetmap.josm.tools.Utils;
|
---|
30 |
|
---|
31 | public final class ReverseWayAction extends JosmAction {
|
---|
32 |
|
---|
33 | public static class ReverseWayResult {
|
---|
34 | private final Way newWay;
|
---|
35 | private final Collection<Command> tagCorrectionCommands;
|
---|
36 | private final Command reverseCommand;
|
---|
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() {
|
---|
49 | List<Command> c = new ArrayList<>();
|
---|
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 |
|
---|
68 | public ReverseWayAction() {
|
---|
69 | super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
|
---|
70 | Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.DIRECT), true);
|
---|
71 | putValue("help", ht("/Action/ReverseWays"));
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void actionPerformed(ActionEvent e) {
|
---|
76 | if (!isEnabled())
|
---|
77 | return;
|
---|
78 | if (getCurrentDataSet() == null)
|
---|
79 | return;
|
---|
80 |
|
---|
81 | final Collection<Way> sel = getCurrentDataSet().getSelectedWays();
|
---|
82 | if (sel.isEmpty()) {
|
---|
83 | new Notification(
|
---|
84 | tr("Please select at least one way."))
|
---|
85 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
86 | .setDuration(Notification.TIME_SHORT)
|
---|
87 | .show();
|
---|
88 | return;
|
---|
89 | }
|
---|
90 |
|
---|
91 | boolean propertiesUpdated = false;
|
---|
92 | Collection<Command> c = new LinkedList<>();
|
---|
93 | for (Way w : sel) {
|
---|
94 | ReverseWayResult revResult;
|
---|
95 | try {
|
---|
96 | revResult = reverseWay(w);
|
---|
97 | } catch (UserCancelException ex) {
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | c.addAll(revResult.getCommands());
|
---|
101 | propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty();
|
---|
102 | }
|
---|
103 | Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
|
---|
104 | if (propertiesUpdated) {
|
---|
105 | getCurrentDataSet().fireSelectionChanged();
|
---|
106 | }
|
---|
107 | Main.map.repaint();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * @param w the way
|
---|
112 | * @return the reverse command and the tag correction commands
|
---|
113 | * @throws UserCancelException if user cancels a reverse warning dialog
|
---|
114 | */
|
---|
115 | public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
|
---|
116 | ReverseWayNoTagCorrector.checkAndConfirmReverseWay(w);
|
---|
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 |
|
---|
129 | @Override
|
---|
130 | protected void updateEnabledState() {
|
---|
131 | if (getCurrentDataSet() == null) {
|
---|
132 | setEnabled(false);
|
---|
133 | } else {
|
---|
134 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
140 | setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
|
---|
141 | }
|
---|
142 | }
|
---|