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

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

update license/copyright information

  • 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, based on much copy&Paste from other Actions.
31 */
32public final class MirrorAction extends JosmAction {
33
34 public MirrorAction() {
35 super(tr("Mirror"), "mirror", tr("Mirror selected nodes and ways."),
36 Shortcut.registerShortcut("tools:mirror", tr("Tool: {0}", tr("Mirror")),
37 KeyEvent.VK_M, Shortcut.SHIFT), true);
38 putValue("help", ht("/Action/Mirror"));
39 }
40
41 @Override
42 public void actionPerformed(ActionEvent e) {
43 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
44 HashSet<Node> nodes = new HashSet<Node>();
45
46 for (OsmPrimitive osm : sel) {
47 if (osm instanceof Node) {
48 nodes.add((Node)osm);
49 } else if (osm instanceof Way) {
50 nodes.addAll(((Way)osm).getNodes());
51 }
52 }
53
54 if (nodes.isEmpty()) {
55 new Notification(
56 tr("Please select at least one node or way."))
57 .setIcon(JOptionPane.INFORMATION_MESSAGE)
58 .setDuration(Notification.TIME_SHORT)
59 .show();
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.