Java Code Examples for org.eclipse.core.resources.IProjectDescription#setLocation()

The following examples show how to use org.eclipse.core.resources.IProjectDescription#setLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public static void importProject(String projectName, IPath directory)
    throws CoreException {
  IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
      projectName);
  if (!project.exists()) {
    IPath path = directory.append(IProjectDescription.DESCRIPTION_FILE_NAME);
    IProjectDescription projectFile = ResourcesPlugin.getWorkspace().loadProjectDescription(
        path);
    IProjectDescription projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(
        projectName);
    projectDescription.setLocation(path);

    project.create(projectFile, null);
  }

  project.open(null);
  JobsUtilities.waitForIdle();
}
 
Example 2
Source File: ResourceUtils.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
public static IProject createAndOpenProject(String name, IPath location, boolean overwrite, IProgressMonitor pm)
		throws CoreException {
	IProject project = EclipseUtils.getWorkspaceRoot().getProject(name);
	if(overwrite && project.exists()) {
		project.delete(true, pm);
	}
	
	IProjectDescription projectDesc = project.getWorkspace().newProjectDescription(project.getName());
	if(location != null) {
		IPath workspaceLocation = project.getWorkspace().getRoot().getLocation();
		if(location.equals(workspaceLocation.append(project.getName()))) {
			// Location is the default project location, so don't set it in description, this causes problems.
			// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=481508
		} else {
			projectDesc.setLocation(location);
		}
	}
	project.create(projectDesc, pm);
	
	project.open(pm);
	return project;
}
 
Example 3
Source File: CompileTests.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
@BeforeClass
public static void importStdLibIntoWorkspace() {
	try {
		String canonicalPath = new File(RELATIVE_PATH_TO_STDLIB).getCanonicalPath();
		IProjectDescription desc = ResourcesPlugin.getWorkspace()
				.loadProjectDescription(new Path(canonicalPath + "/.project"));
		desc.setLocation(new Path(canonicalPath));
		stdLibProject = ResourcesPlugin.getWorkspace().getRoot().getProject(desc.getName());
		if (!stdLibProject.exists()) {
			stdLibProject.create(desc, null);
		}
		stdLibProject.open(null);
	} catch (Exception e) {
		Logger.sys.error("Couldn't import stdlib project into workspace", e);
	}
}
 
Example 4
Source File: NewProjectCreatorTool.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private static IProject importProject(String projectName, String directory) throws Exception {

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IPath path = new Path(directory).append(IProjectDescription.DESCRIPTION_FILE_NAME);

    try {
      IProjectDescription projectFile = workspace.loadProjectDescription(path);
      IProjectDescription projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(
          projectName);
      projectDescription.setLocation(path);

      IProject project = workspace.getRoot().getProject(projectName);
      project.create(projectFile, null);
      project.open(null);

      GWTNature.addNatureToProject(project);

      return project;

    } catch (CoreException e) {
      GWTPluginLog.logError(e);
      throw new Exception("Could not import new GWT project");
    }
  }
 
Example 5
Source File: CreateProjectOperation.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
protected void createProject ( final IProgressMonitor monitor ) throws CoreException
{
    monitor.beginTask ( "Create project", 2 );

    final IProject project = this.info.getProject ();

    final IProjectDescription desc = project.getWorkspace ().newProjectDescription ( project.getName () );
    desc.setLocation ( this.info.getProjectLocation () );
    desc.setNatureIds ( new String[] { Constants.PROJECT_NATURE_CONFIGURATION, PROJECT_NATURE_JS } );

    final ICommand jsCmd = desc.newCommand ();
    jsCmd.setBuilderName ( BUILDER_JS_VALIDATOR );

    final ICommand localBuilder = desc.newCommand ();
    localBuilder.setBuilderName ( Constants.PROJECT_BUILDER );

    desc.setBuildSpec ( new ICommand[] { jsCmd, localBuilder } );

    if ( !project.exists () )
    {
        project.create ( desc, new SubProgressMonitor ( monitor, 1 ) );
        project.open ( new SubProgressMonitor ( monitor, 1 ) );
    }
    monitor.done ();
}
 
Example 6
Source File: AbstractGWTPluginTestCase.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private void importProject(String projectName, IPath directory) throws CoreException {
  IProject project = Util.getWorkspaceRoot().getProject(projectName);
  if (!project.exists()) {
    log("Importing project " + projectName);

    IPath path = directory.append(IProjectDescription.DESCRIPTION_FILE_NAME);
    IProjectDescription projectFile = ResourcesPlugin.getWorkspace().loadProjectDescription(path);
    IProjectDescription projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);
    projectDescription.setLocation(path);

    project.create(projectFile, null);
  } else {
    log("Project " + projectName + " already exists");
  }

  project.open(null);
  JobsUtilities.waitForIdle();
}
 
