source: josm/trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java@ 12533

Last change on this file since 12533 was 12533, checked in by Don-vip, 7 years ago

sonar - fix recent minor code style issues

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