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

Last change on this file was 19519, checked in by stoecker, 3 months ago

unify eol-style

  • Property svn:eol-style set to native
File size: 2.6 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.stream.Collectors;
11
12import org.openstreetmap.josm.data.notes.Note;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
18import org.openstreetmap.josm.spi.preferences.Config;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * User action to copy the URL of one or several object(s) to the clipboard.
23 * @since 17767
24 */
25public class CopyUrlAction extends JosmAction {
26
27 /**
28 * Constructs a new {@code CopyCoordinatesAction}.
29 */
30 public CopyUrlAction() {
31 super(tr("Copy server URLs"), "copy",
32 tr("Copy server URLs of selected objects to clipboard."),
33 Shortcut.registerShortcut("copy:urls", tr("Edit: {0}", tr("Copy server URLs")),
34 KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
35 false, "copy/urls", true);
36 }
37
38 @Override
39 public void actionPerformed(ActionEvent ae) {
40 final String base = Config.getUrls().getBaseBrowseUrl() + '/';
41 String string = getSelected().stream()
42 .filter(p -> !p.isNew())
43 .map(p -> base + OsmPrimitiveType.from(p).getAPIName() + '/' + p.getOsmId())
44 .collect(Collectors.joining("\n"));
45 Note note = getNote();
46 if (note != null && note.getId() > 0) {
47 string = string + "\n" + base + "/note/" + note.getId();
48 }
49 ClipboardUtils.copyString(string);
50 }
51
52 @Override
53 protected void updateEnabledState() {
54 Note note = getNote();
55 setEnabled((note != null && note.getId() > 0) || !getSelected().stream().allMatch(OsmPrimitive::isNew));
56 }
57
58 @Override
59 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
60 updateEnabledState();
61 }
62
63 private static Note getNote() {
64 return MainApplication.isDisplayingMapView() ? MainApplication.getMap().noteDialog.getSelectedNote() : null;
65 }
66
67 private Collection<OsmPrimitive> getSelected() {
68 DataSet ds = getLayerManager().getActiveDataSet();
69 if (ds == null) {
70 return Collections.emptyList();
71 } else {
72 return ds.getAllSelected();
73 }
74 }
75}
Note: See TracBrowser for help on using the repository browser.