1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.LinkedList;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.command.Command;
|
---|
17 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.Notification;
|
---|
23 | import 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 | */
|
---|
32 | public 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<Node>();
|
---|
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<Command>();
|
---|
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 | }
|
---|