01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.views.plugins;
11:
12: import java.io.File;
13: import java.util.ArrayList;
14: import java.util.Iterator;
15:
16: import org.eclipse.jface.action.Action;
17: import org.eclipse.jface.viewers.IStructuredSelection;
18: import org.eclipse.pde.internal.core.FileAdapter;
19: import org.eclipse.swt.dnd.Clipboard;
20: import org.eclipse.swt.dnd.FileTransfer;
21: import org.eclipse.swt.dnd.TextTransfer;
22: import org.eclipse.swt.dnd.Transfer;
23:
24: public class CopyToClipboardAction extends Action {
25: IStructuredSelection selection;
26: private Clipboard clipboard;
27:
28: /**
29: * Constructor for CopyToClipboardAction.
30: */
31: protected CopyToClipboardAction(Clipboard clipboard) {
32: setEnabled(false);
33: this .clipboard = clipboard;
34: }
35:
36: /**
37: * Constructor for CopyToClipboardAction.
38: * @param text
39: */
40: protected CopyToClipboardAction(String text) {
41: super (text);
42: }
43:
44: public void setSelection(IStructuredSelection selection) {
45: this .selection = selection;
46: setEnabled(canCopy(selection));
47: }
48:
49: private boolean canCopy(IStructuredSelection selection) {
50: if (selection.isEmpty())
51: return false;
52: for (Iterator iter = selection.iterator(); iter.hasNext();) {
53: Object obj = iter.next();
54: if (!(obj instanceof FileAdapter))
55: return false;
56: }
57: return true;
58: }
59:
60: public void run() {
61: if (selection.isEmpty())
62: return;
63: ArrayList files = new ArrayList();
64: for (Iterator iter = selection.iterator(); iter.hasNext();) {
65: Object obj = iter.next();
66: if (obj instanceof FileAdapter)
67: files.add(obj);
68: }
69: doCopy(files);
70: }
71:
72: private void doCopy(ArrayList files) {
73: // Get the file names and a string representation
74: int len = files.size();
75: String[] fileNames = new String[len];
76: StringBuffer buf = new StringBuffer();
77: for (int i = 0, length = len; i < length; i++) {
78: FileAdapter adapter = (FileAdapter) files.get(i);
79: File file = adapter.getFile();
80: fileNames[i] = file.getAbsolutePath();
81: if (i > 0)
82: buf.append("\n"); //$NON-NLS-1$
83: buf.append(file.getName());
84: }
85:
86: // set the clipboard contents
87: clipboard.setContents(
88: new Object[] { fileNames, buf.toString() },
89: new Transfer[] { FileTransfer.getInstance(),
90: TextTransfer.getInstance() });
91: }
92: }
|