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.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Collection;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 | import 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 | */
|
---|
24 | public 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 | }
|
---|