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.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
18 | import org.openstreetmap.josm.command.Command;
|
---|
19 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
20 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
21 | import org.openstreetmap.josm.corrector.UserCancelException;
|
---|
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.tools.Shortcut;
|
---|
26 |
|
---|
27 | public final class ReverseWayAction extends JosmAction {
|
---|
28 |
|
---|
29 | public ReverseWayAction() {
|
---|
30 | super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
|
---|
31 | Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.GROUP_EDIT), true);
|
---|
32 | putValue("help", ht("/Action/ReverseWay"));
|
---|
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 = getCurrentDataSet().getSelectedWays();
|
---|
42 | if (sel.isEmpty()) {
|
---|
43 | JOptionPane.showMessageDialog(
|
---|
44 | Main.parent,
|
---|
45 | tr("Please select at least one way."),
|
---|
46 | tr("Information"),
|
---|
47 | JOptionPane.INFORMATION_MESSAGE
|
---|
48 | );
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | boolean propertiesUpdated = false;
|
---|
53 | ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();
|
---|
54 | Collection<Command> c = new LinkedList<Command>();
|
---|
55 | for (Way w : sel) {
|
---|
56 | Way wnew = new Way(w);
|
---|
57 | List<Node> nodesCopy = wnew.getNodes();
|
---|
58 | Collections.reverse(nodesCopy);
|
---|
59 | wnew.setNodes(nodesCopy);
|
---|
60 | if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
|
---|
61 | try
|
---|
62 | {
|
---|
63 | final Collection<Command> changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
|
---|
64 | propertiesUpdated = propertiesUpdated
|
---|
65 | || (changePropertyCommands != null && !changePropertyCommands.isEmpty());
|
---|
66 | c.addAll(changePropertyCommands);
|
---|
67 | }
|
---|
68 | catch(UserCancelException ex)
|
---|
69 | {
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | c.add(new ChangeCommand(w, wnew));
|
---|
74 | }
|
---|
75 | Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
|
---|
76 | if (propertiesUpdated) {
|
---|
77 | getCurrentDataSet().fireSelectionChanged();
|
---|
78 | }
|
---|
79 | Main.map.repaint();
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected int getNumWaysInSelection() {
|
---|
83 | if (getCurrentDataSet() == null) return 0;
|
---|
84 | int ret = 0;
|
---|
85 | for (OsmPrimitive primitive : getCurrentDataSet().getSelected()) {
|
---|
86 | if (primitive instanceof Way) {
|
---|
87 | ret++;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return ret;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | protected void updateEnabledState() {
|
---|
95 | if (getCurrentDataSet() == null) {
|
---|
96 | setEnabled(false);
|
---|
97 | } else {
|
---|
98 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
104 | if (selection == null) {
|
---|
105 | setEnabled(false);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | int n = 0;
|
---|
109 | for (OsmPrimitive primitive : selection) {
|
---|
110 | if (primitive instanceof Way) {
|
---|
111 | n++;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | setEnabled(n > 0);
|
---|
115 | }
|
---|
116 | }
|
---|