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