KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > 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.views.navigator;
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.internal.views.navigator.ResourceNavigatorMessages;
30 import org.eclipse.ui.part.ResourceTransfer;
31
32 /**
33  * Standard action for pasting resources on the clipboard to the selected resource's location.
34  * <p>
35  * This class may be instantiated; it is not intended to be subclassed.
36  * </p>
37  *
38  * @since 2.0
39  */

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

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

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

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

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

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

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

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

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

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

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