KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > CopyResourceAction


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.actions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspace;
19 import org.eclipse.core.resources.IWorkspaceRoot;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
25 import org.eclipse.ui.dialogs.ISelectionValidator;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
28 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
29
30 /**
31  * Standard action for copying the currently selected resources elsewhere
32  * in the workspace. All resources being copied as a group must be siblings.
33  * <p>
34  * This class may be instantiated; it is not intended to be subclassed.
35  * </p>
36  */

37 public class CopyResourceAction extends SelectionListenerAction implements
38         ISelectionValidator {
39
40     /**
41      * The id of this action.
42      */

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

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

49     private Shell shell;
50
51     /**
52      * The operation to run. This is created only during the life-cycle of the
53      * run method.
54      */

55     protected CopyFilesAndFoldersOperation operation;
56
57     private String JavaDoc[] modelProviderIds;
58
59     /**
60      * Returns a new name for a copy of the resource at the given path in the given
61      * workspace. This name could be determined either automatically or by querying
62      * the user. This name will <b>not</b> be verified by the caller, so it must be
63      * valid and unique.
64      * <p>
65      * Note this method is for internal use only.
66      * </p>
67      *
68      * @param originalName the full path of the resource
69      * @param workspace the workspace
70      * @return the new full path for the copy, or <code>null</code> if the resource
71      * should not be copied
72      */

73     public static IPath getNewNameFor(IPath originalName, IWorkspace workspace) {
74         return CopyFilesAndFoldersOperation.getAutoNewNameFor(originalName,
75                 workspace);
76     }
77
78     /**
79      * Creates a new action.
80      *
81      * @param shell the shell for any dialogs
82      */

83     public CopyResourceAction(Shell shell) {
84         this(shell, IDEWorkbenchMessages.CopyResourceAction_title);
85         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
86                 IIDEHelpContextIds.COPY_RESOURCE_ACTION);
87     }
88
89     /**
90      * Creates a new action with the given text.
91      *
92      * @param shell the shell for any dialogs
93      * @param name the string used as the name for the action,
94      * or <code>null</code> if there is no name
95      */

96     CopyResourceAction(Shell shell, String JavaDoc name) {
97         super(name);
98         setToolTipText(IDEWorkbenchMessages.CopyResourceAction_toolTip);
99         setId(CopyResourceAction.ID);
100         if (shell == null) {
101             throw new IllegalArgumentException JavaDoc();
102         }
103         this.shell = shell;
104     }
105
106     /**
107      * Returns the operation to perform when this action runs.
108      *
109      * @return the operation to perform when this action runs.
110      */

111     protected CopyFilesAndFoldersOperation createOperation() {
112         return new CopyFilesAndFoldersOperation(getShell());
113     }
114
115     /**
116      * Returns the path of the container to initially select in the container
117      * selection dialog, or <code>null</code> if there is no initial selection
118      * @return The initial container; <code>null</code> if none.
119      */

120     IContainer getInitialContainer() {
121         List JavaDoc resources = getSelectedResources();
122         if (resources.size() > 0) {
123             IResource resource = (IResource) resources.get(0);
124             return resource.getParent();
125         }
126         return null;
127     }
128
129     /**
130      * Returns an array of resources to use for the operation from
131      * the provided list.
132      *
133      * @param resourceList The list of resources to converted into an array.
134      * @return an array of resources to use for the operation
135      */

136     protected IResource[] getResources(List JavaDoc resourceList) {
137         return (IResource[]) resourceList.toArray(new IResource[resourceList
138                 .size()]);
139     }
140
141     /**
142      * Returns the shell in which to show any dialogs
143      * @return The shell for parenting dialogs; never <code>null</code>.
144      */

145     Shell getShell() {
146         return shell;
147     }
148
149     /**
150      * The <code>CopyResourceAction</code> implementation of this
151      * <code>ISelectionValidator</code> method checks whether the given path
152      * is a good place to copy the selected resources.
153      */

154     public String JavaDoc isValid(Object JavaDoc destination) {
155         IWorkspaceRoot root = IDEWorkbenchPlugin.getPluginWorkspace().getRoot();
156         IContainer container = (IContainer) root
157                 .findMember((IPath) destination);
158
159         if (container != null) {
160             // create a new operation here.
161
// isValid is API and may be called in any context.
162
CopyFilesAndFoldersOperation newOperation = createOperation();
163             List JavaDoc sources = getSelectedResources();
164             IResource[] resources = (IResource[]) sources
165                     .toArray(new IResource[sources.size()]);
166             return newOperation.validateDestination(container, resources);
167         }
168         return null;
169     }
170
171     /**
172      * Asks the user for the destination of this action.
173      *
174      * @return the path on an existing or new resource container, or
175      * <code>null</code> if the operation should be abandoned
176      */

