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

Last change on this file since 10386 was 10382, checked in by Don-vip, 8 years ago

see #12943 - gsoc-core - fix most of deprecation warnings (static accesses must be fixed)

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