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.LinkedHashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
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.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
28 | import org.openstreetmap.josm.gui.Notification;
|
---|
29 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
30 | import org.openstreetmap.josm.tools.Logging;
|
---|
31 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
32 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Reverses the ways that are currently selected by the user
|
---|
36 | */
|
---|
37 | public final class ReverseWayAction extends JosmAction {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * The resulting way after reversing it and the commands to get there.
|
---|
41 | */
|
---|
42 | public static class ReverseWayResult {
|
---|
43 | private final Way newWay;
|
---|
44 | private final Collection<Command> tagCorrectionCommands;
|
---|
45 | private final Command reverseCommand;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Create a new {@link ReverseWayResult}
|
---|
49 | * @param newWay The new way primitive
|
---|
50 | * @param tagCorrectionCommands The commands to correct the tags
|
---|
51 | * @param reverseCommand The command to reverse the way
|
---|
52 | */
|
---|
53 | public ReverseWayResult(Way newWay, Collection<Command> tagCorrectionCommands, Command reverseCommand) {
|
---|
54 | this.newWay = newWay;
|
---|
55 | this.tagCorrectionCommands = tagCorrectionCommands;
|
---|
56 | this.reverseCommand = reverseCommand;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Gets the new way object
|
---|
61 | * @return The new, reversed way
|
---|
62 | */
|
---|
63 | public Way getNewWay() {
|
---|
64 | return newWay;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Gets the commands that will be required to do a full way reversal including changing the tags
|
---|
69 | * @return The comamnds
|
---|
70 | */
|
---|
71 | public Collection<Command> getCommands() {
|
---|
72 | List<Command> c = new ArrayList<>();
|
---|
73 | c.addAll(tagCorrectionCommands);
|
---|
74 | c.add(reverseCommand);
|
---|
75 | return c;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Gets a single sequence command for reversing this way including changing the tags
|
---|
80 | * @return the command
|
---|
81 | */
|
---|
82 | public Command getAsSequenceCommand() {
|
---|
83 | return new SequenceCommand(tr("Reverse way"), getCommands());
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Gets the basic reverse command that only changes the order of the nodes.
|
---|
88 | * @return The reorder nodes command
|
---|
89 | */
|
---|
90 | public Command getReverseCommand() {
|
---|
91 | return reverseCommand;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Gets the command to change the tags of the way
|
---|
96 | * @return The command to reverse the tags
|
---|
97 | */
|
---|
98 | public Collection<Command> getTagCorrectionCommands() {
|
---|
99 | return tagCorrectionCommands;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Creates a new {@link ReverseWayAction} and binds the shortcut
|
---|
105 | */
|
---|
106 | public ReverseWayAction() {
|
---|
107 | super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
|
---|
108 | Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.DIRECT), true);
|
---|
109 | putValue("help", ht("/Action/ReverseWays"));
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public void actionPerformed(ActionEvent e) {
|
---|
114 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
115 | if (!isEnabled() || ds == null)
|
---|
116 | return;
|
---|
117 |
|
---|
118 | final Collection<Way> sel = new LinkedHashSet<>(ds.getSelectedWays());
|
---|
119 | sel.removeIf(Way::isIncomplete);
|
---|
120 | if (sel.isEmpty()) {
|
---|
121 | new Notification(
|
---|
122 | tr("Please select at least one way."))
|
---|
123 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
124 | .setDuration(Notification.TIME_SHORT)
|
---|
125 | .show();
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | Collection<Command> c = new LinkedList<>();
|
---|
130 | for (Way w : sel) {
|
---|
131 | ReverseWayResult revResult;
|
---|
132 | try {
|
---|
133 | revResult = reverseWay(w);
|
---|
134 | } catch (UserCancelException ex) {
|
---|
135 | Logging.trace(ex);
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | c.addAll(revResult.getCommands());
|
---|
139 | }
|
---|
140 | MainApplication.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Reverses a given way.
|
---|
145 | * @param w the way
|
---|
146 | * @return the reverse command and the tag correction commands
|
---|
147 | * @throws UserCancelException if user cancels a reverse warning dialog
|
---|
148 | */
|
---|
149 | public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
|
---|
150 | ReverseWayNoTagCorrector.checkAndConfirmReverseWay(w);
|
---|
151 | Way wnew = new Way(w);
|
---|
152 | List<Node> nodesCopy = wnew.getNodes();
|
---|
153 | Collections.reverse(nodesCopy);
|
---|
154 | wnew.setNodes(nodesCopy);
|
---|
155 |
|
---|
156 | Collection<Command> corrCmds = Collections.<Command>emptyList();
|
---|
157 | if (Config.getPref().getBoolean("tag-correction.reverse-way", true)) {
|
---|
158 | corrCmds = (new ReverseWayTagCorrector()).execute(w, wnew);
|
---|
159 | }
|
---|
160 | return new ReverseWayResult(wnew, corrCmds, new ChangeCommand(w, wnew));
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | protected void updateEnabledState() {
|
---|
165 | updateEnabledStateOnCurrentSelection();
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
170 | setEnabled(selection.stream().anyMatch(
|
---|
171 | o -> o instanceof Way && !o.isIncomplete() && !o.getDataSet().isLocked()));
|
---|
172 | }
|
---|
173 | }
|
---|