source: josm/trunk/src/org/openstreetmap/josm/actions/MirrorAction.java@ 2001

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

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

File size: 2.8 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.HashSet;
10import java.util.LinkedList;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.command.MoveCommand;
17import org.openstreetmap.josm.command.SequenceCommand;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.gui.OptionPaneUtil;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * Mirror the selected nodes or ways along the vertical axis
26 *
27 * Note: If a ways are selected, their nodes are mirrored
28 *
29 * @author Teemu Koskinen, based on much copy&Paste from other Actions.
30 */
31public final class MirrorAction extends JosmAction {
32
33 public MirrorAction() {
34 super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."),
35 Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")),
36 KeyEvent.VK_M, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
37 }
38
39 public void actionPerformed(ActionEvent e) {
40 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
41 HashSet<Node> nodes = new HashSet<Node>();
42
43 for (OsmPrimitive osm : sel) {
44 if (osm instanceof Node) {
45 nodes.add((Node)osm);
46 } else if (osm instanceof Way) {
47 nodes.addAll(((Way)osm).getNodes());
48 }
49 }
50
51 if (nodes.size() == 0) {
52 OptionPaneUtil.showMessageDialog(
53 Main.parent,
54 tr("Please select at least one node or way."),
55 tr("Information"),
56 JOptionPane.INFORMATION_MESSAGE
57 );
58 return;
59 }
60
61 double minEast = 20000000000.0;
62 double maxEast = -20000000000.0;
63 for (Node n : nodes) {
64 double east = n.getEastNorth().east();
65 minEast = Math.min(minEast, east);
66 maxEast = Math.max(maxEast, east);
67 }
68 double middle = (minEast + maxEast) / 2;
69
70 Collection<Command> cmds = new LinkedList<Command>();
71
72 for (Node n : nodes) {
73 cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
74 }
75
76 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
77 Main.map.repaint();
78 }
79
80 @Override
81 protected void updateEnabledState() {
82 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
83 }
84}
Note: See TracBrowser for help on using the repository browser.