[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 |
|
---|
[4381] | 11 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[4418] | 13 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[4381] | 14 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 15 |
|
---|
| 16 | public class CopyCoordinatesAction extends JosmAction {
|
---|
| 17 |
|
---|
| 18 | public CopyCoordinatesAction() {
|
---|
| 19 | super(tr("Copy Coordinates"), null,
|
---|
[4418] | 20 | tr("Copy coordinates of selected nodes to clipboard."),
|
---|
[4958] | 21 | Shortcut.registerShortcut("copy:coordinates", tr("Edit: {0}", tr("Copy Coordinates")),
|
---|
[4982] | 22 | KeyEvent.VK_C, Shortcut.CTRL_SHIFT),
|
---|
[4418] | 23 | false);
|
---|
[4381] | 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());
|
---|
[8390] | 34 | s.append('\n');
|
---|
[4381] | 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 | }
|
---|