Java Code Examples for org.eclipse.core.resources.IProject#open()

The following examples show how to use org.eclipse.core.resources.IProject#open() . 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: PlantUmlExportTestUtils.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
public static IProject getModelsProject() throws IOException, CoreException {
	String projectPath = new File(TEST_MODEL_PATH).getCanonicalPath();
	IWorkspace workspace = ResourcesPlugin.getWorkspace();

	IProjectDescription description = workspace
			.loadProjectDescription(new Path(projectPath + Path.SEPARATOR + PROJECT_FILE));
	IProject genericProject = workspace.getRoot().getProject(description.getName());

	if (!genericProject.exists()) {
		genericProject.create(description, new NullProgressMonitor());
	}

	genericProject.open(new NullProgressMonitor());
	if (!genericProject.isOpen()) {
		throw new RuntimeException("Couldn't open project: " + genericProject.getName());
	}
	project = JavaCore.create(genericProject);
	genericProject.refreshLocal(IProject.DEPTH_INFINITE, new NullProgressMonitor());

	return genericProject;
}
 
Example 2
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 3
Source File: JReFrameworker.java    From JReFrameworker with MIT License 6 votes vote down vote up
private static IJavaProject createProject(String projectName, IPath projectPath, IProject project, IProgressMonitor monitor) throws CoreException {
	IProjectDescription projectDescription = project.getWorkspace().newProjectDescription(project.getName());
	URI location = getProjectLocation(projectName, projectPath);
	projectDescription.setLocationURI(location);
	
	// make this a JReFrameworker project
	projectDescription.setNatureIds(new String[] { JReFrameworkerNature.NATURE_ID, JavaCore.NATURE_ID });

	// build first with Java compiler then JReFramewoker bytecode operations
	BuildCommand javaBuildCommand = new BuildCommand();
	javaBuildCommand.setBuilderName(JavaCore.BUILDER_ID);
	BuildCommand jrefBuildCommand = new BuildCommand();
	jrefBuildCommand.setBuilderName(JReFrameworkerBuilder.BUILDER_ID);
	projectDescription.setBuildSpec(new ICommand[]{ javaBuildCommand, jrefBuildCommand});

	// create and open the Eclipse project
	project.create(projectDescription, null);
	IJavaProject jProject = JavaCore.create(project);
	project.open(new NullProgressMonitor());
	return jProject;
}
 
Example 4
Source File: CheckoutUsingProjectWizardAction.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
    * Creates a project and open it 
    */
private void createAndOpenProject(IProject project, IProjectDescription desc, IProgressMonitor monitor) throws SVNException {
	try {
		monitor.beginTask(null, 5);
		if (project.exists()) {
			if (desc != null) {
				project.move(desc, true, Policy.subMonitorFor(monitor, 3));
			}
		} else {
			if (desc == null) {
				// create in default location
				project.create(Policy.subMonitorFor(monitor, 3));
			} else {
				// create in some other location
				project.create(desc, Policy.subMonitorFor(monitor, 3));
			}
		}
		if (!project.isOpen()) {
			project.open(Policy.subMonitorFor(monitor, 2));
		}
	} catch (CoreException e) {
		throw SVNException.wrapException(e);
	} finally {
		monitor.done();
	}
}
 
