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

Last change on this file since 6388 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 3.2 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 }
39
40 @Override
41 public void actionPerformed(ActionEvent e) {
42 if(isEmptySelection()) return;
43 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
44
45 copy(getEditLayer(), selection);
46 }
47
48 /**
49 * Copies the given primitive ids to the clipboard. The output by this function
50 * looks similar to: node 1089302677,node 1089303458,way 93793372
51 * @param source The OSM data layer source
52 * @param primitives The OSM primitives to copy
53 */
54 public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
55 /* copy ids to the clipboard */
56 StringBuilder idsBuilder = new StringBuilder();
57 for (OsmPrimitive p : primitives) {
58 idsBuilder.append(OsmPrimitiveType.from(p).getAPIName()).append(" ");
59 idsBuilder.append(p.getId()).append(",");
60 }
61 String ids = idsBuilder.substring(0, idsBuilder.length() - 1);
62 Utils.copyToClipboard(ids);
63
64 Main.pasteBuffer.makeCopy(primitives);
65 Main.pasteSource = source;
66 }
67
68 @Override
69 protected void updateEnabledState() {
70 if (getCurrentDataSet() == null) {
71 setEnabled(false);
72 } else {
73 updateEnabledState(getCurrentDataSet().getSelected());
74 }
75 }
76
77 @Override
78 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
79 setEnabled(selection != null && !selection.isEmpty());
80 }
81
82 private boolean isEmptySelection() {
83 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
84 if (sel.isEmpty()) {
85 JOptionPane.showMessageDialog(
86 Main.parent,
87 tr("Please select something to copy."),
88 tr("Information"),
89 JOptionPane.INFORMATION_MESSAGE
90 );
91 return true;
92 }
93 return false;
94 }
95}
Note: See TracBrowser for help on using the repository browser.