source: josm/trunk/src/org/openstreetmap/josm/actions/CopyCoordinatesAction.java

Last change on this file was 18494, checked in by taylor.smock, 22 months ago

Fix #22115: Extract methods from LatLon into ILatLon where they are generally applicable

This also removes calls to Node#getCoor where possible, which reduces
the number of memory allocations in SearchCompiler#match, and overall
allocations due to Node#getCoor

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.Objects;
11import java.util.stream.Collectors;
12
13import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
14import org.openstreetmap.josm.data.coor.conversion.ICoordinateFormat;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * User action to copy the coordinates of one or several node(s) to the clipboard.
23 */
24public class CopyCoordinatesAction extends JosmAction {
25
26 /**
27 * Constructs a new {@code CopyCoordinatesAction}.
28 */
29 public CopyCoordinatesAction() {
30 super(tr("Copy Coordinates"), "copy",
31 tr("Copy coordinates of selected nodes to clipboard."),
32 Shortcut.registerShortcut("copy:coordinates", tr("Edit: {0}", tr("Copy Coordinates")),
33 KeyEvent.VK_C, Shortcut.CTRL_SHIFT),
34 false);
35 setToolbarId("copy/coordinates");
36 }
37
38 @Override
39 public void actionPerformed(ActionEvent ae) {
40 ICoordinateFormat coordinateFormat = CoordinateFormatManager.getDefaultFormat();
41 String string = getSelectedNodes().stream()
42 .filter(Objects::nonNull)
43 .map(c -> coordinateFormat.toString(c, ", "))
44 .collect(Collectors.joining("\n"));
45 ClipboardUtils.copyString(string);
46 }
47
48 @Override
49 protected void updateEnabledState() {
50 setEnabled(!getSelectedNodes().isEmpty());
51 }
52
53 @Override
54 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
55 updateEnabledState();
56 }
57
58 private Collection<Node> getSelectedNodes() {
59 DataSet ds = getLayerManager().getActiveDataSet();
60 if (ds == null) {
61 return Collections.emptyList();
62 } else {
63 return ds.getSelectedNodes();
64 }
65 }
66}
Note: See TracBrowser for help on using the repository browser.