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

Last change on this file since 6265 was 6069, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.tools.Shortcut;
18import org.openstreetmap.josm.tools.Utils;
19
20/**
21 * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else.
22 * @since 404
23 */
24public final class CopyAction extends JosmAction {
25
26 /**
27 * Constructs a new {@code CopyAction}.
28 */
29 public CopyAction() {
30 super(tr("Copy"), "copy",
31 tr("Copy selected objects to paste buffer."),
32 Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true);
33 putValue("help", ht("/Action/Copy"));
34 }
35
36 @Override
37 public void actionPerformed(ActionEvent e) {
38 if(isEmptySelection()) return;
39 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
40
41 copy(getEditLayer(), selection);
42 }
43
44 /**
45 * Copies the given primitive ids to the clipboard.
46 * @param source The OSM data layer source
47 * @param primitives The OSM primitives to copy
48 */
49 public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
50 /* copy ids to the clipboard */
51 StringBuilder idsBuilder = new StringBuilder();
52 for (OsmPrimitive p : primitives) {
53 idsBuilder.append(p.getId()).append(",");
54 }
55 String ids = idsBuilder.substring(0, idsBuilder.length() - 1);
56 Utils.copyToClipboard(ids);
57
58 Main.pasteBuffer.makeCopy(primitives);
59 Main.pasteSource = source;
60 }
61
62 @Override
63 protected void updateEnabledState() {
64 if (getCurrentDataSet() == null) {
65 setEnabled(false);
66 } else {
67 updateEnabledState(getCurrentDataSet().getSelected());
68 }
69 }
70
71 @Override
72 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
73 setEnabled(selection != null && !selection.isEmpty());
74 }
75
76 private boolean isEmptySelection() {
77 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
78 if (sel.isEmpty()) {
79 JOptionPane.showMessageDialog(
80 Main.parent,
81 tr("Please select something to copy."),
82 tr("Information"),
83 JOptionPane.INFORMATION_MESSAGE
84 );
85 return true;
86 }
87 return false;
88 }
89}
Note: See TracBrowser for help on using the repository browser.