| | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
| | 2 | package org.openstreetmap.josm.actions; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 5 | |
| | 6 | import java.awt.event.ActionEvent; |
| | 7 | import java.awt.event.KeyEvent; |
| | 8 | import java.util.Collection; |
| | 9 | import java.util.HashSet; |
| | 10 | import java.util.LinkedList; |
| | 11 | |
| | 12 | import javax.swing.JOptionPane; |
| | 13 | |
| | 14 | import org.openstreetmap.josm.Main; |
| | 15 | import org.openstreetmap.josm.command.Command; |
| | 16 | import org.openstreetmap.josm.command.ChangeCommand; |
| | 17 | import org.openstreetmap.josm.command.MoveCommand; |
| | 18 | import org.openstreetmap.josm.command.SequenceCommand; |
| | 19 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 20 | import org.openstreetmap.josm.data.osm.Node; |
| | 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 22 | import org.openstreetmap.josm.data.osm.Way; |
| | 23 | import org.openstreetmap.josm.tools.Shortcut; |
| | 24 | |
| | 25 | /** |
| | 26 | * Mirror the selected nodes or ways along the vertical axis |
| | 27 | * |
| | 28 | * Note: If a ways are selected, their nodes are mirrored |
| | 29 | * |
| | 30 | * @author Teemu Koskinen, based on much copy&Paste from other Actions. |
| | 31 | */ |
| | 32 | public final class MirrorAction extends JosmAction { |
| | 33 | |
| | 34 | public MirrorAction() { |
| | 35 | super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."), |
| | 36 | Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")), |
| | 37 | KeyEvent.VK_M, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); |
| | 38 | } |
| | 39 | |
| | 40 | public void actionPerformed(ActionEvent e) { |
| | 41 | Collection<OsmPrimitive> sel = Main.ds.getSelected(); |
| | 42 | HashSet<Node> nodes = new HashSet<Node>(); |
| | 43 | |
| | 44 | for (OsmPrimitive osm : sel) { |
| | 45 | if (osm instanceof Node) { |
| | 46 | nodes.add((Node)osm); |
| | 47 | } else if (osm instanceof Way) { |
| | 48 | nodes.addAll(((Way)osm).nodes); |
| | 49 | } |
| | 50 | } |
| | 51 | |
| | 52 | if (nodes.size() == 0) { |
| | 53 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one node or way.")); |
| | 54 | return; |
| | 55 | } |
| | 56 | |
| | 57 | double minEast = 200.0; |
| | 58 | double maxEast = -200.0; |
| | 59 | for (Node n : nodes) { |
| | 60 | minEast = Math.min(minEast, n.eastNorth.east()); |
| | 61 | maxEast = Math.max(maxEast, n.eastNorth.east()); |
| | 62 | } |
| | 63 | double middle = (minEast + maxEast) / 2; |
| | 64 | |
| | 65 | Collection<Command> cmds = new LinkedList<Command>(); |
| | 66 | |
| | 67 | for (Node n : nodes) |
| | 68 | cmds.add(new MoveCommand(n, 2 * (middle - n.eastNorth.east()), 0.0)); |
| | 69 | |
| | 70 | Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds)); |
| | 71 | Main.map.repaint(); |
| | 72 | } |
| | 73 | } |