Example 5
Source File: ExampleImporter.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public IProject importExample(ExampleData edata, IProgressMonitor monitor) {
	try {
		IProjectDescription original = ResourcesPlugin.getWorkspace()
				.loadProjectDescription(new Path(edata.getProjectDir().getAbsolutePath()).append("/.project"));
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(edata.getProjectDir().getName());

		IProjectDescription clone = ResourcesPlugin.getWorkspace().newProjectDescription(original.getName());
		clone.setBuildSpec(original.getBuildSpec());
		clone.setComment(original.getComment());
		clone.setDynamicReferences(original.getDynamicReferences());
		clone.setNatureIds(original.getNatureIds());
		clone.setReferencedProjects(original.getReferencedProjects());
		if (project.exists()) {
			return project;
		}
		project.create(clone, monitor);
		project.open(monitor);

		@SuppressWarnings("unchecked")
		List<IFile> filesToImport = FileSystemStructureProvider.INSTANCE.getChildren(edata.getProjectDir());
		ImportOperation io = new ImportOperation(project.getFullPath(), edata.getProjectDir(),
				FileSystemStructureProvider.INSTANCE, new IOverwriteQuery() {

					@Override
					public String queryOverwrite(String pathString) {
						return IOverwriteQuery.ALL;
					}

				}, filesToImport);
		io.setOverwriteResources(true);
		io.setCreateContainerStructure(false);
		io.run(monitor);
		project.refreshLocal(IProject.DEPTH_INFINITE, monitor);
		return project;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 6
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 7
Source File: ProjectTestUtil.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a simple project.
 *
 * @param projectName the name of the project
 * @param natureIds an array of natures IDs to set on the project, or {@code null} if none should
 *        be set
 * @return the created project
 * @throws CoreException if the project is not created
 */
public static IProject createSimpleProject(String projectName, String... natureIds)
    throws CoreException {
  IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
  if (project.exists()) {
    project.delete(true, true, npm());
  }
  project.create(npm());
  project.open(npm());
  if (natureIds != null) {
    IProjectDescription desc = project.getDescription();
    desc.setNatureIds(natureIds);
    project.setDescription(desc, npm());
  }
  return project;
}
 
Example 8
Source File: AbstractCorrosionTest.java    From corrosion with Eclipse Public License 2.0 6 votes vote down vote up
/**
 *
 * @param projectName
 *            the name that will be used as prefix for the project, and that
 *            will be used to find the content of the project from the plugin
 *            "projects" folder
 * @throws IOException
 * @throws CoreException
 */
protected IProject provisionProject(String projectName) throws IOException, CoreException {
	URL url = FileLocator.find(Platform.getBundle("org.eclipse.corrosion.tests"),
			Path.fromPortableString("projects/" + projectName), Collections.emptyMap());
	url = FileLocator.toFileURL(url);
	File folder = new File(url.getFile());
	if (folder.exists()) {
		IProject project = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(projectName + "_" + getClass().getName() + "_" + System.currentTimeMillis());
		project.create(new NullProgressMonitor());
		this.provisionedProjects.put(projectName, project);
		FileUtils.copyDirectory(folder, project.getLocation().toFile());
		project.open(new NullProgressMonitor());
		project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
		return project;
	} 			
	return null;
}
 
Example 9
Source File: ClientTemplate.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private void createFeatureProject ( final IProject project, final Map<String, String> properties, final IProgressMonitor monitor ) throws CoreException
{
    monitor.beginTask ( "Creating feature project", 6 );

    final String name = this.pluginId + ".feature"; //$NON-NLS-1$
    final IProjectDescription desc = project.getWorkspace ().newProjectDescription ( name );

    final ICommand featureBuilder = desc.newCommand ();
    featureBuilder.setBuilderName ( "org.eclipse.pde.FeatureBuilder" ); //$NON-NLS-1$

    desc.setNatureIds ( new String[] { "org.eclipse.pde.FeatureNature" } ); //$NON-NLS-1$
    desc.setBuildSpec ( new ICommand[] {
            featureBuilder
    } );

    final IProject newProject = project.getWorkspace ().getRoot ().getProject ( name );
    newProject.create ( desc, new SubProgressMonitor ( monitor, 1 ) ); // COUNT:1
    newProject.open ( new SubProgressMonitor ( monitor, 1 ) ); // COUNT:1

    addFilteredResource ( newProject, "pom.xml", getResource ( "feature-pom.xml" ), "UTF-8", properties, new SubProgressMonitor ( monitor, 1 ) ); // COUNT:1
    addFilteredResource ( newProject, "feature.xml", getResource ( "feature/feature.xml" ), "UTF-8", properties, new SubProgressMonitor ( monitor, 1 ) ); // COUNT:1
    addFilteredResource ( newProject, "feature.properties", getResource ( "feature/feature.properties" ), "ISO-8859-1", properties, new SubProgressMonitor ( monitor, 1 ) ); // COUNT:1
    addFilteredResource ( newProject, "build.properties", getResource ( "feature/build.properties" ), "ISO-8859-1", properties, new SubProgressMonitor ( monitor, 1 ) ); // COUNT:1

    monitor.done ();
}
 
Example 10
Source File: FileUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new project.
 * 
 * @param name
 *            the project name
 */
public static IProject createProject( String name ) throws CoreException
{
	IWorkspace ws = ResourcesPlugin.getWorkspace( );
	IWorkspaceRoot root = ws.getRoot( );
	IProject proj = root.getProject( name );
	if ( !proj.exists( ) )
		proj.create( null );
	if ( !proj.isOpen( ) )
		proj.open( null );
	return proj;
}
 
Example 11
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 12
Source File: InternalImpl.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void createProject(String projectName) throws RemoteException {

  log.trace("creating project: " + projectName);
  IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
  try {
    project.create(null);
    project.open(null);
  } catch (CoreException e) {
    log.error("unable to create project '" + projectName + "' : " + e.getMessage(), e);
    throw new RemoteException(e.getMessage(), e.getCause());
  }
}
 
Example 13
Source File: FullSourceWorkspaceModelTests.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
private IJavaProject createJavaProject(String name) throws CoreException {
  IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
  if (project.exists())
    project.delete(true, null);
  project.create(null);
  project.open(null);
  IProjectDescription description = project.getDescription();
  description.setNatureIds(new String[] { JavaCore.NATURE_ID });
  project.setDescription(description, null);
  IJavaProject javaProject = JavaCore.create(project);
  javaProject.setRawClasspath(new IClasspathEntry[] { JavaCore.newVariableEntry(new Path("JRE_LIB"), null, null) }, null);
  return javaProject;
}
 
Example 14
Source File: GamlUtils.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public static IProject createProject(final IProject project) throws CoreException {
	if (!project.exists()) {
		project.create(monitor());
	}
	project.open(monitor());
	return project;
}
 
Example 15
Source File: JavaProjectSetupUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public static IFolder createExternalFolder(String folderName) throws CoreException {
		IPath externalFolderPath = new Path(folderName);
		IProject externalFoldersProject = JavaModelManager.getExternalManager().getExternalFoldersProject();
		if (!externalFoldersProject.isAccessible()) {
			if (!externalFoldersProject.exists())
				externalFoldersProject.create(monitor());
			externalFoldersProject.open(monitor());
		}
		IFolder result = externalFoldersProject.getFolder(externalFolderPath);
		result.create(true, false, null);
//		JavaModelManager.getExternalManager().addFolder(result.getFullPath());
		return result;
	}
 
Example 16
Source File: TestHTML.java    From wildwebdeveloper with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFormat() throws Exception {
	final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("testHTMLFile" + System.currentTimeMillis());
	project.create(null);
	project.open(null);
	final IFile file = project.getFile("blah.html");
	file.create(new ByteArrayInputStream("<html><body><a></a></body></html>".getBytes()), true, null);
	ITextEditor editor = (ITextEditor) IDE
			.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
	editor.setFocus();
	editor.getSelectionProvider().setSelection(new TextSelection(0, 0));
	IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
	AtomicReference<Exception> ex = new AtomicReference<>();
	new DisplayHelper() {
		@Override protected boolean condition() {
			try {
				handlerService.executeCommand("org.eclipse.lsp4e.format", null);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
	}.waitForCondition(editor.getSite().getShell().getDisplay(), 3000);
	if (ex.get() != null) {
		throw ex.get();
	}
	new DisplayHelper() {
		@Override protected boolean condition() {
			return editor.getDocumentProvider().getDocument(editor.getEditorInput()).getNumberOfLines() > 1;
		}
	}.waitForCondition(editor.getSite().getShell().getDisplay(), 3000);
}
 
Example 17
Source File: TestUtils.java    From CogniCrypt with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * This method creates a empty JavaProject in the current workspace
 * 
 * @param projectName for the JavaProject
 * @return new created JavaProject
 * @throws CoreException
 */
public static IJavaProject createJavaProject(final String projectName) throws CoreException {

	final IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
	deleteProject(workSpaceRoot.getProject(projectName));

	final IProject project = workSpaceRoot.getProject(projectName);
	project.create(null);
	project.open(null);

	final IProjectDescription description = project.getDescription();
	description.setNatureIds(new String[] {JavaCore.NATURE_ID});
	project.setDescription(description, null);

	final IJavaProject javaProject = JavaCore.create(project);

	final IFolder binFolder = project.getFolder("bin");
	binFolder.create(false, true, null);
	javaProject.setOutputLocation(binFolder.getFullPath(), null);

	final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
	final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
	final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
	for (final LibraryLocation element : locations) {
		entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
	}
	// add libs to project class path
	javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

	final IFolder sourceFolder = project.getFolder("src");
	sourceFolder.create(false, true, null);

	final IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(sourceFolder);
	final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
	final IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
	System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
	newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageRoot.getPath());
	javaProject.setRawClasspath(newEntries, null);

	return javaProject;
}
 
Example 18
Source File: VibeModelTester.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * <p>
 * This operation sets up the workspace.
 * </p>
 */
@BeforeClass
public static void beforeTests() {

	// Local Declarations
	IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
	IProject project = null;
	String separator = System.getProperty("file.separator");
	String userDir = System.getProperty("user.home") + separator
			+ "ICETests" + separator + "caebatTesterWorkspace";
	// Enable Debugging
	System.setProperty("DebugICE", "");

	// Setup the project
	try {
		// Get the project handle
		IPath projectPath = new Path(userDir + separator + ".project");
		// Create the project description
		IProjectDescription desc = ResourcesPlugin.getWorkspace()
		                    .loadProjectDescription(projectPath);
		// Get the project handle and create it
		project = workspaceRoot.getProject(desc.getName());
		// Create the project if it doesn't exist
		if (!project.exists()) {
			project.create(desc, new NullProgressMonitor());
		}
		// Open the project if it is not already open
		if (project.exists() && !project.isOpen()) {
		   project.open(new NullProgressMonitor());
		}
		// Refresh the workspace
		project.refreshLocal(IResource.DEPTH_INFINITE, null);
	} catch (CoreException e) {
		// Catch exception for creating the project
		e.printStackTrace();
		fail("VIBE Model Tester: Error!  Could not set up project space");
	}

	// Set the global project reference.
	projectSpace = project;

	return;
}
 
Example 19
Source File: MockJavaProjectProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public static IJavaProject createJavaProject(
		final String projectName, 
		String[] projectNatures) {

	IProject project = null;
	IJavaProject javaProject = null;
	try {
		project = IResourcesSetupUtil.root().getProject(projectName);
		deleteProject(project);

		javaProject = JavaCore.create(project);
		IProjectDescription projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(
				projectName);
		project.create(projectDescription, null);
		List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
		projectDescription.setNatureIds(projectNatures);

		final ICommand java = projectDescription.newCommand();
		java.setBuilderName(JavaCore.BUILDER_ID);

		final ICommand manifest = projectDescription.newCommand();
		manifest.setBuilderName("org.eclipse.pde.ManifestBuilder");

		final ICommand schema = projectDescription.newCommand();
		schema.setBuilderName("org.eclipse.pde.SchemaBuilder");

		projectDescription.setBuildSpec(new ICommand[] { java, manifest, schema });

		project.open(null);
		project.setDescription(projectDescription, null);

		classpathEntries.add(JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins")));

		javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]),
				null);
		JavaProjectSetupUtil.addJreClasspathEntry(javaProject);
		
		makeJava8Compliant(javaProject);

		javaProject.setOutputLocation(new Path("/" + projectName + "/bin"), null);
		createManifest(projectName, project);

		refreshExternalArchives(javaProject);
		refresh(javaProject);
	}
	catch (final Exception exception) {
		throw new RuntimeException(exception);
	}
	return javaProject ;
}
 
Example 20
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;
}