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

Last change on this file since 8195 was 8194, checked in by simon04, 9 years ago

Download objects: prefill ids from copied objects

The syntax from CopyAction is e.g. node 123 and has not been parsed yet.

  • Property svn:eol-style set to native
File size: 3.6 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.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.tools.Shortcut;
19import org.openstreetmap.josm.tools.Utils;
20
21/**
22 * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else.
23 * @since 404
24 */
25public final class CopyAction extends JosmAction {
26
27 // regular expression that matches text clipboard contents after copying
28 public static final String CLIPBOARD_REGEXP = "((node|way|relation)\\s\\d+,)*(node|way|relation)\\s\\d+";
29
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 Main.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 if(isEmptySelection()) return;
46 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
47
48 copy(getEditLayer(), selection);
49 }
50
51 /**
52 * Copies the given primitive ids to the clipboard. The output by this function
53 * looks similar to: node 1089302677,node 1089303458,way 93793372
54 * @param source The OSM data layer source
55 * @param primitives The OSM primitives to copy
56 */
57 public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
58 /* copy ids to the clipboard */
59 String ids = getCopyString(primitives);
60 Utils.copyToClipboard(ids);
61
62 Main.pasteBuffer.makeCopy(primitives);
63 Main.pasteSource = source;
64 }
65
66 public static String getCopyString(Collection<? extends OsmPrimitive> primitives) {
67 StringBuilder idsBuilder = new StringBuilder();
68 for (OsmPrimitive p : primitives) {
69 idsBuilder.append(OsmPrimitiveType.from(p).getAPIName()).append(" ");
70 idsBuilder.append(p.getId()).append(",");
71 }
72 return idsBuilder.substring(0, idsBuilder.length() - 1);
73 }
74
75 @Override
76 protected void updateEnabledState() {
77 if (getCurrentDataSet() == null) {
78 setEnabled(false);
79 } else {
80 updateEnabledState(getCurrentDataSet().getSelected());
81 }
82 }
83
84 @Override
85 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
86 setEnabled(selection != null && !selection.isEmpty());
87 }
88
89 private boolean isEmptySelection() {
90 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
91 if (sel.isEmpty()) {
92 JOptionPane.showMessageDialog(
93 Main.parent,
94 tr("Please select something to copy."),
95 tr("Information"),
96 JOptionPane.INFORMATION_MESSAGE
97 );
98 return true;
99 }
100 return false;
101 }
102}
Note: See TracBrowser for help on using the repository browser.