1 11 package org.eclipse.pde.internal.ui.views.plugins; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.pde.internal.core.FileAdapter; 20 import org.eclipse.swt.dnd.Clipboard; 21 import org.eclipse.swt.dnd.FileTransfer; 22 import org.eclipse.swt.dnd.TextTransfer; 23 import org.eclipse.swt.dnd.Transfer; 24 25 public class CopyToClipboardAction extends Action { 26 IStructuredSelection selection; 27 private Clipboard clipboard; 28 29 32 protected CopyToClipboardAction(Clipboard clipboard) { 33 setEnabled(false); 34 this.clipboard = clipboard; 35 } 36 37 41 protected CopyToClipboardAction(String text) { 42 super(text); 43 } 44 45 public void setSelection(IStructuredSelection selection) { 46 this.selection = selection; 47 setEnabled(canCopy(selection)); 48 } 49 50 private boolean canCopy(IStructuredSelection selection) { 51 if (selection.isEmpty()) return false; 52 for (Iterator iter = selection.iterator(); iter.hasNext();) { 53 Object obj = iter.next(); 54 if (!(obj instanceof FileAdapter)) return false; 55 } 56 return true; 57 } 58 59 public void run() { 60 if (selection.isEmpty()) return; 61 ArrayList files = new ArrayList (); 62 for (Iterator iter = selection.iterator(); iter.hasNext();) { 63 Object obj = iter.next(); 64 if (obj instanceof FileAdapter) 65 files.add(obj); 66 } 67 doCopy(files); 68 } 69 private void doCopy(ArrayList files) { 70 int len = files.size(); 72 String [] fileNames = new String [len]; 73 StringBuffer buf = new StringBuffer (); 74 for (int i = 0, length = len; i < length; i++) { 75 FileAdapter adapter = (FileAdapter)files.get(i); 76 File file = adapter.getFile(); 77 fileNames[i] = file.getAbsolutePath(); 78 if (i > 0) 79 buf.append("\n"); buf.append(file.getName()); 81 } 82 83 clipboard.setContents( 85 new Object []{ 86 fileNames, 87 buf.toString()}, 88 new Transfer[]{ 89 FileTransfer.getInstance(), 90 TextTransfer.getInstance()}); 91 } 92 } 93 | Popular Tags |