KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.filesystem.URIUtil;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
20 import org.eclipse.core.resources.mapping.ResourceChangeValidator;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.MultiStatus;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.dialogs.ProjectLocationSelectionDialog;
34 import org.eclipse.ui.ide.IDE;
35 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
36 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
37 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
38
39 /**
40  * Implementation class to perform the actual copying of project resources from
41  * the clipboard when paste action is invoked.
42  * <p>
43  * This class may be instantiated; it is not intended to be subclassed.
44  * </p>
45  */

46 public class CopyProjectOperation {
47
48     /**
49      * Status containing the errors detected when running the operation or
50      * <code>null</code> if no errors detected.
51      */

52     private MultiStatus errorStatus;
53
54     /**
55      * The parent shell used to show any dialogs.
56      */

57     private Shell parentShell;
58
59     private String JavaDoc[] modelProviderIds;
60
61     /**
62      * Validates that the copy of the project will not have undesirable side
63      * effects.
64      *
65      * @param shell
66      * a shell
67      * @param project
68      * the project being copied
69      * @param newName
70      * the new name of the project
71      * @param modelProviderIds
72      * the model provider ids of models that are known to the client
73      * (and can hence be ignored)
74      * @return whether the operation should proceed
75      * @since 3.2
76      * @deprecated As of 3.3, validation is performed in the undoable operation
77      * executed by this operation.
78      */

79     protected static boolean validateCopy(Shell shell, IProject project,
80             String JavaDoc newName, String JavaDoc[] modelProviderIds) {
81         IResourceChangeDescriptionFactory factory = ResourceChangeValidator
82                 .getValidator().createDeltaFactory();
83         factory.copy(project, new Path(newName));
84         return IDE.promptToConfirm(shell,
85                 IDEWorkbenchMessages.CopyProjectAction_confirm, NLS.bind(
86                         IDEWorkbenchMessages.CopyProjectAction_warning, project
87                                 .getName()), factory.getDelta(),
88                 modelProviderIds, false /* no need to sync exec */);
89     }
90
91     /**
92      * Create a new operation initialized with a shell.
93      *
94      * @param shell
95      * parent shell for error dialogs
96      */

97     public CopyProjectOperation(Shell shell) {
98         parentShell = shell;
99     }
100
101     /**
102      * Paste a copy of the project on the clipboard to the workspace.
103      *
104      * @param project
105      * The project that is beign copied.
106      */

107     public void copyProject(IProject project) {
108         errorStatus = null;
109
110         // Get the project name and location in a two element list
111
ProjectLocationSelectionDialog dialog = new ProjectLocationSelectionDialog(
112                 parentShell, project);
113         dialog.setTitle(IDEWorkbenchMessages.CopyProjectOperation_copyProject);
114         if (dialog.open() != Window.OK) {
115             return;
116         }
117
118         Object JavaDoc[] destinationPaths = dialog.getResult();
119         if (destinationPaths == null) {
120             return;
121         }
122
123         String JavaDoc newName = (String JavaDoc) destinationPaths[0];
124         URI JavaDoc newLocation = URIUtil.toURI((String JavaDoc)destinationPaths[1]);
125
126         boolean completed = performProjectCopy(project, newName, newLocation);
127
128         if (!completed) {
129             return; // not appropriate to show errors
130
}
131
132         // If errors occurred, open an Error dialog
133
if (errorStatus != null) {
134             ErrorDialog.openError(parentShell,
135                     IDEWorkbenchMessages.CopyProjectOperation_copyFailedTitle,
136                     null, errorStatus);
137             errorStatus = null;
138         }
139     }
140
141     /**
142      * Copies the project to the new values.
143      *
144      * @param project
145      * the project to copy
146      * @param projectName
147      * the name of the copy
148      * @param newLocation
149      * IPath
150      * @return <code>true</code> if the copy operation completed, and
151      * <code>false</code> if it was abandoned part way
152      */

153     private boolean performProjectCopy(final IProject project,
154             final String JavaDoc projectName, final URI JavaDoc newLocation) {
155         IRunnableWithProgress op = new IRunnableWithProgress() {
156             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
157                 org.eclipse.ui.ide.undo.CopyProjectOperation op = new org.eclipse.ui.ide.undo.CopyProjectOperation(
158                         project, projectName, newLocation,
159                         IDEWorkbenchMessages.CopyProjectOperation_copyProject);
160                 op.setModelProviderIds(getModelProviderIds());
161                 try {
162                     PlatformUI.getWorkbench().getOperationSupport()
163                             .getOperationHistory().execute(
164                                     op,
165                                     monitor,
166                                     WorkspaceUndoUtil
167                                             .getUIInfoAdapter(parentShell));
168                 } catch (final ExecutionException e) {
169                     if (e.getCause() instanceof CoreException) {
170                         recordError((CoreException)e.getCause());
171                     } else {
172                         throw new InvocationTargetException JavaDoc(e);
173                     }
174                 }
175             }
176         };
177
178         try {
179             new ProgressMonitorJobsDialog(parentShell).run(true, true, op);
180         } catch (InterruptedException JavaDoc e) {
181             return false;
182         } catch (InvocationTargetException JavaDoc e) {
183             final String JavaDoc message = e.getTargetException().getMessage();
184             parentShell.getDisplay().syncExec(new Runnable JavaDoc() {
185                 public void run() {
186                     MessageDialog
187                             .openError(
188                                     parentShell,
189                                     IDEWorkbenchMessages.CopyProjectOperation_copyFailedTitle,
190                                     NLS
191                                             .bind(
192                                                     IDEWorkbenchMessages.CopyProjectOperation_internalError,
193                                                     message));
194                 }
195             });
196             return false;
197         }
198
199         return true;
200     }
201
202     /**
203      * Records the core exception to be displayed to the user
204      * once the action is finished.
205      *
206      * @param error a <code>CoreException</code>
207      */

208     private void recordError(CoreException error) {
209
210         if (errorStatus == null) {
211             errorStatus = new MultiStatus(
212                     PlatformUI.PLUGIN_ID,
213                     IStatus.ERROR,
214                     IDEWorkbenchMessages.CopyProjectOperation_copyFailedMessage,
215                     error);
216         }
217
218         errorStatus.merge(error.getStatus());
219     }
220
221     /**
222      * Returns the model provider ids that are known to the client
223      * that instantiated this operation.
224      *
225      * @return the model provider ids that are known to the client
226      * that instantiated this operation.
227      * @since 3.2
228      */

229     public String JavaDoc[] getModelProviderIds() {
230         return modelProviderIds;
231     }
232
233     /**
234      * Sets the model provider ids that are known to the client that
235      * instantiated this operation. Any potential side effects reported by these
236      * models during validation will be ignored.
237      *
238      * @param modelProviderIds
239      * the model providers known to the client who is using this
240      * operation.
241      * @since 3.2
242      */

243     public void setModelProviderIds(String JavaDoc[] modelProviderIds) {
244         this.modelProviderIds = modelProviderIds;
245     }
246 }
247
Popular Tags