| 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 | public 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.GROUP_MENU), 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 | } |
|---|