1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | // Author: David Earl
|
---|
3 | package org.openstreetmap.josm.actions;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 |
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
20 | import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable;
|
---|
21 | import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else.
|
---|
27 | * @since 404
|
---|
28 | */
|
---|
29 | public class CopyAction extends JosmAction {
|
---|
30 | /**
|
---|
31 | * Constructs a new {@code CopyAction}.
|
---|
32 | */
|
---|
33 | public CopyAction() {
|
---|
34 | super(tr("Copy"), "copy",
|
---|
35 | tr("Copy selected objects to paste buffer."),
|
---|
36 | Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true);
|
---|
37 | putValue("help", ht("/Action/Copy"));
|
---|
38 | // CUA shortcut for copy (https://en.wikipedia.org/wiki/IBM_Common_User_Access#Description)
|
---|
39 | MainApplication.registerActionShortcut(this,
|
---|
40 | Shortcut.registerShortcut("system:copy:cua", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_INSERT, Shortcut.CTRL));
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
45 | DataSet set = getLayerManager().getEditDataSet();
|
---|
46 | Collection<OsmPrimitive> selection = set == null ? Collections.<OsmPrimitive>emptySet() : set.getSelected();
|
---|
47 | if (selection.isEmpty()) {
|
---|
48 | showEmptySelectionWarning();
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | copy(getLayerManager().getEditLayer(), selection);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Copies the given primitive ids to the clipboard. The output by this function
|
---|
57 | * looks similar to: node 1089302677,node 1089303458,way 93793372
|
---|
58 | * @param source The OSM data layer source
|
---|
59 | * @param primitives The OSM primitives to copy
|
---|
60 | */
|
---|
61 | public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
|
---|
62 | // copy ids to the clipboard
|
---|
63 | ClipboardUtils.copy(new PrimitiveTransferable(PrimitiveTransferData.getDataWithReferences(primitives), source));
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | protected void updateEnabledState() {
|
---|
68 | updateEnabledStateOnCurrentSelection();
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
73 | setEnabled(selection != null && !selection.isEmpty());
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected void showEmptySelectionWarning() {
|
---|
77 | JOptionPane.showMessageDialog(
|
---|
78 | Main.parent,
|
---|
79 | tr("Please select something to copy."),
|
---|
80 | tr("Information"),
|
---|
81 | JOptionPane.INFORMATION_MESSAGE
|
---|
82 | );
|
---|
83 | }
|
---|
84 | }
|
---|