| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 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.Set;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.JOptionPane;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.command.Command;
|
|---|
| 16 | import org.openstreetmap.josm.command.MoveCommand;
|
|---|
| 17 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 18 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 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.gui.Notification;
|
|---|
| 23 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 24 | import org.openstreetmap.josm.tools.StreamUtils;
|
|---|
| 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 | *
|
|---|
| 31 | * @author Teemu Koskinen
|
|---|
| 32 | */
|
|---|
| 33 | public final class MirrorAction extends JosmAction {
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Constructs a new {@code MirrorAction}.
|
|---|
| 37 | */
|
|---|
| 38 | public MirrorAction() {
|
|---|
| 39 | super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."),
|
|---|
| 40 | Shortcut.registerShortcut("tools:mirror", tr("Tools: {0}", tr("Mirror")),
|
|---|
| 41 | KeyEvent.VK_M, Shortcut.SHIFT), true);
|
|---|
| 42 | setHelpId(ht("/Action/Mirror"));
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | @Override
|
|---|
| 46 | public void actionPerformed(ActionEvent e) {
|
|---|
| 47 | Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getSelected();
|
|---|
| 48 | Set<Node> nodes = new HashSet<>();
|
|---|
| 49 |
|
|---|
| 50 | for (OsmPrimitive osm : sel) {
|
|---|
| 51 | if (osm instanceof Node) {
|
|---|
| 52 | nodes.add((Node) osm);
|
|---|
| 53 | } else if (osm instanceof Way) {
|
|---|
| 54 | nodes.addAll(((Way) osm).getNodes());
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if (nodes.isEmpty()) {
|
|---|
| 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();
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | double minEast = 20000000000.0;
|
|---|
| 68 | double maxEast = -20000000000.0;
|
|---|
| 69 | for (Node n : nodes) {
|
|---|
| 70 | double east = n.getEastNorth().east();
|
|---|
| 71 | minEast = Math.min(minEast, east);
|
|---|
| 72 | maxEast = Math.max(maxEast, east);
|
|---|
| 73 | }
|
|---|
| 74 | double middle = (minEast + maxEast) / 2;
|
|---|
| 75 |
|
|---|
| 76 | Collection<Command> cmds = nodes.stream()
|
|---|
| 77 | .map(n -> new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0))
|
|---|
| 78 | .collect(StreamUtils.toUnmodifiableList());
|
|---|
| 79 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Mirror"), cmds));
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | @Override
|
|---|
| 83 | protected void updateEnabledState() {
|
|---|
| 84 | updateEnabledStateOnCurrentSelection();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @Override
|
|---|
| 88 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 89 | updateEnabledStateOnModifiableSelection(selection);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|