1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
19 | import org.openstreetmap.josm.command.Command;
|
---|
20 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
21 | import org.openstreetmap.josm.corrector.ReverseWayNoTagCorrector;
|
---|
22 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
23 | import org.openstreetmap.josm.corrector.UserCancelException;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
28 | import org.openstreetmap.josm.tools.Utils;
|
---|
29 |
|
---|
30 | public 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 | }
|
---|