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

Last change on this file since 10626 was 10548, checked in by simon04, 8 years ago

Remove duplicated code

Use updateEnabledStateOnCurrentSelection introduced r10409

  • Property svn:eol-style set to native
File size: 4.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.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.data.osm.DataSet;
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.UserCancelException;
30import org.openstreetmap.josm.tools.Utils;
31
32public final class ReverseWayAction extends JosmAction {
33
34 public static class ReverseWayResult {
35 private final Way newWay;
36 private final Collection<Command> tagCorrectionCommands;
37 private final Command reverseCommand;
38
39 public ReverseWayResult(Way newWay, Collection<Command> tagCorrectionCommands, Command reverseCommand) {
40 this.newWay = newWay;
41 this.tagCorrectionCommands = tagCorrectionCommands;
42 this.reverseCommand = reverseCommand;
43 }
44
45 public Way getNewWay() {
46 return newWay;
47 }
48
49 public Collection<Command> getCommands() {
50 List<Command> c = new ArrayList<>();
51 c.addAll(tagCorrectionCommands);
52 c.add(reverseCommand);
53 return c;
54 }
55
56 public Command getAsSequenceCommand() {
57 return new SequenceCommand(tr("Reverse way"), getCommands());
58 }
59
60 public Command getReverseCommand() {
61 return reverseCommand;
62 }
63
64 public Collection<Command> getTagCorrectionCommands() {
65 return tagCorrectionCommands;
66 }
67 }
68
69 public ReverseWayAction() {
70 super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
71 Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.DIRECT), true);
72 putValue("help", ht("/Action/ReverseWays"));
73 }
74
75 @Override
76 public void actionPerformed(ActionEvent e) {
77 DataSet ds = getLayerManager().getEditDataSet();
78 if (!isEnabled() || ds == null)
79 return;
80
81 final Collection<Way> sel = ds.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 Main.trace(ex);
99 return;
100 }
101 c.addAll(revResult.getCommands());
102 propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty();
103 }
104 Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
105 // FIXME: This should be handled by undoRedo.
106 if (propertiesUpdated) {
107 ds.fireSelectionChanged();
108 }
109 }
110
111 /**
112 * @param w the way
113 * @return the reverse command and the tag correction commands
114 * @throws UserCancelException if user cancels a reverse warning dialog
115 */
116 public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
117 ReverseWayNoTagCorrector.checkAndConfirmReverseWay(w);
118 Way wnew = new Way(w);
119 List<Node> nodesCopy = wnew.getNodes();
120 Collections.reverse(nodesCopy);
121 wnew.setNodes(nodesCopy);
122
123 Collection<Command> corrCmds = Collections.<Command>emptyList();
124 if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
125 corrCmds = (new ReverseWayTagCorrector()).execute(w, wnew);
126 }
127 return new ReverseWayResult(wnew, corrCmds, new ChangeCommand(w, wnew));
128 }
129
130 @Override
131 protected void updateEnabledState() {
132 updateEnabledStateOnCurrentSelection();
133 }
134
135 @Override
136 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
137 setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
138 }
139}
Note: See TracBrowser for help on using the repository browser.