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

Last change on this file since 2610 was 2323, checked in by Gubaer, 15 years ago

Added explicit help topics
See also current list of help topics with links to source files and to help pages

File size: 3.1 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.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
37 putValue("help", ht("/Action/Mirror"));
38 }
39
40 public void actionPerformed(ActionEvent e) {
41 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
42 HashSet<Node> nodes = new HashSet<Node>();
43
44 for (OsmPrimitive osm : sel) {
45 if (osm instanceof Node) {
46 nodes.add((Node)osm);
47 } else if (osm instanceof Way) {
48 nodes.addAll(((Way)osm).getNodes());
49 }
50 }
51
52 if (nodes.size() == 0) {
53 JOptionPane.showMessageDialog(
54 Main.parent,
55 tr("Please select at least one node or way."),
56 tr("Information"),
57 JOptionPane.INFORMATION_MESSAGE
58 );
59 return;
60 }
61
62 double minEast = 20000000000.0;
63 double maxEast = -20000000000.0;
64 for (Node n : nodes) {
65 double east = n.getEastNorth().east();
66 minEast = Math.min(minEast, east);
67 maxEast = Math.max(maxEast, east);
68 }
69 double middle = (minEast + maxEast) / 2;
70
71 Collection<Command> cmds = new LinkedList<Command>();
72
73 for (Node n : nodes) {
74 cmds.add(new MoveCommand(n, 2 * (middle - n.getEastNorth().east()), 0.0));
75 }
76
77 Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
78 Main.map.repaint();
79 }
80
81 @Override
82 protected void updateEnabledState() {
83 if (getCurrentDataSet() == null) {
84 setEnabled(false);
85 } else {
86 updateEnabledState(getCurrentDataSet().getSelected());
87 }
88 }
89
90 @Override
91 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
92 setEnabled(selection != null && !selection.isEmpty());
93 }
94}
Note: See TracBrowser for help on using the repository browser.