1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.Node;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
14 | import org.openstreetmap.josm.tools.Utils;
|
---|
15 |
|
---|
16 | public class CopyCoordinatesAction extends JosmAction {
|
---|
17 |
|
---|
18 | public CopyCoordinatesAction() {
|
---|
19 | super(tr("Copy Coordinates"), null,
|
---|
20 | tr("Copy coordinates of selected nodes to clipboard."),
|
---|
21 | Shortcut.registerShortcut("copy:coordinates", tr("Edit: {0}", tr("Copy Coordinates")),
|
---|
22 | KeyEvent.VK_C, Shortcut.CTRL_SHIFT),
|
---|
23 | false);
|
---|
24 | putValue("toolbar", "copy/coordinates");
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public void actionPerformed(ActionEvent ae) {
|
---|
29 | StringBuilder s = new StringBuilder();
|
---|
30 | for (Node n : getSelectedNodes()) {
|
---|
31 | s.append(n.getCoor().lat());
|
---|
32 | s.append(", ");
|
---|
33 | s.append(n.getCoor().lon());
|
---|
34 | s.append('\n');
|
---|
35 | }
|
---|
36 | Utils.copyToClipboard(s.toString().trim());
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | protected void updateEnabledState() {
|
---|
41 | setEnabled(!getSelectedNodes().isEmpty());
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
46 | updateEnabledState();
|
---|
47 | }
|
---|
48 |
|
---|
49 | private Collection<Node> getSelectedNodes() {
|
---|
50 | if (getCurrentDataSet() == null || getCurrentDataSet().getSelected() == null) {
|
---|
51 | return Collections.emptyList();
|
---|
52 | } else {
|
---|
53 | return Utils.filteredCollection(getCurrentDataSet().getSelected(), Node.class);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|