Search in sources :

Example 36 with IProjectDescription

use of org.eclipse.core.resources.IProjectDescription in project knime-core by knime.

the class NewProjectWizard method doFinish.

/**
 * Worker method, creates the project using the given options.
 *
 * @param workflowPath path of the workflow to create in workspace
 * @param monitor Progress monitor
 * @throws CoreException if error while creating the project
 */
public static void doFinish(final IPath workflowPath, final IProgressMonitor monitor) throws CoreException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource resource = root.findMember(workflowPath);
    if (resource != null) {
        throwCoreException("Resource \"" + workflowPath.toString() + "\" does already exist.", null);
    }
    // check if there is a folder with the same name on the file system
    // see bug (http://bimbug.inf.uni-konstanz.de/show_bug.cgi?id=1912)
    IPath rootLocation = root.getLocation();
    if (rootLocation != null) {
        IPath absolutePath = rootLocation.append(workflowPath);
        if (absolutePath.toFile().exists()) {
            throwCoreException("Resource " + workflowPath + " already exists!", null);
        }
    }
    ContainerGenerator generator = new ContainerGenerator(workflowPath);
    IContainer containerResult = generator.generateContainer(monitor);
    if (containerResult instanceof IProject) {
        IProject project = (IProject) containerResult;
        // open the project
        project.open(monitor);
        // Create project description, set the nature IDs and build-commands
        try {
            // set the nature id of the project is enough
            // the name is already set by IProject#create()
            IProjectDescription description = project.getDescription();
            description.setNatureIds(new String[] { KNIMEProjectNature.ID });
            project.setDescription(description, monitor);
        } catch (CoreException ce) {
            throwCoreException("Error while creating project description for " + project.getName(), ce);
        }
    }
    // 
    // 2. Create the optional files, if wanted
    // 
    final IFile defaultFile = containerResult.getFile(new Path(WorkflowPersistor.WORKFLOW_FILE));
    InputStream is = new ByteArrayInputStream(new byte[0]);
    defaultFile.create(is, true, monitor);
    // open the default file, if it was created
    // open the model file in the editor
    monitor.setTaskName("Opening file for editing...");
    Display.getDefault().asyncExec(new Runnable() {

        public void run() {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            try {
                IDE.openEditor(page, defaultFile, true);
            } catch (PartInitException e) {
            // ignore it
            }
        }
    });
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IProject(org.eclipse.core.resources.IProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) ContainerGenerator(org.eclipse.ui.dialogs.ContainerGenerator) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 37 with IProjectDescription

use of org.eclipse.core.resources.IProjectDescription in project eclipse-pmd by acanda.

the class PMDNature method removeFrom.

/**
 * Removes the PMD nature from a project.
 */
public static void removeFrom(final IProject project) throws CoreException {
    if (project.hasNature(ID)) {
        final IProjectDescription description = project.getDescription();
        final ArrayList<String> natureIds = Lists.newArrayList(description.getNatureIds());
        natureIds.remove(ID);
        description.setNatureIds(natureIds.toArray(new String[natureIds.size()]));
        project.setDescription(description, null);
        MarkerUtil.removeAllMarkers(project);
    }
}
Also used : IProjectDescription(org.eclipse.core.resources.IProjectDescription)

Example 38 with IProjectDescription

use of org.eclipse.core.resources.IProjectDescription in project eclipse-pmd by acanda.

the class PMDNature method configure.

@Override
public void configure() throws CoreException {
    final IProjectDescription desc = project.getDescription();
    final ICommand[] commands = desc.getBuildSpec();
    for (int i = 0; i < commands.length; ++i) {
        if (commands[i].getBuilderName().equals(PMDBuilder.ID)) {
            return;
        }
    }
    final ICommand[] newCommands = new ICommand[commands.length + 1];
    System.arraycopy(commands, 0, newCommands, 0, commands.length);
    final ICommand command = desc.newCommand();
    command.setBuilderName(PMDBuilder.ID);
    newCommands[newCommands.length - 1] = command;
    desc.setBuildSpec(newCommands);
    project.setDescription(desc, null);
}
Also used : ICommand(org.eclipse.core.resources.ICommand) IProjectDescription(org.eclipse.core.resources.IProjectDescription)

Example 39 with IProjectDescription

use of org.eclipse.core.resources.IProjectDescription in project eclipse-pmd by acanda.

the class PMDNature method addTo.

/**
 * Adds the PMD nature to a project.
 */
public static void addTo(final IProject project) throws CoreException {
    if (!project.hasNature(ID)) {
        final IProjectDescription description = project.getDescription();
        final ArrayList<String> natureIds = Lists.newArrayList(description.getNatureIds());
        natureIds.add(ID);
        description.setNatureIds(natureIds.toArray(new String[natureIds.size()]));
        project.setDescription(description, null);
        MarkerUtil.removeAllMarkers(project);
    }
}
Also used : IProjectDescription(org.eclipse.core.resources.IProjectDescription)

Example 40 with IProjectDescription

use of org.eclipse.core.resources.IProjectDescription in project eclipse-pmd by acanda.

the class PMDNatureTest method addToDoesNotAddPMDNatureToProject.

/**
 * Verifies that {@link PMDNature#addTo(IProject)} does not change the nature ids if the project already has it.
 */
@Test
public void addToDoesNotAddPMDNatureToProject() throws CoreException {
    final IProject project = mock(IProject.class);
    final IProjectDescription description = mock(IProjectDescription.class);
    when(project.getDescription()).thenReturn(description);
    when(project.hasNature(PMDNature.ID)).thenReturn(true);
    when(description.getNatureIds()).thenReturn(new String[] { "org.example.a", PMDNature.ID, "org.example.b" });
    PMDNature.addTo(project);
    verify(project, never()).setDescription(any(IProjectDescription.class), any(IProgressMonitor.class));
    verify(description, never()).setNatureIds(any(String[].class));
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test)

Aggregations

IProjectDescription (org.eclipse.core.resources.IProjectDescription)68 IProject (org.eclipse.core.resources.IProject)35 CoreException (org.eclipse.core.runtime.CoreException)18 IWorkspace (org.eclipse.core.resources.IWorkspace)16 IPath (org.eclipse.core.runtime.IPath)13 ICommand (org.eclipse.core.resources.ICommand)12 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)11 File (java.io.File)9 IStatus (org.eclipse.core.runtime.IStatus)9 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)8 Path (org.eclipse.core.runtime.Path)8 Status (org.eclipse.core.runtime.Status)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 URI (java.net.URI)5 IResource (org.eclipse.core.resources.IResource)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 InputStream (java.io.InputStream)3 IFile (org.eclipse.core.resources.IFile)3 Resource (org.eclipse.emf.ecore.resource.Resource)3