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

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

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 4.9 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[343]2package org.openstreetmap.josm.actions;
3
[2633]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[343]5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
[3504]9import java.util.ArrayList;
[343]10import java.util.Collection;
11import java.util.Collections;
12import java.util.LinkedList;
[1862]13import java.util.List;
[343]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;
[5724]21import org.openstreetmap.josm.corrector.ReverseWayNoTagCorrector;
[729]22import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
[1862]23import org.openstreetmap.josm.data.osm.Node;
[343]24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.Way;
[6130]26import org.openstreetmap.josm.gui.Notification;
[1084]27import org.openstreetmap.josm.tools.Shortcut;
[8919]28import org.openstreetmap.josm.tools.UserCancelException;
[3504]29import org.openstreetmap.josm.tools.Utils;
[343]30
[1844]31public final class ReverseWayAction extends JosmAction {
[343]32
[3504]33 public static class ReverseWayResult {
[9067]34 private final Way newWay;
35 private final Collection<Command> tagCorrectionCommands;
36 private final Command reverseCommand;
[3504]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() {
[7005]49 List<Command> c = new ArrayList<>();
[3504]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
[1169]68 public ReverseWayAction() {
[1218]69 super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
[4982]70 Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.DIRECT), true);
[3504]71 putValue("help", ht("/Action/ReverseWays"));
[1169]72 }
[343]73
[6084]74 @Override
[1169]75 public void actionPerformed(ActionEvent e) {
[8443]76 if (!isEnabled())
[1820]77 return;
78 if (getCurrentDataSet() == null)
79 return;
80
[2633]81 final Collection<Way> sel = getCurrentDataSet().getSelectedWays();
[1169]82 if (sel.isEmpty()) {
[6130]83 new Notification(
84 tr("Please select at least one way."))
85 .setIcon(JOptionPane.INFORMATION_MESSAGE)
86 .setDuration(Notification.TIME_SHORT)
87 .show();
[1169]88 return;
89 }
[729]90
[1169]91 boolean propertiesUpdated = false;
[7005]92 Collection<Command> c = new LinkedList<>();
[1169]93 for (Way w : sel) {
[3504]94 ReverseWayResult revResult;
95 try {
96 revResult = reverseWay(w);
97 } catch (UserCancelException ex) {
98 return;
[1169]99 }
[3504]100 c.addAll(revResult.getCommands());
101 propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty();
[1169]102 }
103 Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
[1814]104 if (propertiesUpdated) {
[2348]105 getCurrentDataSet().fireSelectionChanged();
[1814]106 }
[1169]107 Main.map.repaint();
108 }
[1820]109
[3504]110 /**
111 * @param w the way
112 * @return the reverse command and the tag correction commands
[5724]113 * @throws UserCancelException if user cancels a reverse warning dialog
[3504]114 */
115 public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
[5724]116 ReverseWayNoTagCorrector.checkAndConfirmReverseWay(w);
[3504]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
[1844]129 @Override
130 protected void updateEnabledState() {
[2256]131 if (getCurrentDataSet() == null) {
132 setEnabled(false);
133 } else {
134 updateEnabledState(getCurrentDataSet().getSelected());
135 }
[1820]136 }
[2256]137
138 @Override
139 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
[3504]140 setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
[2256]141 }
[343]142}
Note: See TracBrowser for help on using the repository browser.