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

Last change on this file since 5299 was 4982, checked in by stoecker, 12 years ago

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

  • Property svn:eol-style set to native
File size: 2.5 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
20public final class CopyAction extends JosmAction {
21
22 public CopyAction() {
23 super(tr("Copy"), "copy",
24 tr("Copy selected objects to paste buffer."),
25 Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true);
26 putValue("help", ht("/Action/Copy"));
27 }
28
29 @Override
30 public void actionPerformed(ActionEvent e) {
31 if(isEmptySelection()) return;
32 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
33
34 copy(getEditLayer(), selection);
35 }
36
37 public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
38 /* copy ids to the clipboard */
39 StringBuilder idsBuilder = new StringBuilder();
40 for (OsmPrimitive p : primitives) {
41 idsBuilder.append(p.getId()).append(",");
42 }
43 String ids = idsBuilder.substring(0, idsBuilder.length() - 1);
44 Utils.copyToClipboard(ids);
45
46 Main.pasteBuffer.makeCopy(primitives);
47 Main.pasteSource = source;
48 }
49
50 @Override
51 protected void updateEnabledState() {
52 if (getCurrentDataSet() == null) {
53 setEnabled(false);
54 } else {
55 updateEnabledState(getCurrentDataSet().getSelected());
56 }
57 }
58
59 @Override
60 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
61 setEnabled(selection != null && !selection.isEmpty());
62 }
63
64 private boolean isEmptySelection() {
65 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
66 if (sel.isEmpty()) {
67 JOptionPane.showMessageDialog(
68 Main.parent,
69 tr("Please select something to copy."),
70 tr("Information"),
71 JOptionPane.INFORMATION_MESSAGE
72 );
73 return true;
74 }
75 return false;
76 }
77}
Note: See TracBrowser for help on using the repository browser.