[5927] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[4381] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
[4418] | 7 | import java.awt.event.KeyEvent;
|
---|
[4381] | 8 | import java.util.Collection;
|
---|
| 9 | import java.util.Collections;
|
---|
[5927] | 10 |
|
---|
[10448] | 11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[4381] | 12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[10604] | 14 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
[4418] | 15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[4381] | 16 |
|
---|
[12581] | 17 | /**
|
---|
| 18 | * User action to copy the coordinates of one or several node(s) to the clipboard.
|
---|
| 19 | */
|
---|
[4381] | 20 | public class CopyCoordinatesAction extends JosmAction {
|
---|
| 21 |
|
---|
[13106] | 22 | /**
|
---|
| 23 | * Constructs a new {@code CopyCoordinatesAction}.
|
---|
| 24 | */
|
---|
[4381] | 25 | public CopyCoordinatesAction() {
|
---|
| 26 | super(tr("Copy Coordinates"), null,
|
---|
[4418] | 27 | tr("Copy coordinates of selected nodes to clipboard."),
|
---|
[4958] | 28 | Shortcut.registerShortcut("copy:coordinates", tr("Edit: {0}", tr("Copy Coordinates")),
|
---|
[4982] | 29 | KeyEvent.VK_C, Shortcut.CTRL_SHIFT),
|
---|
[4418] | 30 | false);
|
---|
[4381] | 31 | putValue("toolbar", "copy/coordinates");
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @Override
|
---|
| 35 | public void actionPerformed(ActionEvent ae) {
|
---|
| 36 | StringBuilder s = new StringBuilder();
|
---|
| 37 | for (Node n : getSelectedNodes()) {
|
---|
[12161] | 38 | s.append(n.lat());
|
---|
[4381] | 39 | s.append(", ");
|
---|
[12161] | 40 | s.append(n.lon());
|
---|
[8390] | 41 | s.append('\n');
|
---|
[4381] | 42 | }
|
---|
[10604] | 43 | ClipboardUtils.copyString(s.toString().trim());
|
---|
[4381] | 44 | }
|
---|
| 45 |
|
---|
| 46 | @Override
|
---|
| 47 | protected void updateEnabledState() {
|
---|
| 48 | setEnabled(!getSelectedNodes().isEmpty());
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 53 | updateEnabledState();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[10448] | 56 | private Collection<Node> getSelectedNodes() {
|
---|
[13434] | 57 | DataSet ds = getLayerManager().getActiveDataSet();
|
---|
[11381] | 58 | if (ds == null) {
|
---|
[4381] | 59 | return Collections.emptyList();
|
---|
| 60 | } else {
|
---|
[12314] | 61 | return ds.getSelectedNodes();
|
---|
[4381] | 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|