| 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 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 6 | |
|---|
| 7 | import java.awt.event.ActionEvent; |
|---|
| 8 | import java.awt.event.KeyEvent; |
|---|
| 9 | import java.util.Collection; |
|---|
| 10 | import java.util.HashSet; |
|---|
| 11 | import java.util.LinkedList; |
|---|
| 12 | |
|---|
| 13 | import javax.swing.JOptionPane; |
|---|
| 14 | |
|---|
| 15 | import org.openstreetmap.josm.Main; |
|---|
| 16 | import org.openstreetmap.josm.command.Command; |
|---|
| 17 | import org.openstreetmap.josm.command.MoveCommand; |
|---|
| 18 | import org.openstreetmap.josm.command.SequenceCommand; |
|---|
| 19 | import org.openstreetmap.josm.data.osm.Node; |
|---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 21 | import org.openstreetmap.josm.data.osm.Way; |
|---|
| 22 | import 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 | */ |
|---|
| 31 | public 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 | putValue("help", ht("/Action/Mirror")); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | public void actionPerformed(ActionEvent e) { |
|---|
| 41 | Collection<OsmPrimitive> sel = getCurrentDataSet().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).getNodes()); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | if (nodes.size() == 0) { |
|---|
| 53 | JOptionPane.showMessageDialog( |
|---|
| 54 | Main.parent, |
|---|
| 55 | tr("Please select at least one node or way."), |
|---|
| 56 | tr("Information"), |
|---|
| 57 | JOptionPane.INFORMATION_MESSAGE |
|---|
| 58 | ); |
|---|
| 59 | return; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | double minEast = 20000000000.0; |
|---|
| 63 | double maxEast = -20000000000.0; |
|---|
| 64 | for (Node n : nodes) { |
|---|
| 65 | double east = n.getEastNorth().east(); |
|---|
| 66 | minEast = Math.min(minEast, east); |
|---|
| 67 | maxEast = Math.max(maxEast, east); |
|---|
| 68 | } |
|---|
| 69 | double middle = (minEast + maxEast) / 2; |
|---|
| 70 | |
|---|
| 71 | Collection<Command> cmds = new LinkedList<Command>(); |
|---|
| 72 | |
|---|
| 73 | for (Node n : nodes) { |
|---|
| 74 | cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0)); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds)); |
|---|
| 78 | Main.map.repaint(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | @Override |
|---|
| 82 | protected void updateEnabledState() { |
|---|
| 83 | if (getCurrentDataSet() == null) { |
|---|
| 84 | setEnabled(false); |
|---|
| 85 | } else { |
|---|
| 86 | updateEnabledState(getCurrentDataSet().getSelected()); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | @Override |
|---|
| 91 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
|---|
| 92 | setEnabled(selection != null && !selection.isEmpty()); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|