[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | // Author: David Earl
|
---|
| 3 | package org.openstreetmap.josm.actions;
|
---|
| 4 |
|
---|
[3384] | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[626] | 6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 7 |
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
[1523] | 10 | import java.util.Collection;
|
---|
[626] | 11 |
|
---|
| 12 | import javax.swing.JOptionPane;
|
---|
| 13 |
|
---|
| 14 | import org.openstreetmap.josm.Main;
|
---|
[1523] | 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[6320] | 16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[3384] | 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[1084] | 18 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[4380] | 19 | import org.openstreetmap.josm.tools.Utils;
|
---|
[626] | 20 |
|
---|
[5953] | 21 | /**
|
---|
[6069] | 22 | * Copy OSM primitives to clipboard in order to paste them, or their tags, somewhere else.
|
---|
[5953] | 23 | * @since 404
|
---|
| 24 | */
|
---|
[1820] | 25 | public final class CopyAction extends JosmAction {
|
---|
[6920] | 26 |
|
---|
[6321] | 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+";
|
---|
[626] | 29 |
|
---|
[5953] | 30 | /**
|
---|
| 31 | * Constructs a new {@code CopyAction}.
|
---|
| 32 | */
|
---|
[1169] | 33 | public CopyAction() {
|
---|
| 34 | super(tr("Copy"), "copy",
|
---|
| 35 | tr("Copy selected objects to paste buffer."),
|
---|
[4982] | 36 | Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true);
|
---|
[2323] | 37 | putValue("help", ht("/Action/Copy"));
|
---|
[6920] | 38 | // CUA shortcut for copy (https://en.wikipedia.org/wiki/IBM_Common_User_Access#Description)
|
---|
[6451] | 39 | Main.registerActionShortcut(this,
|
---|
| 40 | Shortcut.registerShortcut("system:copy:cua", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_INSERT, Shortcut.CTRL));
|
---|
[1169] | 41 | }
|
---|
[626] | 42 |
|
---|
[4380] | 43 | @Override
|
---|
[1169] | 44 | public void actionPerformed(ActionEvent e) {
|
---|
[8510] | 45 | if (isEmptySelection()) return;
|
---|
[2892] | 46 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
[1514] | 47 |
|
---|
[3384] | 48 | copy(getEditLayer(), selection);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[5953] | 51 | /**
|
---|
[6320] | 52 | * Copies the given primitive ids to the clipboard. The output by this function
|
---|
| 53 | * looks similar to: node 1089302677,node 1089303458,way 93793372
|
---|
[5953] | 54 | * @param source The OSM data layer source
|
---|
| 55 | * @param primitives The OSM primitives to copy
|
---|
| 56 | */
|
---|
[3384] | 57 | public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) {
|
---|
[2892] | 58 | /* copy ids to the clipboard */
|
---|
[8194] | 59 | String ids = getCopyString(primitives);
|
---|
| 60 | Utils.copyToClipboard(ids);
|
---|
| 61 |
|
---|
| 62 | Main.pasteBuffer.makeCopy(primitives);
|
---|
| 63 | Main.pasteSource = source;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public static String getCopyString(Collection<? extends OsmPrimitive> primitives) {
|
---|
[2892] | 67 | StringBuilder idsBuilder = new StringBuilder();
|
---|
[3384] | 68 | for (OsmPrimitive p : primitives) {
|
---|
[8390] | 69 | idsBuilder.append(OsmPrimitiveType.from(p).getAPIName()).append(' ').append(p.getId()).append(',');
|
---|
[2892] | 70 | }
|
---|
[8194] | 71 | return idsBuilder.substring(0, idsBuilder.length() - 1);
|
---|
[1351] | 72 | }
|
---|
[1514] | 73 |
|
---|
[1820] | 74 | @Override
|
---|
| 75 | protected void updateEnabledState() {
|
---|
[2256] | 76 | if (getCurrentDataSet() == null) {
|
---|
| 77 | setEnabled(false);
|
---|
| 78 | } else {
|
---|
| 79 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
| 80 | }
|
---|
[1169] | 81 | }
|
---|
[1514] | 82 |
|
---|
[2256] | 83 | @Override
|
---|
| 84 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 85 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1814] | 88 | private boolean isEmptySelection() {
|
---|
| 89 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
[1351] | 90 | if (sel.isEmpty()) {
|
---|
[2008] | 91 | JOptionPane.showMessageDialog(
|
---|
[1814] | 92 | Main.parent,
|
---|
[1847] | 93 | tr("Please select something to copy."),
|
---|
| 94 | tr("Information"),
|
---|
| 95 | JOptionPane.INFORMATION_MESSAGE
|
---|
[1814] | 96 | );
|
---|
[1351] | 97 | return true;
|
---|
| 98 | }
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
[626] | 101 | }
|
---|