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

Last change on this file since 6083 was 5724, checked in by Don-vip, 11 years ago

fix #4664 - warn when reverting a way with direction defined by tag

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.LinkedList;
13import java.util.List;
14
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.command.ChangeCommand;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.command.SequenceCommand;
21import org.openstreetmap.josm.corrector.ReverseWayNoTagCorrector;
22import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
23import org.openstreetmap.josm.corrector.UserCancelException;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.Way;
27import org.openstreetmap.josm.tools.Shortcut;
28import org.openstreetmap.josm.tools.Utils;
29
30public final class ReverseWayAction extends JosmAction {
31
32 public static class ReverseWayResult {
33 private Way newWay;
34 private Collection<Command> tagCorrectionCommands;
35 private Command reverseCommand;
36
37 public ReverseWayResult(Way newWay, Collection<Command> tagCorrectionCommands, Command reverseCommand) {
38 this.newWay = newWay;
39 this.tagCorrectionCommands = tagCorrectionCommands;
40 this.reverseCommand = reverseCommand;
41 }
42
43 public Way getNewWay() {
44 return newWay;
45 }
46
47 public Collection<Command> getCommands() {
48 List<Command> c = new ArrayList<Command>();
49 c.addAll(tagCorrectionCommands);
50 c.add(reverseCommand);
51 return c;
52 }
53
54 public Command getAsSequenceCommand() {
55 return new SequenceCommand(tr("Reverse way"), getCommands());
56 }
57
58 public Command getReverseCommand() {
59 return reverseCommand;
60 }
61
62 public Collection<Command> getTagCorrectionCommands() {
63 return tagCorrectionCommands;
64 }
65 }
66
67 public ReverseWayAction() {
68 super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
69 Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.DIRECT), true);
70 putValue("help", ht("/Action/ReverseWays"));
71 }
72
73 public void actionPerformed(ActionEvent e) {
74 if (! isEnabled())
75 return;
76 if (getCurrentDataSet() == null)
77 return;
78
79 final Collection<Way> sel = getCurrentDataSet().getSelectedWays();
80 if (sel.isEmpty()) {
81 JOptionPane.showMessageDialog(
82 Main.parent,
83 tr("Please select at least one way."),
84 tr("Information"),
85 JOptionPane.INFORMATION_MESSAGE
86 );
87 return;
88 }
89
90 boolean propertiesUpdated = false;
91 Collection<Command> c = new LinkedList<Command>();
92 for (Way w : sel) {
93 ReverseWayResult revResult;
94 try {
95 revResult = reverseWay(w);
96 } catch (UserCancelException ex) {
97 return;
98 }
99 c.addAll(revResult.getCommands());
100 propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty();
101 }
102 Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
103 if (propertiesUpdated) {
104 getCurrentDataSet().fireSelectionChanged();
105 }
106 Main.map.repaint();
107 }
108
109 /**
110 * @param w the way
111 * @return the reverse command and the tag correction commands
112 * @throws UserCancelException if user cancels a reverse warning dialog
113 */
114 public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
115 ReverseWayNoTagCorrector.checkAndConfirmReverseWay(w);
116 Way wnew = new Way(w);
117 List<Node> nodesCopy = wnew.getNodes();
118 Collections.reverse(nodesCopy);
119 wnew.setNodes(nodesCopy);
120
121 Collection<Command> corrCmds = Collections.<Command>emptyList();
122 if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
123 corrCmds = (new ReverseWayTagCorrector()).execute(w, wnew);
124 }
125 return new ReverseWayResult(wnew, corrCmds, new ChangeCommand(w, wnew));
126 }
127
128 protected int getNumWaysInSelection() {
129 if (getCurrentDataSet() == null) return 0;
130 int ret = 0;
131 for (OsmPrimitive primitive : getCurrentDataSet().getSelected()) {
132 if (primitive instanceof Way) {
133 ret++;
134 }
135 }
136 return ret;
137 }
138
139 @Override
140 protected void updateEnabledState() {
141 if (getCurrentDataSet() == null) {
142 setEnabled(false);
143 } else {
144 updateEnabledState(getCurrentDataSet().getSelected());
145 }
146 }
147
148 @Override
149 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
150 setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
151 }
152}
Note: See TracBrowser for help on using the repository browser.