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 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
17 | import org.openstreetmap.josm.command.Command;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
20 | import org.openstreetmap.josm.corrector.UserCancelException;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
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 | if (! isEnabled())
|
---|
37 | return;
|
---|
38 | if (getCurrentDataSet() == null)
|
---|
39 | return;
|
---|
40 |
|
---|
41 | final Collection<Way> sel = new LinkedList<Way>();
|
---|
42 | for (OsmPrimitive primitive : getCurrentDataSet().getSelected()) {
|
---|
43 | if (primitive instanceof Way) {
|
---|
44 | sel.add((Way)primitive);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | if (sel.isEmpty()) {
|
---|
48 | OptionPaneUtil.showMessageDialog(
|
---|
49 | Main.parent,
|
---|
50 | tr("Please select at least one way."),
|
---|
51 | tr("Information"),
|
---|
52 | JOptionPane.INFORMATION_MESSAGE
|
---|
53 | );
|
---|
54 | return;
|
---|
55 | }
|
---|
56 |
|
---|
57 | boolean propertiesUpdated = false;
|
---|
58 | ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();
|
---|
59 | Collection<Command> c = new LinkedList<Command>();
|
---|
60 | for (Way w : sel) {
|
---|
61 | Way wnew = new Way(w);
|
---|
62 | List<Node> nodesCopy = wnew.getNodes();
|
---|
63 | Collections.reverse(nodesCopy);
|
---|
64 | wnew.setNodes(nodesCopy);
|
---|
65 | if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
|
---|
66 | try
|
---|
67 | {
|
---|
68 | final Collection<Command> changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
|
---|
69 | propertiesUpdated = propertiesUpdated
|
---|
70 | || (changePropertyCommands != null && !changePropertyCommands.isEmpty());
|
---|
71 | c.addAll(changePropertyCommands);
|
---|
72 | }
|
---|
73 | catch(UserCancelException ex)
|
---|
74 | {
|
---|
75 | return;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | c.add(new ChangeCommand(w, wnew));
|
---|
79 | }
|
---|
80 | Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
|
---|
81 | if (propertiesUpdated) {
|
---|
82 | DataSet.fireSelectionChanged(getCurrentDataSet().getSelected());
|
---|
83 | }
|
---|
84 | Main.map.repaint();
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected int getNumWaysInSelection() {
|
---|
88 | if (getCurrentDataSet() == null) return 0;
|
---|
89 | int ret = 0;
|
---|
90 | for (OsmPrimitive primitive : getCurrentDataSet().getSelected()) {
|
---|
91 | if (primitive instanceof Way) {
|
---|
92 | ret++;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | return ret;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | protected void updateEnabledState() {
|
---|
100 | setEnabled(getNumWaysInSelection() > 0);
|
---|
101 | }
|
---|
102 | }
|
---|