Search in sources :

Example 76 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot 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 77 with IWorkspaceRoot

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

the class WorkflowGroupSelectionDialog method pathToTreeSelection.

/**
 * @param path the path of a resource
 * @return the selection to be passed to a tree in order to select the
 *  resource denoted by the given path
 */
public static IStructuredSelection pathToTreeSelection(final IPath path) {
    if (path != null) {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IResource r = root.findMember(path);
        if (r != null && !r.equals(root)) {
            IContainer c = r.getParent();
            if (r instanceof IContainer) {
                c = (IContainer) r;
            }
            String[] segments = c.getFullPath().segments();
            Object[] treePathSegments = new Object[segments.length];
            // find all parents in order to create the path segments
            int i = 1;
            while (c.getParent() != null) {
                treePathSegments[treePathSegments.length - i] = c;
                c = c.getParent();
                i++;
            }
            TreePath treePath = new TreePath(treePathSegments);
            return new TreeSelection(treePath);
        }
    }
    // default: return empty selection
    return new TreeSelection();
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 78 with IWorkspaceRoot

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

the class V07ToV08ConverterTest method convertWorkspaceLocation.

@Test
public void convertWorkspaceLocation() {
    final RuleSetConfiguration config = new WorkspaceRuleSetConfiguration(1, "Workspace Config", Paths.get("../src/ch.acanda.eclipse.pmd/pmd.xml"));
    final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
    when(workspaceRoot.getLocationURI()).thenReturn(URI.create("file:///home/workspace/"));
    doReturn(createProjects()).when(workspaceRoot).getProjects();
    final Location result = V07ToV08Converter.getLocation(config, workspaceRoot);
    assertEquals("Location path", Paths.get("ch.acanda.eclipse.pmd", "pmd.xml").toString(), result.getPath());
    assertEquals("Location context", LocationContext.WORKSPACE, result.getContext());
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 79 with IWorkspaceRoot

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

the class AddRuleSetConfigurationModelTest method validateWorkspaceConfigurationWithProjectOutsideWorkspace.

/**
 * Verifies that a rule set configuration in a project outside of the workspace does not produce an error in the
 * "Add Rule Set Configuration" wizard.
 */
@Test
public void validateWorkspaceConfigurationWithProjectOutsideWorkspace() throws IOException {
    final Path ruleSetFile = createRuleSetFile();
    final IProject project = mock(IProject.class);
    final IWorkspace workspace = mock(IWorkspace.class);
    final IWorkspaceRoot root = mock(IWorkspaceRoot.class);
    when(project.getWorkspace()).thenReturn(workspace);
    when(workspace.getRoot()).thenReturn(root);
    when(root.getProject(anyString())).thenReturn(project);
    when(project.getLocationURI()).thenReturn(ruleSetFile.getParent().toUri());
    final AddRuleSetConfigurationModel model = new AddRuleSetConfigurationModel(project);
    model.setWorkspaceTypeSelected(true);
    model.setName("X");
    model.setLocation("ProjectX/" + ruleSetFile.getName(ruleSetFile.getNameCount() - 1));
    final ValidationResult validationResult = new ValidationResult();
    model.validate(AddRuleSetConfigurationModel.LOCATION, validationResult);
    if (validationResult.hasErrors()) {
        final String msg = "The validation should not result in any errors " + "if the project is located outside the workspace. First error: ";
        fail(msg + validationResult.getFirstErrorMessage());
    }
}
Also used : Path(java.nio.file.Path) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) Matchers.anyString(org.mockito.Matchers.anyString) ValidationResult(ch.acanda.eclipse.pmd.ui.model.ValidationResult) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test)

Example 80 with IWorkspaceRoot

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

the class LocationResolver method resolveWorkspaceLocation.

private static Path resolveWorkspaceLocation(final Location location, final IProject project) {
    // format of the location's path: <project-name>/<project-relative-path>
    if (location.getPath().trim().length() > 0) {
        final Path locationPath = Paths.get(toOSPath(location.getPath()));
        if (locationPath.getNameCount() >= 2) {
            final String projectName = locationPath.getName(0).toString();
            final IWorkspaceRoot root = project.getWorkspace().getRoot();
            final URI locationURI = root.getProject(projectName).getLocationURI();
            if (locationURI != null) {
                final Path projectRelativePath = locationPath.subpath(1, locationPath.getNameCount());
                return Paths.get(locationURI).resolve(projectRelativePath);
            }
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) URI(java.net.URI)

Aggregations

IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)172 IProject (org.eclipse.core.resources.IProject)69 IPath (org.eclipse.core.runtime.IPath)60 IResource (org.eclipse.core.resources.IResource)57 IFile (org.eclipse.core.resources.IFile)53 CoreException (org.eclipse.core.runtime.CoreException)50 IWorkspace (org.eclipse.core.resources.IWorkspace)34 File (java.io.File)30 Path (org.eclipse.core.runtime.Path)29 IContainer (org.eclipse.core.resources.IContainer)26 URI (java.net.URI)18 IFolder (org.eclipse.core.resources.IFolder)17 IOException (java.io.IOException)15 IProjectDescription (org.eclipse.core.resources.IProjectDescription)13 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 IStatus (org.eclipse.core.runtime.IStatus)11 IJavaProject (org.eclipse.jdt.core.IJavaProject)11 Location (ch.acanda.eclipse.pmd.domain.Location)10