1
/*******************************************************************************
2
* Copyright (c) 2000, 2010 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
9
* IBM Corporation - initial API and implementation
10
* Anton Leherbauer (Wind River Systems)
11
*******************************************************************************/
12
package org.eclipse.cdt.internal.ui.cview;
14
import java.util.Iterator;
15
import java.util.List;
17
import org.eclipse.core.resources.IContainer;
18
import org.eclipse.core.resources.IResource;
19
import org.eclipse.core.runtime.Assert;
20
import org.eclipse.core.runtime.IPath;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.IStructuredSelection;
23
import org.eclipse.swt.SWTError;
24
import org.eclipse.swt.dnd.Clipboard;
25
import org.eclipse.swt.dnd.DND;
26
import org.eclipse.swt.dnd.FileTransfer;
27
import org.eclipse.swt.dnd.TextTransfer;
28
import org.eclipse.swt.dnd.Transfer;
29
import org.eclipse.swt.widgets.Shell;
30
import org.eclipse.ui.PlatformUI;
31
import org.eclipse.ui.actions.SelectionListenerAction;
32
import org.eclipse.ui.part.ResourceTransfer;
34
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
37
* Standard action for copying the currently selected resources to the clipboard.
39
* This class may be instantiated; it is not intended to be subclassed.
44
public class CopyAction extends SelectionListenerAction {
47
* The id of this action.
49
public static final String ID = PlatformUI.PLUGIN_ID + ".CopyAction"; //$NON-NLS-1$
52
* The shell in which to show any dialogs.
59
private Clipboard clipboard;
62
* Associated paste action. May be <code>null</code>
64
private PasteAction pasteAction;
67
* Creates a new action.
69
* @param shell the shell for any dialogs
70
* @param clipboard a platform clipboard
72
public CopyAction(Shell shell, Clipboard clipboard) {
73
super(CViewMessages.CopyAction_title);
74
Assert.isNotNull(shell);
75
Assert.isNotNull(clipboard);
77
this.clipboard = clipboard;
78
setToolTipText(CViewMessages.CopyAction_toolTip);
80
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ICHelpContextIds.COPY_ACTION);
84
* Creates a new action.
86
* @param shell the shell for any dialogs
87
* @param clipboard a platform clipboard
88
* @param pasteAction a paste action
92
public CopyAction(Shell shell, Clipboard clipboard, PasteAction pasteAction) {
93
this(shell, clipboard);
94
this.pasteAction = pasteAction;
97
* The <code>CopyAction</code> implementation of this method defined
98
* on <code>IAction</code> copies the selected resources to the
103
List<?> selectedResources = getSelectedResources();
104
IResource[] resources = selectedResources.toArray(new IResource[selectedResources.size()]);
106
// Get the file names and a string representation
107
final int length = resources.length;
108
int actualLength = 0;
109
String[] fileNames = new String[length];
110
StringBuffer buf = new StringBuffer();
111
for (int i = 0; i < length; i++) {
112
IPath location = resources[i].getLocation();
113
// location may be null. See bug 29491.
114
if (location != null)
115
fileNames[actualLength++] = location.toOSString();
117
buf.append("\n"); //$NON-NLS-1$
118
buf.append(resources[i].getName());
120
// was one or more of the locations null?
121
if (actualLength < length) {
122
String[] tempFileNames = fileNames;
123
fileNames = new String[actualLength];
124
for (int i = 0; i < actualLength; i++)
125
fileNames[i] = tempFileNames[i];
127
setClipboard(resources, fileNames, buf.toString());
129
// update the enablement of the paste action
130
// workaround since the clipboard does not suppot callbacks
131
if (pasteAction != null && pasteAction.getStructuredSelection() != null)
132
pasteAction.selectionChanged(pasteAction.getStructuredSelection());
135
* Set the clipboard contents. Prompt to retry if clipboard is busy.
137
* @param resources the resources to copy to the clipboard
138
* @param fileNames file names of the resources to copy to the clipboard
139
* @param names string representation of all names
141
private void setClipboard(IResource[] resources, String[] fileNames, String names) {
143
// set the clipboard contents
144
if (fileNames.length > 0) {
145
clipboard.setContents(
146
new Object[] { resources, fileNames, names },
147
new Transfer[] { ResourceTransfer.getInstance(), FileTransfer.getInstance(), TextTransfer.getInstance()});
149
clipboard.setContents(
150
new Object[] { resources, names },
151
new Transfer[] { ResourceTransfer.getInstance(), TextTransfer.getInstance()});
153
} catch (SWTError e) {
154
if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
156
if (MessageDialog.openQuestion(shell, CViewMessages.CopyToClipboardProblemDialog_title, CViewMessages.CopyToClipboardProblemDialog_message))
157
setClipboard(resources, fileNames, names);
161
* The <code>CopyAction</code> implementation of this
162
* <code>SelectionListenerAction</code> method enables this action if
163
* one or more resources of compatible types are selected.
166
protected boolean updateSelection(IStructuredSelection selection) {
167
if (!super.updateSelection(selection))
170
if (getSelectedNonResources().size() > 0)
173
List<?> selectedResources = getSelectedResources();
174
if (selectedResources.size() == 0)
177
boolean projSelected = selectionIsOfType(IResource.PROJECT);
178
boolean fileFoldersSelected = selectionIsOfType(IResource.FILE | IResource.FOLDER);
179
if (!projSelected && !fileFoldersSelected)
182
// selection must be homogeneous
183
if (projSelected && fileFoldersSelected)
186
// must have a common parent
187
IContainer firstParent = ((IResource) selectedResources.get(0)).getParent();
188
if (firstParent == null)
191
Iterator<?> resourcesEnum = selectedResources.iterator();
192
while (resourcesEnum.hasNext()) {
193
IResource currentResource = (IResource) resourcesEnum.next();
194
if (!currentResource.getParent().equals(firstParent))
196
// resource location must exist
197
if (currentResource.getLocationURI() == null)