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