[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1639] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[6130] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1639] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 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;
|
---|
[7859] | 12 | import java.util.Set;
|
---|
[1639] | 13 |
|
---|
| 14 | import javax.swing.JOptionPane;
|
---|
| 15 |
|
---|
| 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;
|
---|
[12641] | 22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[6130] | 23 | import org.openstreetmap.josm.gui.Notification;
|
---|
[1639] | 24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Mirror the selected nodes or ways along the vertical axis
|
---|
| 28 | *
|
---|
| 29 | * Note: If a ways are selected, their nodes are mirrored
|
---|
| 30 | *
|
---|
[6830] | 31 | * @author Teemu Koskinen
|
---|
[1639] | 32 | */
|
---|
| 33 | public final class MirrorAction extends JosmAction {
|
---|
| 34 |
|
---|
[6830] | 35 | /**
|
---|
| 36 | * Constructs a new {@code MirrorAction}.
|
---|
| 37 | */
|
---|
[1639] | 38 | public MirrorAction() {
|
---|
| 39 | super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."),
|
---|
[1814] | 40 | Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")),
|
---|
[4982] | 41 | KeyEvent.VK_M, Shortcut.SHIFT), true);
|
---|
[2323] | 42 | putValue("help", ht("/Action/Mirror"));
|
---|
[1639] | 43 | }
|
---|
| 44 |
|
---|
[6084] | 45 | @Override
|
---|
[1639] | 46 | public void actionPerformed(ActionEvent e) {
|
---|
[10382] | 47 | Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getSelected();
|
---|
[7859] | 48 | Set<Node> nodes = new HashSet<>();
|
---|
[1639] | 49 |
|
---|
| 50 | for (OsmPrimitive osm : sel) {
|
---|
| 51 | if (osm instanceof Node) {
|
---|
[8510] | 52 | nodes.add((Node) osm);
|
---|
[1639] | 53 | } else if (osm instanceof Way) {
|
---|
[8510] | 54 | nodes.addAll(((Way) osm).getNodes());
|
---|
[1639] | 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[6093] | 58 | if (nodes.isEmpty()) {
|
---|
[6130] | 59 | new Notification(
|
---|
| 60 | tr("Please select at least one node or way."))
|
---|
| 61 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
| 62 | .setDuration(Notification.TIME_SHORT)
|
---|
| 63 | .show();
|
---|
[1639] | 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[1722] | 67 | double minEast = 20000000000.0;
|
---|
| 68 | double maxEast = -20000000000.0;
|
---|
[1639] | 69 | for (Node n : nodes) {
|
---|
[1722] | 70 | double east = n.getEastNorth().east();
|
---|
| 71 | minEast = Math.min(minEast, east);
|
---|
| 72 | maxEast = Math.max(maxEast, east);
|
---|
[1639] | 73 | }
|
---|
| 74 | double middle = (minEast + maxEast) / 2;
|
---|
| 75 |
|
---|
[7005] | 76 | Collection<Command> cmds = new LinkedList<>();
|
---|
[1639] | 77 |
|
---|
[1814] | 78 | for (Node n : nodes) {
|
---|
[1722] | 79 | cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
|
---|
[1814] | 80 | }
|
---|
[1639] | 81 |
|
---|
[12641] | 82 | MainApplication.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
|
---|
[1639] | 83 | }
|
---|
[1820] | 84 |
|
---|
| 85 | @Override
|
---|
| 86 | protected void updateEnabledState() {
|
---|
[10409] | 87 | updateEnabledStateOnCurrentSelection();
|
---|
[1820] | 88 | }
|
---|
[2256] | 89 |
|
---|
| 90 | @Override
|
---|
| 91 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 92 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 93 | }
|
---|
[1639] | 94 | }
|
---|