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

Last change on this file since 7083 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • 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;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.command.MoveCommand;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Way;
22import org.openstreetmap.josm.gui.Notification;
23import org.openstreetmap.josm.tools.Shortcut;
24
25/**
26 * Mirror the selected nodes or ways along the vertical axis
27 *
28 * Note: If a ways are selected, their nodes are mirrored
29 *
30 * @author Teemu Koskinen
31 */
32public final class MirrorAction extends JosmAction {
33
34 /**
35 * Constructs a new {@code MirrorAction}.
36 */
37 public MirrorAction() {
38 super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."),
39 Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")),
40 KeyEvent.VK_M, Shortcut.SHIFT), true);
41 putValue("help", ht("/Action/Mirror"));
42 }
43
44 @Override
45 public void actionPerformed(ActionEvent e) {
46 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
47 HashSet<Node> nodes = new HashSet<>();
48
49 for (OsmPrimitive osm : sel) {
50 if (osm instanceof Node) {
51 nodes.add((Node)osm);
52 } else if (osm instanceof Way) {
53 nodes.addAll(((Way)osm).getNodes());
54 }
55 }
56
57 if (nodes.isEmpty()) {
58 new Notification(
59 tr("Please select at least one node or way."))
60 .setIcon(JOptionPane.INFORMATION_MESSAGE)
61 .setDuration(Notification.TIME_SHORT)
62 .show();
63 return;
64 }
65
66 double minEast = 20000000000.0;
67 double maxEast = -20000000000.0;
68 for (Node n : nodes) {
69 double east = n.getEastNorth().east();
70 minEast = Math.min(minEast, east);
71 maxEast = Math.max(maxEast, east);
72 }
73 double middle = (minEast + maxEast) / 2;
74
75 Collection<Command> cmds = new LinkedList<>();
76
77 for (Node n : nodes) {
78 cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
79 }
80
81 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
82 Main.map.repaint();
83 }
84
85 @Override
86 protected void updateEnabledState() {
87 if (getCurrentDataSet() == null) {
88 setEnabled(false);
89 } else {
90 updateEnabledState(getCurrentDataSet().getSelected());
91 }
92 }
93
94 @Override
95 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
96 setEnabled(selection != null && !selection.isEmpty());
97 }
98}
Note: See TracBrowser for help on using the repository browser.