1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IProjectDescription; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IResourceStatus; 19 import org.eclipse.core.resources.IWorkspace; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.OperationCanceledException; 26 import org.eclipse.core.runtime.Platform; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 import org.eclipse.jface.dialogs.ErrorDialog; 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.wizard.Wizard; 32 import org.eclipse.osgi.util.NLS; 33 import org.eclipse.ui.PlatformUI; 34 import org.eclipse.ui.actions.WorkspaceModifyOperation; 35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 36 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 37 38 44 public class CreateProjectWizard extends Wizard { 45 private NewProjectWizard wizard; 46 47 private WizardNewProjectNameAndLocationPage page; 48 49 53 CreateProjectWizard(WizardNewProjectNameAndLocationPage page, 54 NewProjectWizard wizard) { 55 super(); 56 this.page = page; 57 this.wizard = wizard; 58 } 59 60 66 private IProject createNewProject() { 67 final IProject newProjectHandle = page.getProjectHandle(); 69 70 IPath defaultPath = Platform.getLocation(); 72 IPath newPath = page.getLocationPath(); 73 if (defaultPath.equals(newPath)) { 74 newPath = null; 75 } 76 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 77 final IProjectDescription description = workspace 78 .newProjectDescription(newProjectHandle.getName()); 79 description.setLocation(newPath); 80 81 WorkspaceModifyOperation op = new WorkspaceModifyOperation() { 83 protected void execute(IProgressMonitor monitor) 84 throws CoreException { 85 createProject(description, newProjectHandle, monitor); 86 } 87 }; 88 89 try { 91 getContainer().run(true, true, op); 92 } catch (InterruptedException e) { 93 return null; 94 } catch (InvocationTargetException e) { 95 Throwable t = e.getTargetException(); 96 if (t instanceof CoreException) { 97 if (((CoreException) t).getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) { 98 MessageDialog 99 .openError( 100 getShell(), 101 IDEWorkbenchMessages.CreateProjectWizard_errorTitle, 102 IDEWorkbenchMessages.CreateProjectWizard_caseVariantExistsError 103 ); 104 } else { 105 ErrorDialog.openError(getShell(), IDEWorkbenchMessages.CreateProjectWizard_errorTitle, 106 null, ((CoreException) t).getStatus()); 108 } 109 } else { 110 IDEWorkbenchPlugin.getDefault().getLog().log( 112 new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, t 113 .toString(), t)); 114 MessageDialog 115 .openError( 116 getShell(), 117 IDEWorkbenchMessages.CreateProjectWizard_errorTitle, 118 NLS.bind(IDEWorkbenchMessages.CreateProjectWizard_internalError, t.getMessage())); 119 } 120 return null; 121 } 122 123 return newProjectHandle; 124 } 125 126 136 private void createProject(IProjectDescription description, 137 IProject projectHandle, IProgressMonitor monitor) 138 throws CoreException, OperationCanceledException { 139 try { 140 monitor.beginTask("", 2000); 142 projectHandle.create(description, new SubProgressMonitor(monitor, 143 1000)); 144 145 if (monitor.isCanceled()) { 146 throw new OperationCanceledException(); 147 } 148 149 projectHandle.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1000)); 150 151 } finally { 152 monitor.done(); 153 } 154 } 155 156 162 String getProjectName() { 163 return page.getProjectName(); 164 } 165 166 169 public boolean performFinish() { 170 if (wizard.getNewProject() != null) { 171 return true; 172 } 173 174 IProject project = createNewProject(); 175 if (project != null) { 176 wizard.setNewProject(project); 177 return true; 178 } else { 179 return false; 180 } 181 } 182 } 183 | Popular Tags |