001 // License: GPL. Copyright 2007 by Immanuel Scholz and others 002 // Author: David Earl 003 package org.openstreetmap.josm.actions; 004 005 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 006 import static org.openstreetmap.josm.tools.I18n.tr; 007 008 import java.awt.event.ActionEvent; 009 import java.awt.event.KeyEvent; 010 import java.util.Collection; 011 012 import javax.swing.JOptionPane; 013 014 import org.openstreetmap.josm.Main; 015 import org.openstreetmap.josm.data.osm.OsmPrimitive; 016 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 017 import org.openstreetmap.josm.tools.Shortcut; 018 import org.openstreetmap.josm.tools.Utils; 019 020 public final class CopyAction extends JosmAction { 021 022 public CopyAction() { 023 super(tr("Copy"), "copy", 024 tr("Copy selected objects to paste buffer."), 025 Shortcut.registerShortcut("system:copy", tr("Edit: {0}", tr("Copy")), KeyEvent.VK_C, Shortcut.CTRL), true); 026 putValue("help", ht("/Action/Copy")); 027 } 028 029 @Override 030 public void actionPerformed(ActionEvent e) { 031 if(isEmptySelection()) return; 032 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); 033 034 copy(getEditLayer(), selection); 035 } 036 037 public static void copy(OsmDataLayer source, Collection<OsmPrimitive> primitives) { 038 /* copy ids to the clipboard */ 039 StringBuilder idsBuilder = new StringBuilder(); 040 for (OsmPrimitive p : primitives) { 041 idsBuilder.append(p.getId()).append(","); 042 } 043 String ids = idsBuilder.substring(0, idsBuilder.length() - 1); 044 Utils.copyToClipboard(ids); 045 046 Main.pasteBuffer.makeCopy(primitives); 047 Main.pasteSource = source; 048 } 049 050 @Override 051 protected void updateEnabledState() { 052 if (getCurrentDataSet() == null) { 053 setEnabled(false); 054 } else { 055 updateEnabledState(getCurrentDataSet().getSelected()); 056 } 057 } 058 059 @Override 060 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 061 setEnabled(selection != null && !selection.isEmpty()); 062 } 063 064 private boolean isEmptySelection() { 065 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected(); 066 if (sel.isEmpty()) { 067 JOptionPane.showMessageDialog( 068 Main.parent, 069 tr("Please select something to copy."), 070 tr("Information"), 071 JOptionPane.INFORMATION_MESSAGE 072 ); 073 return true; 074 } 075 return false; 076 } 077 }