Example 7
Source File: ResourceHelper.java    From tlaplus with MIT License 6 votes vote down vote up
/**
* Renames and moves the project, but does not delete the old project. It's
* the callee's reponsibility.
* 
* @param project
* @param aNewName
*/
  public static IProject projectRename(final IProject project, final String aNewName, final IProgressMonitor aMonitor)
  {
      try
      {
      	// move the project location to the new location and name
          final IProjectDescription description = project.getDescription();
          final IPath basePath = description.getLocation().removeLastSegments(1).removeTrailingSeparator();
	final IPath newPath = basePath.append(aNewName.concat(TOOLBOX_DIRECTORY_SUFFIX)).addTrailingSeparator();
          description.setLocation(newPath);
          description.setName(aNewName);

          // refresh the project prior to moving to make sure the fs and resource fw are in sync
      	project.refreshLocal(IResource.DEPTH_INFINITE, aMonitor);
          
      	project.copy(description, IResource.NONE | IResource.SHALLOW, aMonitor);
          
          return ResourcesPlugin.getWorkspace().getRoot().getProject(aNewName);
      } catch (CoreException e)
      {
          Activator.getDefault().logError("Error renaming a specification", e);
      }
      return null;
  }
 
Example 8
Source File: MavenProjectImporterTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testPreexistingIProjectDifferentName() throws Exception {
	File from = new File(getSourceProjectDirectory(), "maven/salut");
	Path projectDir = Files.createTempDirectory("testImportDifferentName");

	IWorkspaceRoot wsRoot = WorkspaceHelper.getWorkspaceRoot();
	IWorkspace workspace = wsRoot.getWorkspace();
	String projectName = projectDir.getFileName().toString();
	IProject salut = wsRoot.getProject("salut");
	salut.delete(true, monitor);
	IProjectDescription description = workspace.newProjectDescription(projectName);
	description.setLocation(new org.eclipse.core.runtime.Path(projectDir.toFile().getAbsolutePath()));

	IProject project = wsRoot.getProject(projectName);
	project.create(description, monitor);


	assertTrue(WorkspaceHelper.getAllProjects().contains(project));

	FileUtils.copyDirectory(from, projectDir.toFile());

	assertTrue(project.exists());
	Job updateJob = projectsManager.updateWorkspaceFolders(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.toString())), Collections.emptySet());
	updateJob.join(20000, monitor);
	assertTrue("Failed to import preexistingProjectTest:\n" + updateJob.getResult().getException(), updateJob.getResult().isOK());
}
 
Example 9
Source File: TexlipseProjectCreationOperation.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create the project directory.
 * If the user has specified an external project location,
 * the project is created with a custom description for the location.
 * 
 * @param project project
 * @param monitor progress monitor
 * @throws CoreException
 */
private void createProject(IProject project, IProgressMonitor monitor)
        throws CoreException {

    monitor.subTask(TexlipsePlugin.getResourceString("projectWizardProgressDirectory"));
    
    if (!project.exists()) {
        if (attributes.getProjectLocation() != null) {
            IProjectDescription desc = project.getWorkspace().newProjectDescription(project.getName());
            IPath projectPath = new Path(attributes.getProjectLocation());
            IStatus stat = ResourcesPlugin.getWorkspace().validateProjectLocation(project, projectPath);
            if (stat.getSeverity() != IStatus.OK) {
                // should not happen. the location should have been checked in the wizard page
                throw new CoreException(stat);
            }
            desc.setLocation(projectPath);
            project.create(desc, monitor);
        } else {
            project.create(monitor);
        }
    }
    if (!project.isOpen()) {
        project.open(monitor);
    }
}
 
Example 10
Source File: ProjectFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected IProjectDescription createProjectDescription() {
	final IProjectDescription projectDescription = workspace.newProjectDescription(projectName);
	if (location != null && !Platform.getLocation().equals(location.removeLastSegments(1))) {
		projectDescription.setLocation(location);
	}

	if (referencedProjects != null && referencedProjects.size() != 0) {
		projectDescription
				.setReferencedProjects(referencedProjects.toArray(new IProject[referencedProjects.size()]));
	}
	if (projectNatures != null)
		projectDescription.setNatureIds(projectNatures.toArray(new String[projectNatures.size()]));
	if (builderIds != null)
		setBuilder(projectDescription, builderIds.toArray(new String[builderIds.size()]));
	return projectDescription;
}
 
