source: josm/trunk/src/org/openstreetmap/josm/actions/MirrorAction.java@ 11257

Last change on this file since 11257 was 10467, checked in by Don-vip, 8 years ago

fix #13037 - Small fixes for unit tests (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 3.0 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[1639]2package org.openstreetmap.josm.actions;
3
[6130]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[1639]5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.LinkedList;
[7859]12import java.util.Set;
[1639]13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.command.Command;
18import org.openstreetmap.josm.command.MoveCommand;
19import org.openstreetmap.josm.command.SequenceCommand;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.Way;
[6130]23import org.openstreetmap.josm.gui.Notification;
[1639]24import 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 */
33public 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
82 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
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}
Note: See TracBrowser for help on using the repository browser.