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

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

see #12478 - Use ​Swing Copy/Paste instead of CopyAction/PasteAction with custom buffer (layer part: patch by michael2402, modified) - gsoc-core

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