/** * Creates a new project resource with the selected name. * * <p>In normal usage, this method is invoked after the user has pressed Finish on the wizard; the * enablement of the Finish button implies that all controls on the pages currently contain valid * values. * * <p>Note that this wizard caches the new project once it has been successfully created; * subsequent invocations of this method will answer the same project resource without attempting * to create it again. * * @return the created project resource, or <code>null</code> if the project was not created */ private IProject createNewProject() { if (newProject != null) { return newProject; } // get a project handle final String projectName = page.getProjectName(); IProject projectHandle = page.getProjectHandle(projectName); if (projectHandle.exists()) { String newProjectName = ProjectUtils.generateUniqueNameFrom(projectName); projectHandle = page.getProjectHandle(newProjectName); } final AbstractSample sampleContent = page.getCurrentSample(); final IProject newProjectHandle = projectHandle; // get a project descriptor URI location = page.getLocationURI(); final IProjectDescription description = createProjectDescription(newProjectHandle, location); // create the new project operation IRunnableWithProgress op = new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException { CreateProjectOperation op = new CreateProjectOperation(description, ResourceMessages.NewProject_windowTitle); try { IStatus status = op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell())); if (status.isOK()) { createdFile = createProjectContent(newProjectHandle, null, projectName, sampleContent); } } catch (ExecutionException e) { throw new InvocationTargetException(e); } catch (CoreException e) { throw new InvocationTargetException(e); } } }; try { getContainer().run(true, true, op); } catch (InterruptedException e) { return null; } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t instanceof ExecutionException && t.getCause() instanceof CoreException) { CoreException cause = (CoreException) t.getCause(); StatusAdapter status; if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) { status = new StatusAdapter( StatusUtil.newStatus( IStatus.WARNING, NLS.bind( ResourceMessages.NewProject_caseVariantExistsError, newProjectHandle.getName()), cause)); } else { status = new StatusAdapter( StatusUtil.newStatus( cause.getStatus().getSeverity(), ResourceMessages.NewProject_errorMessage, cause)); } status.setProperty( IStatusAdapterConstants.TITLE_PROPERTY, ResourceMessages.NewProject_errorMessage); StatusManager.getManager().handle(status, StatusManager.BLOCK); } else { StatusAdapter status = new StatusAdapter( new Status( IStatus.WARNING, IDEWorkbenchPlugin.IDE_WORKBENCH, 0, NLS.bind(ResourceMessages.NewProject_internalError, t.getMessage()), t)); status.setProperty( IStatusAdapterConstants.TITLE_PROPERTY, ResourceMessages.NewProject_errorMessage); StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK); } return null; } newProject = newProjectHandle; return newProject; }
private void createFolder(IPath path) { final AbstractSample sampleContent = page.getCurrentSample(); IPath containerPath = path.removeLastSegments(1); IResource container = ResourceUtil.getResource(containerPath.toFile()); if (container != null) { IPath newFolderPath = container.getFullPath().append(path.lastSegment()); final IFolder newFolderHandle = IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(newFolderPath); IRunnableWithProgress op = new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException { AbstractOperation op; op = new CreateFolderOperation( newFolderHandle, null, IDEWorkbenchMessages.WizardNewFolderCreationPage_title); try { IStatus status = op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell())); if (status.isOK()) { createdFile = createProjectContent( newProject, newFolderHandle, newFolderHandle.getName(), sampleContent); } } catch (ExecutionException e) { throw new InvocationTargetException(e); } catch (CoreException e) { throw new InvocationTargetException(e); } } }; try { getContainer().run(true, true, op); } catch (InterruptedException e) { } catch (InvocationTargetException e) { // ExecutionExceptions are handled above, but unexpected runtime // exceptions and errors may still occur. IDEWorkbenchPlugin.log( getClass(), "createNewFolder()", e.getTargetException()); // $NON-NLS-1$ MessageDialog.open( MessageDialog.ERROR, getContainer().getShell(), IDEWorkbenchMessages.WizardNewFolderCreationPage_internalErrorTitle, NLS.bind( IDEWorkbenchMessages.WizardNewFolder_internalError, e.getTargetException().getMessage()), SWT.SHEET); } newProject = newFolderHandle.getProject(); } }