Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2008 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.ui.navigator.resources; |
12 |
|
13 |
import java.util.List; |
14 |
|
15 |
import org.eclipse.core.commands.AbstractHandler; |
16 |
import org.eclipse.core.commands.ExecutionEvent; |
17 |
import org.eclipse.core.commands.ExecutionException; |
18 |
import org.eclipse.core.resources.IContainer; |
19 |
import org.eclipse.core.resources.IFile; |
20 |
import org.eclipse.core.resources.IProject; |
21 |
import org.eclipse.core.resources.IResource; |
22 |
import org.eclipse.core.runtime.Assert; |
23 |
import org.eclipse.jface.viewers.IStructuredSelection; |
24 |
import org.eclipse.swt.dnd.Clipboard; |
25 |
import org.eclipse.swt.dnd.FileTransfer; |
26 |
import org.eclipse.swt.dnd.TransferData; |
27 |
import org.eclipse.swt.widgets.Shell; |
28 |
import org.eclipse.ui.PlatformUI; |
29 |
import org.eclipse.ui.actions.CopyFilesAndFoldersOperation; |
30 |
import org.eclipse.ui.actions.CopyProjectOperation; |
31 |
import org.eclipse.ui.actions.SelectionListenerAction; |
32 |
import org.eclipse.ui.part.ResourceTransfer; |
33 |
import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages; |
34 |
|
35 |
/** |
36 |
* Standard action for pasting resources on the clipboard to the selected resource's location. |
37 |
* <p> |
38 |
* This class may be instantiated; it is not intended to be subclassed. |
39 |
* </p> |
40 |
* |
41 |
* @since 3.5 |
42 |
*/ |
43 |
public class PasteHandler extends AbstractHandler { |
44 |
|
45 |
/** |
46 |
* The id of this action. |
47 |
*/ |
48 |
public static final String ID = PlatformUI.PLUGIN_ID + ".PasteAction";//$NON-NLS-1$ |
49 |
|
50 |
/** |
51 |
* The shell in which to show any dialogs. |
52 |
*/ |
53 |
private Shell shell; |
54 |
|
55 |
/** |
56 |
* System clipboard |
57 |
*/ |
58 |
private Clipboard clipboard; |
59 |
|
60 |
/** |
61 |
* Creates a new action. |
62 |
* |
63 |
* @param shell the shell for any dialogs |
64 |
* @param clipboard the clipboard |
65 |
*/ |
66 |
public PasteHandler(Shell shell, Clipboard clipboard) { |
67 |
super(WorkbenchNavigatorMessages.PasteAction_Past_); |
68 |
Assert.isNotNull(shell); |
69 |
Assert.isNotNull(clipboard); |
70 |
this.shell = shell; |
71 |
this.clipboard = clipboard; |
72 |
setToolTipText(WorkbenchNavigatorMessages.PasteAction_Paste_selected_resource_s_); |
73 |
setId(PasteHandler.ID); |
74 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "HelpId"); //$NON-NLS-1$ |
75 |
// TODO INavigatorHelpContextIds.PASTE_ACTION); |
76 |
} |
77 |
|
78 |
/** |
79 |
* Returns the actual target of the paste action. Returns null |
80 |
* if no valid target is selected. |
81 |
* |
82 |
* @return the actual target of the paste action |
83 |
*/ |
84 |
private IResource getTarget() { |
85 |
List selectedResources = getSelectedResources(); |
86 |
|
87 |
for (int i = 0; i < selectedResources.size(); i++) { |
88 |
IResource resource = (IResource) selectedResources.get(i); |
89 |
|
90 |
if (resource instanceof IProject && !((IProject) resource).isOpen()) { |
91 |
return null; |
92 |
} |
93 |
if (resource.getType() == IResource.FILE) { |
94 |
resource = resource.getParent(); |
95 |
} |
96 |
if (resource != null) { |
97 |
return resource; |
98 |
} |
99 |
} |
100 |
return null; |
101 |
} |
102 |
|
103 |
/** |
104 |
* Returns whether any of the given resources are linked resources. |
105 |
* |
106 |
* @param resources resource to check for linked type. may be null |
107 |
* @return true=one or more resources are linked. false=none of the |
108 |
* resources are linked |
109 |
*/ |
110 |
private boolean isLinked(IResource[] resources) { |
111 |
for (int i = 0; i < resources.length; i++) { |
112 |
if (resources[i].isLinked()) { |
113 |
return true; |
114 |
} |
115 |
} |
116 |
return false; |
117 |
} |
118 |
|
119 |
/* (non-Javadoc) |
120 |
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
121 |
*/ |
122 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
123 |
// TODO Auto-generated method stub |
124 |
return null; |
125 |
} |
126 |
|
127 |
/** |
128 |
* Implementation of method defined on <code>IAction</code>. |
129 |
*/ |
130 |
public void run() { |
131 |
// try a resource transfer |
132 |
ResourceTransfer resTransfer = ResourceTransfer.getInstance(); |
133 |
IResource[] resourceData = (IResource[]) clipboard |
134 |
.getContents(resTransfer); |
135 |
|
136 |
if (resourceData != null && resourceData.length > 0) { |
137 |
if (resourceData[0].getType() == IResource.PROJECT) { |
138 |
// enablement checks for all projects |
139 |
for (int i = 0; i < resourceData.length; i++) { |
140 |
CopyProjectOperation operation = new CopyProjectOperation( |
141 |
this.shell); |
142 |
operation.copyProject((IProject) resourceData[i]); |
143 |
} |
144 |
} else { |
145 |
// enablement should ensure that we always have access to a container |
146 |
IContainer container = getContainer(); |
147 |
|
148 |
CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( |
149 |
this.shell); |
150 |
operation.copyResources(resourceData, container); |
151 |
} |
152 |
return; |
153 |
} |
154 |
|
155 |
// try a file transfer |
156 |
FileTransfer fileTransfer = FileTransfer.getInstance(); |
157 |
String[] fileData = (String[]) clipboard.getContents(fileTransfer); |
158 |
|
159 |
if (fileData != null) { |
160 |
// enablement should ensure that we always have access to a container |
161 |
IContainer container = getContainer(); |
162 |
|
163 |
CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation( |
164 |
this.shell); |
165 |
operation.copyFiles(fileData, container); |
166 |
} |
167 |
} |
168 |
|
169 |
/** |
170 |
* Returns the container to hold the pasted resources. |
171 |
*/ |
172 |
private IContainer getContainer() { |
173 |
List selection = getSelectedResources(); |
174 |
if (selection.get(0) instanceof IFile) { |
175 |
return ((IFile) selection.get(0)).getParent(); |
176 |
} |
177 |
return (IContainer) selection.get(0); |
178 |
} |
179 |
|
180 |
/** |
181 |
* The <code>PasteAction</code> implementation of this |
182 |
* <code>SelectionListenerAction</code> method enables this action if |
183 |
* a resource compatible with what is on the clipboard is selected. |
184 |
* |
185 |
* -Clipboard must have IResource or java.io.File |
186 |
* -Projects can always be pasted if they are open |
187 |
* -Workspace folder may not be copied into itself |
188 |
* -Files and folders may be pasted to a single selected folder in open |
189 |
* project or multiple selected files in the same folder |
190 |
*/ |
191 |
protected boolean updateSelection(IStructuredSelection selection) { |
192 |
if (!super.updateSelection(selection)) { |
193 |
return false; |
194 |
} |
195 |
|
196 |
final IResource[][] clipboardData = new IResource[1][]; |
197 |
shell.getDisplay().syncExec(new Runnable() { |
198 |
public void run() { |
199 |
// clipboard must have resources or files |
200 |
ResourceTransfer resTransfer = ResourceTransfer.getInstance(); |
201 |
clipboardData[0] = (IResource[]) clipboard |
202 |
.getContents(resTransfer); |
203 |
} |
204 |
}); |
205 |
IResource[] resourceData = clipboardData[0]; |
206 |
boolean isProjectRes = resourceData != null && resourceData.length > 0 |
207 |
&& resourceData[0].getType() == IResource.PROJECT; |
208 |
|
209 |
if (isProjectRes) { |
210 |
for (int i = 0; i < resourceData.length; i++) { |
211 |
// make sure all resource data are open projects |
212 |
// can paste open projects regardless of selection |
213 |
if (resourceData[i].getType() != IResource.PROJECT |
214 |
|| ((IProject) resourceData[i]).isOpen() == false) { |
215 |
return false; |
216 |
} |
217 |
} |
218 |
return true; |
219 |
} |
220 |
|
221 |
if (getSelectedNonResources().size() > 0) { |
222 |
return false; |
223 |
} |
224 |
|
225 |
IResource targetResource = getTarget(); |
226 |
// targetResource is null if no valid target is selected (e.g., open project) |
227 |
// or selection is empty |
228 |
if (targetResource == null) { |
229 |
return false; |
230 |
} |
231 |
|
232 |
// can paste files and folders to a single selection (file, folder, |
233 |
// open project) or multiple file selection with the same parent |
234 |
List selectedResources = getSelectedResources(); |
235 |
if (selectedResources.size() > 1) { |
236 |
for (int i = 0; i < selectedResources.size(); i++) { |
237 |
IResource resource = (IResource) selectedResources.get(i); |
238 |
if (resource.getType() != IResource.FILE) { |
239 |
return false; |
240 |
} |
241 |
if (!targetResource.equals(resource.getParent())) { |
242 |
return false; |
243 |
} |
244 |
} |
245 |
} |
246 |
if (resourceData != null) { |
247 |
// linked resources can only be pasted into projects |
248 |
if (isLinked(resourceData) |
249 |
&& targetResource.getType() != IResource.PROJECT |
250 |
&& targetResource.getType() != IResource.FOLDER) { |
251 |
return false; |
252 |
} |
253 |
|
254 |
if (targetResource.getType() == IResource.FOLDER) { |
255 |
// don't try to copy folder to self |
256 |
for (int i = 0; i < resourceData.length; i++) { |
257 |
if (targetResource.equals(resourceData[i])) { |
258 |
return false; |
259 |
} |
260 |
} |
261 |
} |
262 |
return true; |
263 |
} |
264 |
TransferData[] transfers = clipboard.getAvailableTypes(); |
265 |
FileTransfer fileTransfer = FileTransfer.getInstance(); |
266 |
for (int i = 0; i < transfers.length; i++) { |
267 |
if (fileTransfer.isSupportedType(transfers[i])) { |
268 |
return true; |
269 |
} |
270 |
} |
271 |
return false; |
272 |
} |
273 |
|
274 |
} |
275 |
|