1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | // Author: David Earl
|
---|
3 | package org.openstreetmap.josm.actions;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 |
|
---|
8 | import java.awt.Toolkit;
|
---|
9 | import java.awt.datatransfer.Clipboard;
|
---|
10 | import java.awt.datatransfer.ClipboardOwner;
|
---|
11 | import java.awt.datatransfer.StringSelection;
|
---|
12 | import java.awt.datatransfer.Transferable;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.util.Collection;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
23 |
|
---|
24 | public final class CopyAction extends JosmAction {
|
---|
25 |
|
---|
26 | public CopyAction() {
|
---|
27 | super(tr("Copy"), "copy",
|
---|
28 | tr("Copy selected objects to paste buffer."),
|
---|
29 | Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.GROUP_MENU), true);
|
---|
30 | putValue("help", ht("/Action/Copy"));
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void actionPerformed(ActionEvent e) {
|
---|
34 | if(isEmptySelection()) return;
|
---|
35 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
36 |
|
---|
37 | copy(getEditLayer(), selection);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
|
---|
41 | /* copy ids to the clipboard */
|
---|
42 | StringBuilder idsBuilder = new StringBuilder();
|
---|
43 | for (OsmPrimitive p : primitives) {
|
---|
44 | idsBuilder.append(p.getId()+",");
|
---|
45 | }
|
---|
46 | String ids = idsBuilder.substring(0, idsBuilder.length() - 1);
|
---|
47 | try {
|
---|
48 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
|
---|
49 | new StringSelection(ids.toString()), new ClipboardOwner() {
|
---|
50 | public void lostOwnership(Clipboard clipboard, Transferable contents) {}
|
---|
51 | }
|
---|
52 | );
|
---|
53 | }
|
---|
54 | catch (RuntimeException x) {}
|
---|
55 |
|
---|
56 | Main.pasteBuffer.makeCopy(primitives);
|
---|
57 | Main.pasteSource = source;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void updateEnabledState() {
|
---|
62 | if (getCurrentDataSet() == null) {
|
---|
63 | setEnabled(false);
|
---|
64 | } else {
|
---|
65 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
71 | setEnabled(selection != null && !selection.isEmpty());
|
---|
72 | }
|
---|
73 |
|
---|
74 | private boolean isEmptySelection() {
|
---|
75 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
76 | if (sel.isEmpty()) {
|
---|
77 | JOptionPane.showMessageDialog(
|
---|
78 | Main.parent,
|
---|
79 | tr("Please select something to copy."),
|
---|
80 | tr("Information"),
|
---|
81 | JOptionPane.INFORMATION_MESSAGE
|
---|
82 | );
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 | }
|
---|