[1639] | 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;
|
---|
[2323] | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1639] | 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."),
|
---|
[1814] | 35 | Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")),
|
---|
[4982] | 36 | KeyEvent.VK_M, Shortcut.SHIFT), true);
|
---|
[2323] | 37 | putValue("help", ht("/Action/Mirror"));
|
---|
[1639] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public void actionPerformed(ActionEvent e) {
|
---|
[1814] | 41 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
[1639] | 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) {
|
---|
[1862] | 48 | nodes.addAll(((Way)osm).getNodes());
|
---|
[1639] | 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | if (nodes.size() == 0) {
|
---|
[2017] | 53 | JOptionPane.showMessageDialog(
|
---|
[1847] | 54 | Main.parent,
|
---|
| 55 | tr("Please select at least one node or way."),
|
---|
| 56 | tr("Information"),
|
---|
| 57 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 58 | );
|
---|
[1639] | 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[1722] | 62 | double minEast = 20000000000.0;
|
---|
| 63 | double maxEast = -20000000000.0;
|
---|
[1639] | 64 | for (Node n : nodes) {
|
---|
[1722] | 65 | double east = n.getEastNorth().east();
|
---|
| 66 | minEast = Math.min(minEast, east);
|
---|
| 67 | maxEast = Math.max(maxEast, east);
|
---|
[1639] | 68 | }
|
---|
| 69 | double middle = (minEast + maxEast) / 2;
|
---|
| 70 |
|
---|
| 71 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
| 72 |
|
---|
[1814] | 73 | for (Node n : nodes) {
|
---|
[1722] | 74 | cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
|
---|
[1814] | 75 | }
|
---|
[1639] | 76 |
|
---|
| 77 | Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
|
---|
| 78 | Main.map.repaint();
|
---|
| 79 | }
|
---|
[1820] | 80 |
|
---|
| 81 | @Override
|
---|
| 82 | protected void updateEnabledState() {
|
---|
[2256] | 83 | if (getCurrentDataSet() == null) {
|
---|
| 84 | setEnabled(false);
|
---|
| 85 | } else {
|
---|
| 86 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
| 87 | }
|
---|
[1820] | 88 | }
|
---|
[2256] | 89 |
|
---|
| 90 | @Override
|
---|
| 91 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 92 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 93 | }
|
---|
[1639] | 94 | }
|
---|