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

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

see #12943 - gsoc-core - fix most of deprecation warnings (static accesses must be fixed)

  • Property svn:eol-style set to native
File size: 3.7 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;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.tools.Shortcut;
20import org.openstreetmap.josm.tools.Utils;
21
22/**
23 * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else.
24 * @since 404
25 */
26public final class CopyAction extends JosmAction {
27
28 // regular expression that matches text clipboard contents after copying
29 public static final String CLIPBOARD_REGEXP = "((node|way|relation)\\s\\d+,)*(node|way|relation)\\s\\d+";
30
31 /**
32 * Constructs a new {@code CopyAction}.
33 */
34 public CopyAction() {
35 super(tr("Copy"), "copy",
36 tr("Copy selected objects to paste buffer."),
37 Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true);
38 putValue("help", ht("/Action/Copy"));
39 // CUA shortcut for copy (https://en.wikipedia.org/wiki/IBM_Common_User_Access#Description)
40 Main.registerActionShortcut(this,
41 Shortcut.registerShortcut("system:copy:cua", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_INSERT, Shortcut.CTRL));
42 }
43
44 @Override
45 public void actionPerformed(ActionEvent e) {
46 if (isEmptySelection()) return;
47 Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
48
49 copy(getLayerManager().getEditLayer(), selection);
50 }
51
52 /**
53 * Copies the given primitive ids to the clipboard. The output by this function
54 * looks similar to: node 1089302677,node 1089303458,way 93793372
55 * @param source The OSM data layer source
56 * @param primitives The OSM primitives to copy
57 */
58 public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
59 /* copy ids to the clipboard */
60 String ids = getCopyString(primitives);
61 Utils.copyToClipboard(ids);
62
63 Main.pasteBuffer.makeCopy(primitives);
64 Main.pasteSource = source;
65 }
66
67 public static String getCopyString(Collection<? extends OsmPrimitive> primitives) {
68 StringBuilder idsBuilder = new StringBuilder();
69 for (OsmPrimitive p : primitives) {
70 idsBuilder.append(OsmPrimitiveType.from(p).getAPIName()).append(' ').append(p.getId()).append(',');
71 }
72 return idsBuilder.substring(0, idsBuilder.length() - 1);
73 }
74
75 @Override
76 protected void updateEnabledState() {
77 DataSet ds = getLayerManager().getEditDataSet();
78 if (ds == null) {
79 setEnabled(false);
80 } else {
81 updateEnabledState(ds.getSelected());
82 }
83 }
84
85 @Override
86 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
87 setEnabled(selection != null && !selection.isEmpty());
88 }
89
90 private static boolean isEmptySelection() {
91 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
92 if (sel.isEmpty()) {
93 JOptionPane.showMessageDialog(
94 Main.parent,
95 tr("Please select something to copy."),
96 tr("Information"),
97 JOptionPane.INFORMATION_MESSAGE
98 );
99 return true;
100 }
101 return false;
102 }
103}
Note: See TracBrowser for help on using the repository browser.