KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.filesystem.URIUtil;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IProjectDescription;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.osgi.util.NLS;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.dialogs.ProjectLocationSelectionDialog;
35 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
36 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
37 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
38 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
39 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
40 import org.eclipse.ui.plugin.AbstractUIPlugin;
41
42 /**
43  * The CopyProjectAction is the action designed to copy projects specifically as
44  * they have different semantics from other resources. Note that this action
45  * assumes that a single project is selected and being manipulated. This should
46  * be disabled for multi select or no selection.
47  */

48 public class CopyProjectAction extends SelectionListenerAction {
49     private static String JavaDoc COPY_TOOL_TIP = IDEWorkbenchMessages.CopyProjectAction_toolTip;
50
51     private static String JavaDoc COPY_TITLE = IDEWorkbenchMessages.CopyProjectAction_title;
52
53     private static String JavaDoc PROBLEMS_TITLE = IDEWorkbenchMessages.CopyProjectAction_copyFailedTitle;
54
55     /**
56      * The id of this action.
57      */

58     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".CopyProjectAction";//$NON-NLS-1$
59

60     /**
61      * The shell in which to show any dialogs.
62      */

63     protected Shell shell;
64
65     /**
66      * Status containing the errors detected when running the operation or
67      * <code>null</code> if no errors detected.
68      */

69     protected IStatus errorStatus;
70
71     private String JavaDoc[] modelProviderIds;
72
73     /**
74      * Creates a new project copy action with the default text.
75      *
76      * @param shell
77      * the shell for any dialogs
78      */

79     public CopyProjectAction(Shell shell) {
80         this(shell, COPY_TITLE);
81         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
82                 IIDEHelpContextIds.COPY_PROJECT_ACTION);
83     }
84
85     /**
86      * Creates a new project copy action with the given text.
87      *
88      * @param shell
89      * the shell for any dialogs
90      * @param name
91      * the string used as the text for the action, or
92      * <code>null</code> if there is no text
93      */

94     CopyProjectAction(Shell shell, String JavaDoc name) {
95         super(name);
96         setToolTipText(COPY_TOOL_TIP);
97         setId(CopyProjectAction.ID);
98         if (shell == null) {
99             throw new IllegalArgumentException JavaDoc();
100         }
101         this.shell = shell;
102     }
103
104     /**
105      * Create a new IProjectDescription for the copy using the name and path
106      * selected from the dialog.
107      *
108      * @return IProjectDescription
109      * @param project
110      * the source project
111      * @param projectName
112      * the name for the new project
113      * @param rootLocation
114      * the path the new project will be stored under.
115      */

116     protected IProjectDescription createDescription(IProject project,
117             String JavaDoc projectName, IPath rootLocation) throws CoreException {
118         // Get a copy of the current description and modify it
119
IProjectDescription newDescription = project.getDescription();
120         newDescription.setName(projectName);
121
122         // If the location is the default then set the location to null
123
if (rootLocation.equals(Platform.getLocation())) {
124             newDescription.setLocation(null);
125         } else {
126             newDescription.setLocation(rootLocation);
127         }
128
129         return newDescription;
130     }
131
132     /**
133      * Opens an error dialog to display the given message.
134      * <p>
135      * Note that this method must be called from UI thread.
136      * </p>
137      *
138      * @param message
139      * the message
140      */

141     void displayError(String JavaDoc message) {
142         MessageDialog.openError(this.shell, getErrorsTitle(), message);
143     }
144
145     /**
146      * Return the title of the errors dialog.
147      *
148      * @return java.lang.String
149      *
150      * @deprecated As of 3.3, the undoable operation created by this action
151      * handles error dialogs.
152      */

153     protected String JavaDoc getErrorsTitle() {
154         return PROBLEMS_TITLE;
155     }
156
157     /**
158      * Get the plugin used by a copy action
159      *
160      * @return AbstractUIPlugin
161      */

162     protected org.eclipse.ui.plugin.AbstractUIPlugin getPlugin() {
163         return (AbstractUIPlugin) Platform.getPlugin(PlatformUI.PLUGIN_ID);
164     }
165
166     /**
167      * Copies the project to the new values.
168      *
169      * @param project
170      * the project to copy
171      * @param projectName
172      * the name of the copy
173      * @param newLocation
174      * URI
175      * @return <code>true</code> if the copy operation completed, and
176      * <code>false</code> if it was abandoned part way
177      */

