source: osm/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/SymmetryAction.java

Last change on this file was 35579, checked in by Klumbumbus, 4 years ago

see #19851 - Fix shortcut names

File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.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.actions.JosmAction;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.command.MoveCommand;
18import org.openstreetmap.josm.command.SequenceCommand;
19import org.openstreetmap.josm.data.UndoRedoHandler;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.MainApplication;
25import org.openstreetmap.josm.gui.Notification;
26import org.openstreetmap.josm.tools.Shortcut;
27
28/**
29 * Mirror the selected ways nodes or ways along line given by two first selected points
30 *
31 * Note: If a ways are selected, their nodes are mirrored
32 *
33 * @author Alexei Kasatkin, based on much copy+paste from other MirrorAction :)
34 */
35public final class SymmetryAction extends JosmAction {
36
37 /**
38 * Constructs a new {@code SymmetryAction}.
39 */
40 public SymmetryAction() {
41 super(tr("Symmetry"), "symmetry", tr("Mirror selected nodes and ways."),
42 Shortcut.registerShortcut("tools:symmetry", tr("More tools: {0}", tr("Symmetry")),
43 KeyEvent.VK_S, Shortcut.ALT_SHIFT), true);
44 putValue("help", ht("/Action/Symmetry"));
45 }
46
47 @Override
48 public void actionPerformed(ActionEvent e) {
49 Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getSelected();
50 HashSet<Node> nodes = new HashSet<>();
51 EastNorth p1 = null, p2 = null;
52
53 for (OsmPrimitive osm : sel) {
54 if (osm instanceof Node) {
55 if (p1 == null) p1 = ((Node) osm).getEastNorth(); else
56 if (p2 == null) p2 = ((Node) osm).getEastNorth(); else
57 nodes.add((Node) osm);
58 }
59 }
60 for (OsmPrimitive osm : sel) {
61 if (osm instanceof Way) {
62 nodes.addAll(((Way) osm).getNodes());
63 }
64 }
65
66 if (p1 == null || p2 == null || nodes.size() < 1) {
67 new Notification(
68 tr("Please select at least two nodes for symmetry axis and something else to mirror.")
69 ).setIcon(JOptionPane.WARNING_MESSAGE).show();
70 return;
71 }
72
73 double ne, nn, l, e0, n0;
74 e0 = p1.east();
75 n0 = p1.north();
76 ne = -(p2.north() - p1.north());
77 nn = (p2.east() - p1.east());
78 l = Math.hypot(ne, nn);
79 ne /= l; nn /= l; // normal unit vector
80
81 Collection<Command> cmds = new LinkedList<>();
82
83 for (Node n : nodes) {
84 EastNorth c = n.getEastNorth();
85 double pr = (c.east()-e0)*ne + (c.north()-n0)*nn;
86 //pr=10;
87 cmds.add(new MoveCommand(n, -2*ne*pr, -2*nn*pr));
88 }
89
90 UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Symmetry"), cmds));
91 MainApplication.getMap().repaint();
92 }
93
94 @Override
95 protected void updateEnabledState() {
96 updateEnabledStateOnCurrentSelection();
97 }
98
99 @Override
100 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
101 setEnabled(selection != null && !selection.isEmpty());
102 }
103}
Note: See TracBrowser for help on using the repository browser.