177     IPath queryDestinationResource() {
178         // start traversal at root resource, should probably start at a
179
// better location in the tree
180
ContainerSelectionDialog dialog = new ContainerSelectionDialog(shell,
181                 getInitialContainer(), true, IDEWorkbenchMessages.CopyResourceAction_selectDestination);
182         dialog.setValidator(this);
183         dialog.showClosedProjects(false);
184         dialog.open();
185         Object JavaDoc[] result = dialog.getResult();
186         if (result != null && result.length == 1) {
187             return (IPath) result[0];
188         }
189         return null;
190     }
191
192     /* (non-Javadoc)
193      * Method declared on IAction.
194      */

195     public void run() {
196         try {
197             operation = createOperation();
198             operation.setModelProviderIds(getModelProviderIds());
199
200             // WARNING: do not query the selected resources more than once
201
// since the selection may change during the run,
202
// e.g. due to window activation when the prompt dialog is dismissed.
203
// For more details, see Bug 60606 [Navigator] (data loss) Navigator deletes/moves the wrong file
204
List JavaDoc sources = getSelectedResources();
205
206             IPath destination = queryDestinationResource();
207             if (destination == null) {
208                 return;
209             }
210
211             IWorkspaceRoot root = IDEWorkbenchPlugin.getPluginWorkspace()
212                     .getRoot();
213             IContainer container = (IContainer) root.findMember(destination);
214             if (container == null) {
215                 return;
216             }
217
218             runOperation(getResources(sources), container);
219         } finally {
220             operation = null;
221         }
222     }
223
224     /**
225      * Runs the operation created in <code>createOperaiton</code>
226      *
227      * @param resources source resources to pass to the operation
228      * @param destination destination container to pass to the operation
229      */

230     protected void runOperation(IResource[] resources, IContainer destination) {
231         operation.copyResources(resources, destination);
232     }
233
234     /**
235      * The <code>CopyResourceAction</code> implementation of this
236      * <code>SelectionListenerAction</code> method enables this action only if
237      * all of the one or more selections are sibling resources which are
238      * local (depth infinity).
239      */

240     protected boolean updateSelection(IStructuredSelection selection) {
241         if (!super.updateSelection(selection)) {
242             return false;
243         }
244         if (getSelectedNonResources().size() > 0) {
245             return false;
246         }
247
248         // to enable this command all selected resources must be siblings
249
List JavaDoc selectedResources = getSelectedResources();
250         if (selectedResources.size() == 0) {
251             return false;
252         }
253         IContainer firstParent = ((IResource) selectedResources.get(0))
254                 .getParent();
255         if (firstParent == null) {
256             return false;
257         }
258         Iterator JavaDoc resourcesEnum = selectedResources.iterator();
259         while (resourcesEnum.hasNext()) {
260             IResource currentResource = (IResource) resourcesEnum.next();
261             if (!currentResource.exists()) {
262                 return false;
263             }
264             if (currentResource.getType() == IResource.PROJECT) {
265                 return false;
266             }
267             IContainer parent = currentResource.getParent();
268             if ((parent != null) && (!parent.equals(firstParent))) {
269                 return false;
270             }
271         }
272         return true;
273     }
274     
275     /**
276      * Returns the model provider ids that are known to the client
277      * that instantiated this operation.
278      *
279      * @return the model provider ids that are known to the client
280      * that instantiated this operation.
281      * @since 3.2
282      */

283     public String JavaDoc[] getModelProviderIds() {
284         return modelProviderIds;
285     }
286
287     /**
288      * Sets the model provider ids that are known to the client
289      * that instantiated this operation. Any potential side effects
290      * reported by these models during validation will be ignored.
291      *
292      * @param modelProviderIds the model providers known to the client
293      * who is using this operation.
294      * @since 3.2
295      */

296     public void setModelProviderIds(String JavaDoc[] modelProviderIds) {
297         this.modelProviderIds = modelProviderIds;
298     }
299 }
300
Popular Tags