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

Last change on this file since 1847 was 1847, checked in by Gubaer, 15 years ago

replaced calls to JOptionPane.* by calls to OptionPaneUtil.*

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