1 11 package org.eclipse.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URI ; 15 import java.util.List ; 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 48 public class CopyProjectAction extends SelectionListenerAction { 49 private static String COPY_TOOL_TIP = IDEWorkbenchMessages.CopyProjectAction_toolTip; 50 51 private static String COPY_TITLE = IDEWorkbenchMessages.CopyProjectAction_title; 52 53 private static String PROBLEMS_TITLE = IDEWorkbenchMessages.CopyProjectAction_copyFailedTitle; 54 55 58 public static final String ID = PlatformUI.PLUGIN_ID + ".CopyProjectAction"; 60 63 protected Shell shell; 64 65 69 protected IStatus errorStatus; 70 71 private String [] modelProviderIds; 72 73 79 public CopyProjectAction(Shell shell) { 80 this(shell, COPY_TITLE); 81 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 82 IIDEHelpContextIds.COPY_PROJECT_ACTION); 83 } 84 85 94 CopyProjectAction(Shell shell, String name) { 95 super(name); 96 setToolTipText(COPY_TOOL_TIP); 97 setId(CopyProjectAction.ID); 98 if (shell == null) { 99 throw new IllegalArgumentException (); 100 } 101 this.shell = shell; 102 } 103 104 116 protected IProjectDescription createDescription(IProject project, 117 String projectName, IPath rootLocation) throws CoreException { 118 IProjectDescription newDescription = project.getDescription(); 120 newDescription.setName(projectName); 121 122 if (rootLocation.equals(Platform.getLocation())) { 124 newDescription.setLocation(null); 125 } else { 126 newDescription.setLocation(rootLocation); 127 } 128 129 return newDescription; 130 } 131 132 141 void displayError(String message) { 142 MessageDialog.openError(this.shell, getErrorsTitle(), message); 143 } 144 145 153 protected String getErrorsTitle() { 154 return PROBLEMS_TITLE; 155 } 156 157 162 protected org.eclipse.ui.plugin.AbstractUIPlugin getPlugin() { 163 return (AbstractUIPlugin) Platform.getPlugin(PlatformUI.PLUGIN_ID); 164 } 165 166 178 boolean performCopy(final IProject project, final String projectName, 179 final URI 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 e) { 203 return false; 204 } catch (InvocationTargetException e) { 205 displayError(NLS.bind( 206 IDEWorkbenchMessages.CopyProjectAction_internalError, e 207 .getTargetException().getMessage())); 208 return false; 209 } 210 211 return true; 212 } 213 214 222 protected Object [] 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 237 final void recordError(CoreException error) { 238 this.errorStatus = error.getStatus(); 239 } 240 241 244 public void run() { 245 246 errorStatus = null; 247 248 IProject project = (IProject) getSelectedResources().get(0); 249 250 Object [] destinationPaths = queryDestinationParameters(project); 252 if (destinationPaths == null) { 253 return; 254 } 255 256 String newName = (String ) destinationPaths[0]; 257 URI newLocation = URIUtil.toURI((String ) destinationPaths[1]); 258 259 boolean completed = performCopy(project, newName, newLocation); 260 261 if (!completed) { 262 return; } 264 265 if (errorStatus != null) { 267 ErrorDialog.openError(this.shell, getErrorsTitle(), null, 268 errorStatus); 269 errorStatus = null; 270 } 271 } 272 273 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 List 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 307 public String [] getModelProviderIds() { 308 return modelProviderIds; 309 } 310 311 321 public void setModelProviderIds(String [] modelProviderIds) { 322 this.modelProviderIds = modelProviderIds; 323 } 324 } 325 | Popular Tags |