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

Last change on this file since 10604 was 10604, checked in by Don-vip, 8 years ago

fix #12478, fix #12565, fix #11114 - Use ​Swing Copy/Paste instead of CopyAction/PasteAction with custom buffer (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 2.9 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.datatransfer.ClipboardUtils;
19import org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable;
20import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
21import org.openstreetmap.josm.tools.Shortcut;
22
23/**
24 * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else.
25 * @since 404
26 */
27public class CopyAction extends JosmAction {
28 /**
29 * Constructs a new {@code CopyAction}.
30 */
31 public CopyAction() {
32 super(tr("Copy"), "copy",
33 tr("Copy selected objects to paste buffer."),
34 Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true);
35 putValue("help", ht("/Action/Copy"));
36 // CUA shortcut for copy (https://en.wikipedia.org/wiki/IBM_Common_User_Access#Description)
37 Main.registerActionShortcut(this,
38 Shortcut.registerShortcut("system:copy:cua", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_INSERT, Shortcut.CTRL));
39 }
40
41 @Override
42 public void actionPerformed(ActionEvent e) {
43 DataSet set = getLayerManager().getEditDataSet();
44 Collection<OsmPrimitive> selection = set == null ? Collections.<OsmPrimitive>emptySet() : set.getSelected();
45 if (selection.isEmpty()) {
46 showEmptySelectionWarning();
47 return;
48 }
49
50 copy(selection);
51 }
52
53 /**
54 * Copies the given primitive ids to the clipboard. The output by this function
55 * looks similar to: node 1089302677,node 1089303458,way 93793372
56 * @param primitives The OSM primitives to copy
57 */
58 public static void copy(Collection<OsmPrimitive> primitives) {
59 // copy ids to the clipboard
60 ClipboardUtils.copy(new PrimitiveTransferable(PrimitiveTransferData.getDataWithReferences(primitives)));
61 }
62
63 @Override
64 protected void updateEnabledState() {
65 updateEnabledStateOnCurrentSelection();
66 }
67
68 @Override
69 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
70 setEnabled(selection != null && !selection.isEmpty());
71 }
72
73 protected void showEmptySelectionWarning() {
74 JOptionPane.showMessageDialog(
75 Main.parent,
76 tr("Please select something to copy."),
77 tr("Information"),
78 JOptionPane.INFORMATION_MESSAGE
79 );
80 }
81}
Note: See TracBrowser for help on using the repository browser.