178     boolean performCopy(final IProject project, final String JavaDoc projectName,
179             final URI JavaDoc newLocation) {
180         IRunnableWithProgress op = new IRunnableWithProgress() {
181             public void run(IProgressMonitor monitor) {
182                 org.eclipse.ui.ide.undo.CopyProjectOperation op = new org.eclipse.ui.ide.undo.CopyProjectOperation(
183                         project, projectName, newLocation, getText());
184                 op.setModelProviderIds(getModelProviderIds());
185                 try {
186                     PlatformUI.getWorkbench().getOperationSupport()
187                             .getOperationHistory().execute(op, monitor,
188                                     WorkspaceUndoUtil.getUIInfoAdapter(shell));
189                 } catch (ExecutionException e) {
190                     if (e.getCause() instanceof CoreException) {
191                         recordError((CoreException)e.getCause());
192                     } else {
193                         IDEWorkbenchPlugin.log(e.getMessage(), e);
194                         displayError(e.getMessage());
195                     }
196                 }
197             }
198         };
199
200         try {
201             new ProgressMonitorJobsDialog(shell).run(true, true, op);
202         } catch (InterruptedException JavaDoc e) {
203             return false;
204         } catch (InvocationTargetException JavaDoc e) {
205             displayError(NLS.bind(
206                     IDEWorkbenchMessages.CopyProjectAction_internalError, e
207                             .getTargetException().getMessage()));
208             return false;
209         }
210
211         return true;
212     }
213
214     /**
215      * Query for a new project name and destination using the parameters in the
216      * existing project.
217      *
218      * @return Object [] or null if the selection is cancelled
219      * @param project
220      * the project we are going to copy.
221      */

222     protected Object JavaDoc[] queryDestinationParameters(IProject project) {
223         ProjectLocationSelectionDialog dialog = new ProjectLocationSelectionDialog(
224                 shell, project);
225         dialog.setTitle(IDEWorkbenchMessages.CopyProjectAction_copyTitle);
226         dialog.open();
227         return dialog.getResult();
228     }
229
230     /**
231      * Records the core exception to be displayed to the user once the action is
232      * finished.
233      *
234      * @param error
235      * a <code>CoreException</code>
236      */

237     final void recordError(CoreException error) {
238         this.errorStatus = error.getStatus();
239     }
240
241     /**
242      * Implementation of method defined on <code>IAction</code>.
243      */

244     public void run() {
245
246         errorStatus = null;
247
248         IProject project = (IProject) getSelectedResources().get(0);
249
250         // Get the project name and location in a two element list
251
Object JavaDoc[] destinationPaths = queryDestinationParameters(project);
252         if (destinationPaths == null) {
253             return;
254         }
255
256         String JavaDoc newName = (String JavaDoc) destinationPaths[0];
257         URI JavaDoc newLocation = URIUtil.toURI((String JavaDoc) destinationPaths[1]);
258
259         boolean completed = performCopy(project, newName, newLocation);
260
261         if (!completed) {
262             return; // not appropriate to show errors
263
}
264
265         // If errors occurred, open an Error dialog
266
if (errorStatus != null) {
267             ErrorDialog.openError(this.shell, getErrorsTitle(), null,
268                     errorStatus);
269             errorStatus = null;
270         }
271     }
272
273     /**
274      * The <code>CopyResourceAction</code> implementation of this
275      * <code>SelectionListenerAction</code> method enables this action only if
276      * there is a single selection which is a project.
277      */

278     protected boolean updateSelection(IStructuredSelection selection) {
279         if (!super.updateSelection(selection)) {
280             return false;
281         }
282         if (getSelectedNonResources().size() > 0) {
283             return false;
284         }
285
286         // to enable this command there must be one project selected and nothing
287
// else
288
List JavaDoc selectedResources = getSelectedResources();
289         if (selectedResources.size() != 1) {
290             return false;
291         }
292         IResource source = (IResource) selectedResources.get(0);
293         if (source instanceof IProject && ((IProject) source).isOpen()) {
294             return true;
295         }
296         return false;
297     }
298
299     /**
300      * Returns the model provider ids that are known to the client that
301      * instantiated this operation.
302      *
303      * @return the model provider ids that are known to the client that
304      * instantiated this operation.
305      * @since 3.2
306      */

307     public String JavaDoc[] getModelProviderIds() {
308         return modelProviderIds;
309     }
310
311     /**
312      * Sets the model provider ids that are known to the client that
313      * instantiated this operation. Any potential side effects reported by these
314      * models during validation will be ignored.
315      *
316      * @param modelProviderIds
317      * the model providers known to the client who is using this
318      * operation.
319      * @since 3.2
320      */

321     public void setModelProviderIds(String JavaDoc[] modelProviderIds) {
322         this.modelProviderIds = modelProviderIds;
323     }
324 }
325
Popular Tags