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

Last change on this file since 6087 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
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.tools.Shortcut;
23
24/**
25 * Mirror the selected nodes or ways along the vertical axis
26 *
27 * Note: If a ways are selected, their nodes are mirrored
28 *
29 * @author Teemu Koskinen, based on much copy&Paste from other Actions.
30 */
31public final class MirrorAction extends JosmAction {
32
33 public MirrorAction() {
34 super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."),
35 Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")),
36 KeyEvent.VK_M, Shortcut.SHIFT), true);
37 putValue("help", ht("/Action/Mirror"));
38 }
39
40 @Override
41 public void actionPerformed(ActionEvent e) {
42 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
43 HashSet<Node> nodes = new HashSet<Node>();
44
45 for (OsmPrimitive osm : sel) {
46 if (osm instanceof Node) {
47 nodes.add((Node)osm);
48 } else if (osm instanceof Way) {
49 nodes.addAll(((Way)osm).getNodes());
50 }
51 }
52
53 if (nodes.size() == 0) {
54 JOptionPane.showMessageDialog(
55 Main.parent,
56 tr("Please select at least one node or way."),
57 tr("Information"),
58 JOptionPane.INFORMATION_MESSAGE
59 );
60 return;
61 }
62
63 double minEast = 20000000000.0;
64 double maxEast = -20000000000.0;
65 for (Node n : nodes) {
66 double east = n.getEastNorth().east();
67 minEast = Math.min(minEast, east);
68 maxEast = Math.max(maxEast, east);
69 }
70 double middle = (minEast + maxEast) / 2;
71
72 Collection<Command> cmds = new LinkedList<Command>();
73
74 for (Node n : nodes) {
75 cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
76 }
77
78 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
79 Main.map.repaint();
80 }
81
82 @Override
83 protected void updateEnabledState() {
84 if (getCurrentDataSet() == null) {
85 setEnabled(false);
86 } else {
87 updateEnabledState(getCurrentDataSet().getSelected());
88 }
89 }
90
91 @Override
92 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
93 setEnabled(selection != null && !selection.isEmpty());
94 }
95}
Note: See TracBrowser for help on using the repository browser.