1 11 package org.eclipse.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URI ; 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 46 public class CopyProjectOperation { 47 48 52 private MultiStatus errorStatus; 53 54 57 private Shell parentShell; 58 59 private String [] modelProviderIds; 60 61 79 protected static boolean validateCopy(Shell shell, IProject project, 80 String newName, String [] 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 ); 89 } 90 91 97 public CopyProjectOperation(Shell shell) { 98 parentShell = shell; 99 } 100 101 107 public void copyProject(IProject project) { 108 errorStatus = null; 109 110 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 [] destinationPaths = dialog.getResult(); 119 if (destinationPaths == null) { 120 return; 121 } 122 123 String newName = (String ) destinationPaths[0]; 124 URI newLocation = URIUtil.toURI((String )destinationPaths[1]); 125 126 boolean completed = performProjectCopy(project, newName, newLocation); 127 128 if (!completed) { 129 return; } 131 132 if (errorStatus != null) { 134 ErrorDialog.openError(parentShell, 135 IDEWorkbenchMessages.CopyProjectOperation_copyFailedTitle, 136 null, errorStatus); 137 errorStatus = null; 138 } 139 } 140 141 153 private boolean performProjectCopy(final IProject project, 154 final String projectName, final URI newLocation) { 155 IRunnableWithProgress op = new IRunnableWithProgress() { 156 public void run(IProgressMonitor monitor) throws InvocationTargetException { 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 (e); 173 } 174 } 175 } 176 }; 177 178 try { 179 new ProgressMonitorJobsDialog(parentShell).run(true, true, op); 180 } catch (InterruptedException e) { 181 return false; 182 } catch (InvocationTargetException e) { 183 final String message = e.getTargetException().getMessage(); 184 parentShell.getDisplay().syncExec(new Runnable () { 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 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 229 public String [] getModelProviderIds() { 230 return modelProviderIds; 231 } 232 233 243 public void setModelProviderIds(String [] modelProviderIds) { 244 this.modelProviderIds = modelProviderIds; 245 } 246 } 247 | Popular Tags |