Example 11
Source File: EclipseFileSystemTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetURIForImportedProject() {
  try {
    final IWorkspace ws = ResourcesPlugin.getWorkspace();
    final IWorkspaceRoot root = ws.getRoot();
    final IProjectDescription description = ws.newProjectDescription("bar");
    description.setLocation(root.getLocation().append("foo/bar"));
    final IProject project = root.getProject("bar");
    project.create(description, null);
    project.open(null);
    final Path file = new Path("/bar/Foo.text");
    Assert.assertFalse(this.fs.exists(file));
    Assert.assertNull(this.fs.toURI(file));
    try {
      this.fs.setContents(file, "Hello Foo");
      Assert.fail();
    } catch (final Throwable _t) {
      if (_t instanceof IllegalArgumentException) {
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
    Assert.assertFalse(this.fs.exists(file));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 12
Source File: ProjectCreator.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static IProject createProjectOnLocation(IPath baseDirectory, String projectName) throws CoreException {
	IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);
	description.setLocation(baseDirectory);
	IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());
	if (!project.exists()) {
		project.create(description, new NullProgressMonitor());
	}
	return project;
}
 
Example 13
Source File: PyStructureConfigHelpers.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new project resource with the entered name.
 * 
 * @param projectName The name of the project
 * @param projectLocationPath the location for the project. If null, the default location (in the workspace)
 * will be used to create the project.
 * @param references The projects that should be referenced from the newly created project
 * 
 * @return the created project resource, or <code>null</code> if the project was not created
 * @throws CoreException 
 * @throws OperationCanceledException 
 */
public static IProject createPydevProject(String projectName, IPath projectLocationPath, IProject[] references,

        IProgressMonitor monitor, String projectType, String projectInterpreter,
        ICallback<List<IContainer>, IProject> getSourceFolderHandlesCallback,
        ICallback<List<String>, IProject> getExternalSourceFolderHandlesCallback,
        ICallback<List<IPath>, IProject> getExistingSourceFolderHandlesCallback,
        ICallback<Map<String, String>, IProject> getVariableSubstitutionCallback)
        throws OperationCanceledException, CoreException {

    // get a project handle
    final IProject projectHandle = getProjectHandle(projectName);

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IProjectDescription description = workspace.newProjectDescription(projectHandle.getName());
    description.setLocation(projectLocationPath);

    // update the referenced project if provided
    if (references != null && references.length > 0) {
        description.setReferencedProjects(references);
    }

    createPydevProject(description, projectHandle, monitor, projectType, projectInterpreter,
            getSourceFolderHandlesCallback, getExternalSourceFolderHandlesCallback,
            getExistingSourceFolderHandlesCallback, getVariableSubstitutionCallback);
    return projectHandle;
}
 
Example 14
Source File: CargoTools.java    From corrosion with Eclipse Public License 2.0 5 votes vote down vote up
public static void ensureDotCargoImportedAsProject(IProgressMonitor monitor) throws CoreException {
	File cargoFolder = new File(System.getProperty("user.home") + "/.cargo"); //$NON-NLS-1$ //$NON-NLS-2$
	if (!cargoFolder.exists()) {
		return;
	}
	IWorkspace workspace = ResourcesPlugin.getWorkspace();
	for (IProject project : workspace.getRoot().getProjects()) {
		if (!project.isOpen() && project.getName().startsWith(".cargo")) { //$NON-NLS-1$
			project.open(monitor);
		}
		IPath location = project.getLocation();
		if (location != null) {
			File projectFolder = location.toFile().getAbsoluteFile();
			if (cargoFolder.getAbsolutePath().startsWith(projectFolder.getAbsolutePath())) {
				return; // .cargo already imported
			}
		}
	}
	// No .cargo folder available in workspace
	String projectName = ".cargo"; //$NON-NLS-1$
	while (workspace.getRoot().getProject(projectName).exists()) {
		projectName += '_';
	}
	IProjectDescription description = workspace.newProjectDescription(projectName);
	description.setLocation(Path.fromOSString(cargoFolder.getAbsolutePath()));
	IProject cargoProject = workspace.getRoot().getProject(projectName);
	cargoProject.create(description, monitor);
	cargoProject.open(monitor);
}
 
Example 15
Source File: ProjectHelper.java    From eclipse-extras with Eclipse Public License 1.0 4 votes vote down vote up
private IProjectDescription createProjectDescription() {
  IWorkspace workspace = ResourcesPlugin.getWorkspace();
  IProjectDescription result = workspace.newProjectDescription( projectName );
  result.setLocation( projectLocation );
  return result;
}
 
Example 16
Source File: ProjectsManager.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static IProject createJavaProject(IProject project, IPath projectLocation, String src, String bin, IProgressMonitor monitor) throws CoreException, OperationCanceledException {
	if (project.exists()) {
		return project;
	}
	JavaLanguageServerPlugin.logInfo("Creating the Java project " + project.getName());
	//Create project
	IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName());
	if (projectLocation != null) {
		description.setLocation(projectLocation);
	}
	project.create(description, monitor);
	project.open(monitor);

	//Turn into Java project
	description = project.getDescription();
	description.setNatureIds(new String[] { JavaCore.NATURE_ID });
	project.setDescription(description, monitor);

	IJavaProject javaProject = JavaCore.create(project);
	configureJVMSettings(javaProject);

	//Add build output folder
	if (StringUtils.isNotBlank(bin)) {
		IFolder output = project.getFolder(bin);
		if (!output.exists()) {
			output.create(true, true, monitor);
		}
		javaProject.setOutputLocation(output.getFullPath(), monitor);
	}

	List<IClasspathEntry> classpaths = new ArrayList<>();
	//Add source folder
	if (StringUtils.isNotBlank(src)) {
		IFolder source = project.getFolder(src);
		if (!source.exists()) {
			source.create(true, true, monitor);
		}
		IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(source);
		IClasspathEntry srcClasspath = JavaCore.newSourceEntry(root.getPath());
		classpaths.add(srcClasspath);
	}

	//Find default JVM
	IClasspathEntry jre = JavaRuntime.getDefaultJREContainerEntry();
	classpaths.add(jre);

	//Add JVM to project class path
	javaProject.setRawClasspath(classpaths.toArray(new IClasspathEntry[0]), monitor);

	JavaLanguageServerPlugin.logInfo("Finished creating the Java project " + project.getName());
	return project;
}
 
Example 17
Source File: ResourceUtil.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param projectPath
 *            the project location
 * @param natureIds
 *            the list of required natures
 * @param builderIds
 *            the list of required builders
 * @return a project description for the project that includes the list of required natures and builders
 */
public static IProjectDescription getProjectDescription(IPath projectPath, String[] natureIds, String[] builderIds)
{
	if (projectPath == null)
	{
		return null;
	}

	IProjectDescription description = null;
	IPath dotProjectPath = projectPath.append(IProjectDescription.DESCRIPTION_FILE_NAME);
	File dotProjectFile = dotProjectPath.toFile();
	IWorkspace workspace = ResourcesPlugin.getWorkspace();
	if (dotProjectFile.exists())
	{
		// loads description from the existing .project file
		try
		{
			description = workspace.loadProjectDescription(dotProjectPath);
			if (Platform.getLocation().isPrefixOf(projectPath))
			{
				description.setLocation(null);
			}
		}
		catch (CoreException e)
		{
			IdeLog.logWarning(CorePlugin.getDefault(), "Failed to load the existing .project file.", e); //$NON-NLS-1$
		}
	}
	if (description == null)
	{
		// creates a new project description
		description = workspace.newProjectDescription(projectPath.lastSegment());
		if (Platform.getLocation().isPrefixOf(projectPath))
		{
			description.setLocation(null);
		}
		else
		{
			description.setLocation(projectPath);
		}
	}

	// adds the required natures to the project description
	if (!ArrayUtil.isEmpty(natureIds))
	{
		Set<String> natures = CollectionsUtil.newInOrderSet(natureIds);
		CollectionsUtil.addToSet(natures, description.getNatureIds());
		description.setNatureIds(natures.toArray(new String[natures.size()]));
	}

	// adds the required builders to the project description
	if (!ArrayUtil.isEmpty(builderIds))
	{
		ICommand[] existingBuilders = description.getBuildSpec();
		List<ICommand> builders = CollectionsUtil.newList(existingBuilders);
		for (String builderId : builderIds)
		{
			if (!hasBuilder(builderId, existingBuilders))
			{
				ICommand newBuilder = description.newCommand();
				newBuilder.setBuilderName(builderId);
				builders.add(newBuilder);
			}
		}
		description.setBuildSpec(builders.toArray(new ICommand[builders.size()]));
	}

	return description;
}
 
Example 18
Source File: CheckoutCommand.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private void setProjectToRoot(final IProject project, File destPath) throws CoreException {
	IProjectDescription description = project.getDescription();
	description.setLocation(new Path(destPath.getAbsolutePath()));
	project.move(description, true, null);
}
 
Example 19
Source File: ExternalFoldersManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void createExternalFoldersProject(IProject project, IProgressMonitor monitor) throws CoreException {
	IProjectDescription desc = project.getWorkspace().newProjectDescription(project.getName());
	IPath stateLocation = JavaCore.getPlugin().getStateLocation();
	desc.setLocation(stateLocation.append(EXTERNAL_PROJECT_NAME));
	project.create(desc, IResource.HIDDEN, monitor);
}
 
Example 20
Source File: SampleProject.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
public void moveToLocation(Location packageLocation) throws CoreException {
	IProjectDescription description = project.getDescription();
	description.setLocation(ResourceUtils.epath(packageLocation));
	project.move(description, false, null);
}