| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.actions; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 5 | |
| | 6 | import java.awt.event.ActionEvent; |
| | 7 | import java.awt.event.KeyEvent; |
| | 8 | import java.util.Collection; |
| | 9 | import java.util.Collections; |
| | 10 | import java.util.stream.Collectors; |
| | 11 | |
| | 12 | import org.openstreetmap.josm.data.notes.Note; |
| | 13 | import org.openstreetmap.josm.data.osm.DataSet; |
| | 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 15 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
| | 16 | import org.openstreetmap.josm.gui.MainApplication; |
| | 17 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils; |
| | 18 | import org.openstreetmap.josm.spi.preferences.Config; |
| | 19 | import 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 xxx |
| | 24 | */ |
| | 25 | public 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 | } |