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

Last change on this file since 10446 was 10382, checked in by Don-vip, 8 years ago

see #12943 - gsoc-core - fix most of deprecation warnings (static accesses must be fixed)

  • Property svn:eol-style set to native
File size: 4.9 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 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 ds.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 @Override
130 protected void updateEnabledState() {
131 DataSet ds = getLayerManager().getEditDataSet();
132 if (ds == null) {
133 setEnabled(false);
134 } else {
135 updateEnabledState(ds.getSelected());
136 }
137 }
138
139 @Override
140 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
141 setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
142 }
143}
Note: See TracBrowser for help on using the repository browser.