source: josm/trunk/src/org/openstreetmap/josm/actions/CopyAction.java@ 13568

Last change on this file since 13568 was 13434, checked in by Don-vip, 6 years ago

see #8039, see #10456 - support read-only data layers

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2// Author: David Earl
3package org.openstreetmap.josm.actions;
4
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.Collections;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
20import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable;
21import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import 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 */
29public 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().getActiveDataSet();
46 Collection<OsmPrimitive> selection = set == null ? Collections.<OsmPrimitive>emptySet() : set.getSelected();
47 if (selection.isEmpty()) {
48 showEmptySelectionWarning();
49 return;
50 }
51
52 copy(getLayerManager().getActiveDataLayer(), 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(true);
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}
Note: See TracBrowser for help on using the repository browser.