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

Last change on this file since 8790 was 8443, checked in by Don-vip, 9 years ago

remove extra whitespaces

  • Property svn:eol-style set to native
File size: 5.2 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.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.gui.Notification;
28import org.openstreetmap.josm.tools.Shortcut;
29import org.openstreetmap.josm.tools.Utils;
30
31public final class ReverseWayAction extends JosmAction {
32
33 public static class ReverseWayResult {
34 private Way newWay;
35 private Collection<Command> tagCorrectionCommands;
36 private 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 protected int getNumWaysInSelection() {
130 if (getCurrentDataSet() == null) return 0;
131 int ret = 0;
132 for (OsmPrimitive primitive : getCurrentDataSet().getSelected()) {
133 if (primitive instanceof Way) {
134 ret++;
135 }
136 }
137 return ret;
138 }
139
140 @Override
141 protected void updateEnabledState() {
142 if (getCurrentDataSet() == null) {
143 setEnabled(false);
144 } else {
145 updateEnabledState(getCurrentDataSet().getSelected());
146 }
147 }
148
149 @Override
150 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
151 setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
152 }
153}
Note: See TracBrowser for help on using the repository browser.