KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > PasteAction


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.ui.internal.navigator.resources.actions;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.dnd.Clipboard;
22 import org.eclipse.swt.dnd.FileTransfer;
23 import org.eclipse.swt.dnd.TransferData;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
27 import org.eclipse.ui.actions.CopyProjectOperation;
28 import org.eclipse.ui.actions.SelectionListenerAction;
29 import org.eclipse.ui.part.ResourceTransfer;
30
31 /**
32  * Standard action for pasting resources on the clipboard to the selected resource's location.
33  * <p>
34  * This class may be instantiated; it is not intended to be subclassed.
35  * </p>
36  *
37  * @since 2.0
38  */

39 /*package*/class PasteAction extends SelectionListenerAction {
40
41     /**
42      * The id of this action.
43      */

44     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".PasteAction";//$NON-NLS-1$
45

46     /**
47      * The shell in which to show any dialogs.
48      */

49     private Shell shell;
50
51     /**
52      * System clipboard
53      */

54     private Clipboard clipboard;
55
56     /**
57      * Creates a new action.
58      *
59      * @param shell the shell for any dialogs
60      * @param clipboard the clipboard
61      */

62     public PasteAction(Shell shell, Clipboard clipboard) {
63         super("Paste"); // TODO ResourceNavigatorMessages.PasteAction_title); //$NON-NLS-1$
64
Assert.isNotNull(shell);
65         Assert.isNotNull(clipboard);
66         this.shell = shell;
67         this.clipboard = clipboard;
68         setToolTipText("Paste ToolTip"); // TODO ResourceNavigatorMessages.PasteAction_toolTip); //$NON-NLS-1$
69
setId(PasteAction.ID);
70         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "HelpId"); //$NON-NLS-1$
71
// TODO INavigatorHelpContextIds.PASTE_ACTION);
72
}
73
74     /**
75      * Returns the actual target of the paste action. Returns null
76      * if no valid target is selected.
77      *
78      * @return the actual target of the paste action
79      */

80     private IResource getTarget() {
81         List JavaDoc selectedResources = getSelectedResources();
82
83         for (int i = 0; i < selectedResources.size(); i++) {
84             IResource resource = (IResource) selectedResources.get(i);
85
86             if (resource instanceof IProject && !((IProject) resource).isOpen()) {
87                 return null;
88             }
89             if (resource.getType() == IResource.FILE) {
90                 resource = resource.getParent();
91             }
92             if (resource != null) {
93                 return resource;
94             }
95         }
96         return null;
97     }
98
99     /**
100      * Returns whether any of the given resources are linked resources.
101      *
102      * @param resources resource to check for linked type. may be null
103      * @return true=one or more resources are linked. false=none of the
104      * resources are linked
105      */

106     private boolean isLinked(IResource[] resources) {
107         for (int i = 0; i < resources.length; i++) {
108             if (resources[i].isLinked()) {
109                 return true;
110             }
111         }
112         return false;
113     }
114
115     /**
116      * Implementation of method defined on <code>IAction</code>.
117      */

118     public void run() {
119         // try a resource transfer
120
ResourceTransfer resTransfer = ResourceTransfer.getInstance();
121         IResource[] resourceData = (IResource[]) clipboard
122                 .getContents(resTransfer);
123
124         if (resourceData != null && resourceData.length > 0) {
125             if (resourceData[0].getType() == IResource.PROJECT) {
126                 // enablement checks for all projects
127
for (int i = 0; i < resourceData.length; i++) {
128                     CopyProjectOperation operation = new CopyProjectOperation(
129                             this.shell);
130                     operation.copyProject((IProject) resourceData[i]);
131                 }
132             } else {
133                 // enablement should ensure that we always have access to a container
134
IContainer container = getContainer();
135
136                 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(
137                         this.shell);
138                 operation.copyResources(resourceData, container);
139             }
140             return;
141         }
142
143         // try a file transfer
144
FileTransfer fileTransfer = FileTransfer.getInstance();
145         String JavaDoc[] fileData = (String JavaDoc[]) clipboard.getContents(fileTransfer);
146
147         if (fileData != null) {
148             // enablement should ensure that we always have access to a container
149
IContainer container = getContainer();
150
151             CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(
152                     this.shell);
153             operation.copyFiles(fileData, container);
154         }
155     }
156
157     /**
158      * Returns the container to hold the pasted resources.
159      */

