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

Last change on this file since 1862 was 1862, checked in by jttt, 15 years ago

Way refactoring - added method that will in future replace public field nodes

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.LinkedList;
11import java.util.List;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.ChangeCommand;
17import org.openstreetmap.josm.command.Command;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
20import org.openstreetmap.josm.corrector.UserCancelException;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.gui.OptionPaneUtil;
26import org.openstreetmap.josm.tools.Shortcut;
27
28public 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}
Note: See TracBrowser for help on using the repository browser.