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
}
}
});
}
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();
}
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());
}
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());
}
}
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;
}
Aggregations