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.DataSet;
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 |
|
---|
17 | public class CopyCoordinatesAction extends JosmAction {
|
---|
18 |
|
---|
19 | public CopyCoordinatesAction() {
|
---|
20 | super(tr("Copy Coordinates"), null,
|
---|
21 | tr("Copy coordinates of selected nodes to clipboard."),
|
---|
22 | Shortcut.registerShortcut("copy:coordinates", tr("Edit: {0}", tr("Copy Coordinates")),
|
---|
23 | KeyEvent.VK_C, Shortcut.CTRL_SHIFT),
|
---|
24 | false);
|
---|
25 | putValue("toolbar", "copy/coordinates");
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public void actionPerformed(ActionEvent ae) {
|
---|
30 | StringBuilder s = new StringBuilder();
|
---|
31 | for (Node n : getSelectedNodes()) {
|
---|
32 | s.append(n.getCoor().lat());
|
---|
33 | s.append(", ");
|
---|
34 | s.append(n.getCoor().lon());
|
---|
35 | s.append('\n');
|
---|
36 | }
|
---|
37 | Utils.copyToClipboard(s.toString().trim());
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | protected void updateEnabledState() {
|
---|
42 | setEnabled(!getSelectedNodes().isEmpty());
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
47 | updateEnabledState();
|
---|
48 | }
|
---|
49 |
|
---|
50 | private Collection<Node> getSelectedNodes() {
|
---|
51 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
52 | if (ds == null || ds.getSelected() == null) {
|
---|
53 | return Collections.emptyList();
|
---|
54 | } else {
|
---|
55 | return Utils.filteredCollection(ds.getSelected(), Node.class);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|