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

Last change on this file since 8444 was 7859, checked in by Don-vip, 9 years ago

fix various Sonar issues, improve Javadoc

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
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;
12import java.util.Set;
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;
23import org.openstreetmap.josm.gui.Notification;
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 *
31 * @author Teemu Koskinen
32 */
33public 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("Tool: {0}", tr("Mirror")),
41 KeyEvent.VK_M, Shortcut.SHIFT), true);
42 putValue("help", ht("/Action/Mirror"));
43 }
44
45 @Override
46 public void actionPerformed(ActionEvent e) {
47 Collection<OsmPrimitive> sel = getCurrentDataSet().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 = new LinkedList<>();
77
78 for (Node n : nodes) {
79 cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
80 }
81
82 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
83 Main.map.repaint();
84 }
85
86 @Override
87 protected void updateEnabledState() {
88 if (getCurrentDataSet() == null) {
89 setEnabled(false);
90 } else {
91 updateEnabledState(getCurrentDataSet().getSelected());
92 }
93 }
94
95 @Override
96 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
97 setEnabled(selection != null && !selection.isEmpty());
98 }
99}
Note: See TracBrowser for help on using the repository browser.