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

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

JosmAction is now a LayerChangeListener and a SelectionChangeListener
updated all JosmActions
fixed #3018: Make sure tools menu entries (and actions) are deactivated when no layer

File size: 2.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.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(Main.parent, tr("Please select at least one node or way."));
52 return;
53 }
54
55 double minEast = 20000000000.0;
56 double maxEast = -20000000000.0;
57 for (Node n : nodes) {
58 double east = n.getEastNorth().east();
59 minEast = Math.min(minEast, east);
60 maxEast = Math.max(maxEast, east);
61 }
62 double middle = (minEast + maxEast) / 2;
63
64 Collection<Command> cmds = new LinkedList<Command>();
65
66 for (Node n : nodes) {
67 cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
68 }
69
70 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
71 Main.map.repaint();
72 }
73
74 @Override
75 protected void updateEnabledState() {
76 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
77 }
78}
Note: See TracBrowser for help on using the repository browser.