KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > CopyToClipboardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.view;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
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     /**
30      * Constructor for CopyToClipboardAction.
31      */

32     protected CopyToClipboardAction(Clipboard clipboard) {
33         setEnabled(false);
34         this.clipboard = clipboard;
35     }
36
37     /**
38      * Constructor for CopyToClipboardAction.
39      * @param text
40      */

41     protected CopyToClipboardAction(String JavaDoc 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 JavaDoc iter = selection.iterator(); iter.hasNext();) {
53             Object JavaDoc 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 JavaDoc files = new ArrayList JavaDoc();
62         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
63             Object JavaDoc obj = iter.next();
64             if (obj instanceof FileAdapter)
65                 files.add(obj);
66         }
67         doCopy(files);
68     }
69     private void doCopy(ArrayList JavaDoc files) {
70     // Get the file names and a string representation
71
int len = files.size();
72     String JavaDoc[] fileNames = new String JavaDoc[len];
73     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
74     for (int i = 0, length = len; i < length; i++) {
75         FileAdapter adapter = (FileAdapter)files.get(i);
76         File JavaDoc file = adapter.getFile();
77         fileNames[i] = file.getAbsolutePath();
78         if (i > 0)
79             buf.append("\n"); //$NON-NLS-1$
80
buf.append(file.getName());
81     }
82     
83     // set the clipboard contents
84
clipboard.setContents(
85         new Object JavaDoc[]{
86             fileNames,
87             buf.toString()},
88         new Transfer[]{
89             FileTransfer.getInstance(),
90             TextTransfer.getInstance()});
91     }
92 }
93
Popular Tags