160     private IContainer getContainer() {
161         List JavaDoc selection = getSelectedResources();
162         if (selection.get(0) instanceof IFile) {
163             return ((IFile) selection.get(0)).getParent();
164         }
165         return (IContainer) selection.get(0);
166     }
167
168     /**
169      * The <code>PasteAction</code> implementation of this
170      * <code>SelectionListenerAction</code> method enables this action if
171      * a resource compatible with what is on the clipboard is selected.
172      *
173      * -Clipboard must have IResource or java.io.File
174      * -Projects can always be pasted if they are open
175      * -Workspace folder may not be copied into itself
176      * -Files and folders may be pasted to a single selected folder in open
177      * project or multiple selected files in the same folder
178      */

179     protected boolean updateSelection(IStructuredSelection selection) {
180         if (!super.updateSelection(selection)) {
181             return false;
182         }
183
184         final IResource[][] clipboardData = new IResource[1][];
185         shell.getDisplay().syncExec(new Runnable JavaDoc() {
186             public void run() {
187                 // clipboard must have resources or files
188
ResourceTransfer resTransfer = ResourceTransfer.getInstance();
189                 clipboardData[0] = (IResource[]) clipboard
190                         .getContents(resTransfer);
191             }
192         });
193         IResource[] resourceData = clipboardData[0];
194         boolean isProjectRes = resourceData != null && resourceData.length > 0
195                 && resourceData[0].getType() == IResource.PROJECT;
196
197         if (isProjectRes) {
198             for (int i = 0; i < resourceData.length; i++) {
199                 // make sure all resource data are open projects
200
// can paste open projects regardless of selection
201
if (resourceData[i].getType() != IResource.PROJECT
202                         || ((IProject) resourceData[i]).isOpen() == false) {
203                     return false;
204                 }
205             }
206             return true;
207         }
208
209         if (getSelectedNonResources().size() > 0) {
210             return false;
211         }
212
213         IResource targetResource = getTarget();
214         // targetResource is null if no valid target is selected (e.g., open project)
215
// or selection is empty
216
if (targetResource == null) {
217             return false;
218         }
219
220         // can paste files and folders to a single selection (file, folder,
221
// open project) or multiple file selection with the same parent
222
List JavaDoc selectedResources = getSelectedResources();
223         if (selectedResources.size() > 1) {
224             for (int i = 0; i < selectedResources.size(); i++) {
225                 IResource resource = (IResource) selectedResources.get(i);
226                 if (resource.getType() != IResource.FILE) {
227                     return false;
228                 }
229                 if (!targetResource.equals(resource.getParent())) {
230                     return false;
231                 }
232             }
233         }
234         if (resourceData != null) {
235             // linked resources can only be pasted into projects
236
if (isLinked(resourceData)
237                     && targetResource.getType() != IResource.PROJECT) {
238                 return false;
239             }
240
241             if (targetResource.getType() == IResource.FOLDER) {
242                 // don't try to copy folder to self
243
for (int i = 0; i < resourceData.length; i++) {
244                     if (targetResource.equals(resourceData[i])) {
245                         return false;
246                     }
247                 }
248             }
249             return true;
250         }
251         TransferData[] transfers = clipboard.getAvailableTypes();
252         FileTransfer fileTransfer = FileTransfer.getInstance();
253         for (int i = 0; i < transfers.length; i++) {
254             if (fileTransfer.isSupportedType(transfers[i])) {
255                 return true;
256             }
257         }
258         return false;
259     }
260 }
261
262
Popular Tags