1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.LinkedList;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
16 | import org.openstreetmap.josm.command.Command;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
18 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
19 | import org.openstreetmap.josm.corrector.UserCancelException;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | public final class ReverseWayAction extends JosmAction {
|
---|
29 |
|
---|
30 | public ReverseWayAction() {
|
---|
31 | super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
|
---|
32 | Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.GROUP_EDIT), true);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void actionPerformed(ActionEvent e) {
|
---|
36 | final Collection<Way> sel = new LinkedList<Way>();
|
---|
37 | new AbstractVisitor() {
|
---|
38 | public void visit(Node n) {
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void visit(Way w) {
|
---|
42 | sel.add(w);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void visit(Relation e) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void visitAll() {
|
---|
49 | for (OsmPrimitive osm : Main.ds.getSelected())
|
---|
50 | osm.visit(this);
|
---|
51 | }
|
---|
52 | }.visitAll();
|
---|
53 |
|
---|
54 | if (sel.isEmpty()) {
|
---|
55 | JOptionPane.showMessageDialog(Main.parent,
|
---|
56 | tr("Please select at least one way."));
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | boolean propertiesUpdated = false;
|
---|
61 | ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();
|
---|
62 | Collection<Command> c = new LinkedList<Command>();
|
---|
63 | for (Way w : sel) {
|
---|
64 | Way wnew = new Way(w);
|
---|
65 | Collections.reverse(wnew.nodes);
|
---|
66 | if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
|
---|
67 | try
|
---|
68 | {
|
---|
69 | final Collection<Command> changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
|
---|
70 | propertiesUpdated = propertiesUpdated
|
---|
71 | || (changePropertyCommands != null && !changePropertyCommands.isEmpty());
|
---|
72 | c.addAll(changePropertyCommands);
|
---|
73 | }
|
---|
74 | catch(UserCancelException ex)
|
---|
75 | {
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | c.add(new ChangeCommand(w, wnew));
|
---|
80 | }
|
---|
81 | Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
|
---|
82 | if (propertiesUpdated)
|
---|
83 | DataSet.fireSelectionChanged(Main.ds.getSelected());
|
---|
84 | Main.map.repaint();
|
---|
85 | }
|
---|
86 | }
|
---|