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

The following examples show how to use org.eclipse.core.resources.IProject#getFolder() . 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: ExternalResourceManager.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
private static final IStatus linkSdkDirectory(IProject p, IProgressMonitor monitor) throws CoreException {
	monitor.beginTask(Messages.ExternalResourceManager_LinkingSdkFilesToResources, 1);
	
	IStatus status = Status.OK_STATUS;
	
	recreateExternalsFolder(p.getName(), monitor);
	IFolder externalsFolder = p.getFolder(SpecialFolderNames.VIRTUAL_MOUNT_ROOT_DIR_NAME);
	
	try{
		File libDefFile = getLibraryDefinitionsPath(p);
		if (libDefFile != null){
			status = linkDirectory(p, externalsFolder, libDefFile, monitor, false);
		}
	}
	finally{
		monitor.done();
	}
	
	return status;
}
 
Example 2
Source File: BirtWizardUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns IFolder object
 * 
 * @param project
 * @param dir
 * @return
 * @throws CoreException
 */
public static IFolder getFolder( IProject project, String dest )
		throws CoreException
{
	if ( project == null )
		return null;

	// find destination folder
	IFolder folder;
	if ( dest == null || dest.length( ) <= 0 )
	{
		dest = ""; //$NON-NLS-1$
	}

	// if folder doesn't exist, try to create it.
	folder = project.getFolder( dest );
	if ( !folder.exists( ) )
	{
		folder.create( true, true, null );
	}

	return folder;
}
 
Example 3
Source File: ScopedPreferences.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the contents of the configuration file to be used or null.
 */
private static IFile getProjectConfigFile(IProject project, String filename, boolean createPath) {
    try {
        if (project != null && project.exists()) {
            IFolder folder = project.getFolder(".settings");
            if (createPath) {
                if (!folder.exists()) {
                    folder.create(true, true, new NullProgressMonitor());
                }
            }
            return folder.getFile(filename);
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
 
Example 4
Source File: JavaClasspathTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testJavaSourceLevelMismatch() throws Exception {
	IProject project = testHelper.getProject();

	// create a fake java.util.List without type param
	IFolder folder = project.getFolder(new Path("src/java/util"));
	IResourcesSetupUtil.createFolder(folder.getFullPath());
	IFile list = folder.getFile("List.xtend");
	list.create(new StringInputStream("package java.util; class List {}"), true, null);

	IFile file = project.getFile("src/Foo.xtend");
	if (!file.exists())
		file.create(new StringInputStream(TEST_CLAZZ),
				true, null);
	IResourcesSetupUtil.cleanBuild();
	IResourcesSetupUtil.waitForBuild();
	markerAssert.assertErrorMarker(file, IssueCodes.JDK_NOT_ON_CLASSPATH);

	list.delete(true, null);
	IResourcesSetupUtil.cleanBuild();
	IResourcesSetupUtil.waitForBuild();
	markerAssert.assertNoErrorMarker(file);
}
 
Example 5
Source File: AbstractWorkbenchTestCase.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a source folder and configures the project to use it and the junit.jar
 *
 * @param addNature if false, no nature will be initially added to the project (if true, the nature will be added)
 */
protected IFolder createSourceFolder(IProgressMonitor monitor, IProject project, boolean addNature,
        boolean isJython)
        throws CoreException {
    IFolder sourceFolder = project.getFolder(new Path("src"));
    if (!sourceFolder.exists()) {
        sourceFolder.create(true, true, monitor);
    }
    if (addNature) {
        String name = project.getName();
        if (isJython) {
            PythonNature.addNature(project, monitor, PythonNature.JYTHON_VERSION_2_5, "/" + name +
                    "/src|/" + name
                    +
                    "/grinder.jar", null, null, null);
        } else {
            PythonNature.addNature(project, monitor, PythonNature.PYTHON_VERSION_2_6, "/" + name +
                    "/src", null,
                    null, null);
        }
    }
    return sourceFolder;
}
 
Example 6
Source File: CheckProjectHelper.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets file handle for the given project (initialized if it doesn't exist).
 *
 * @param project
 *          the project
 * @param filename
 *          the filename
 * @return the existing file
 * @throws CoreException
 *           the core exception
 */
public IFile getHelpFile(final IProject project, final String filename) throws CoreException {
  final List<String> splittedPath = Lists.newArrayList(Splitter.on('/').split(filename));
  for (final String seg : splittedPath) {
    if (splittedPath.indexOf(seg) != splittedPath.size() - 1) {
      IFolder folder = project.getFolder(seg);
      if (!folder.exists()) {
        folder.create(true, true, null);
      }
    }
  }
  IFile file = project.getFile(filename);
  if (!file.exists()) {
    file.create(new ByteArrayInputStream("".getBytes()), true, null);
  }
  return file;
}
 
Example 7
Source File: StandardFacetInstallationTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testStandardFacetInstallation_doesNotOverwriteWebXml()
    throws CoreException, IOException {
  // Create an empty web.xml.
  IProject project = projectCreator.getProject();
  IFolder folder = project.getFolder(new Path("src/main/webapp/WEB-INF"));
  ResourceUtils.createFolders(folder, null);
  IFile webXml = project.getFile("src/main/webapp/WEB-INF/web.xml");
  webXml.create(new ByteArrayInputStream(new byte[0]), true, null);
  assertEmptyFile(webXml);

  IFacetedProject facetedProject = projectCreator.getFacetedProject();
  AppEngineStandardFacet.installAppEngineFacet(facetedProject, true, null);
  ProjectUtils.waitForProjects(project); // App Engine runtime is added via a Job, so wait.

  assertEmptyFile(webXml);
}
 
Example 8
Source File: ProjectUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static IProject createInvisibleProjectIfNotExist(IPath workspaceRoot) throws OperationCanceledException, CoreException {
	String invisibleProjectName = ProjectUtils.getWorkspaceInvisibleProjectName(workspaceRoot);
	IProject invisibleProject = ResourcesPlugin.getWorkspace().getRoot().getProject(invisibleProjectName);
	if (!invisibleProject.exists()) {
		ProjectsManager.createJavaProject(invisibleProject, null, null, "bin", null);
		// Link the workspace root to the invisible project.
		IFolder workspaceLinkFolder = invisibleProject.getFolder(ProjectUtils.WORKSPACE_LINK);
		if (!workspaceLinkFolder.isLinked()) {
			try {
				JDTUtils.createFolders(workspaceLinkFolder.getParent(), null);
				workspaceLinkFolder.createLink(workspaceRoot.toFile().toURI(), IResource.REPLACE, null);
			} catch (CoreException e) {
				invisibleProject.delete(true, null);
				throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID,
						Messages.format("Failed to create linked resource from ''{0}'' to the invisible project ''{1}''.", new String[] { workspaceRoot.toString(), invisibleProjectName }), e));
			}
		}
	}

	return invisibleProject;
}
 
Example 9
Source File: FlexExistingDeployArtifactStagingDelegateTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws  CoreException {
  IProject project = projectCreator.getProject();
  stagingDirectory = project.getFolder("stagingDirectory").getLocation();

  IFolder appEngineFolder = project.getFolder("appEngineDirectory");
  appEngineFolder.create(true, true, null);
  String appYaml = "runtime: java\n" + 
      "env: flex\n";
  appEngineFolder.getFile("app.yaml")
      .create(new ByteArrayInputStream(appYaml.getBytes(Charsets.UTF_8)), true, null);
  appEngineDirectory = appEngineFolder.getLocation();

}
 
Example 10
Source File: DialogUtils.java    From typescript.java with MIT License 5 votes vote down vote up
public static ElementTreeSelectionDialog createFolderDialog(String initialFolder, final IProject project,
		final boolean showAllProjects, final boolean showFolder, Shell shell) {

	ILabelProvider lp = new WorkbenchLabelProvider();
	ITreeContentProvider cp = new WorkbenchContentProvider();
	FolderSelectionDialog dialog = new FolderSelectionDialog(shell, lp, cp);
	// dialog.setTitle(TypeScriptUIMessages.TernModuleOptionsPanel_selectPathDialogTitle);
	IContainer folder = StringUtils.isEmpty(initialFolder) ? project
			: (project != null ? project.getFolder(initialFolder)
					: ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(initialFolder)));
	if (folder != null && folder.exists()) {
		dialog.setInitialSelection(folder);
	}
	dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
	ViewerFilter filter = new ViewerFilter() {

		@Override
		public boolean select(Viewer viewer, Object parentElement, Object element) {
			if (element instanceof IProject) {
				if (showAllProjects)
					return true;
				IProject p = (IProject) element;
				return (p.equals(project));
			} else if (element instanceof IContainer) {
				IContainer container = (IContainer) element;
				if (showFolder && container.getType() == IResource.FOLDER) {
					return true;
				}
				return false;
			}
			return false;
		}
	};
	dialog.addFilter(filter);
	return dialog;
}
 
Example 11
Source File: SpecialFolderNames.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
public static Set<String> getSpecialFolderRelativePathes(IProject p) {
    if (!NatureUtils.hasNature(p, NatureIdRegistry.MODULA2_SOURCE_PROJECT_NATURE_ID)) {
        return Collections.emptySet();
    }
    
    Set<String> pathes = new HashSet<>();
    for (String name : SPECIAL_FOLDER_NAMES) {
        IFolder f = p.getFolder(name);
        if (f != null) {
            pathes.add(f.getProjectRelativePath().toPortableString());
        }
    }
    
    return pathes;
}
 
Example 12
Source File: CodeTemplates.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private static void createWebXml(IProject project, AppEngineProjectConfig config,
    boolean isStandardProject, IProgressMonitor monitor) throws CoreException {
  Map<String, String> properties = new HashMap<>();

  String packageValue = config.getPackageName().isEmpty()
      ? ""  //$NON-NLS-1$
      : config.getPackageName() + "."; //$NON-NLS-1$
  properties.put("package", packageValue); //$NON-NLS-1$

  if (isServlet25Selected(config)) {
    if (isObjectifySelected(config)) {
      properties.put("objectifyAdded", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    properties.put("servletVersion", "2.5"); //$NON-NLS-1$ //$NON-NLS-2$
    properties.put("namespace", "http://java.sun.com/xml/ns/javaee"); //$NON-NLS-1$ //$NON-NLS-2$
    properties.put("schemaUrl", "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"); //$NON-NLS-1$ //$NON-NLS-2$
  } else {
    properties.put("servletVersion", "3.1"); //$NON-NLS-1$ //$NON-NLS-2$
    properties.put("namespace", "http://xmlns.jcp.org/xml/ns/javaee"); //$NON-NLS-1$ //$NON-NLS-2$
    properties.put("schemaUrl", "http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"); //$NON-NLS-1$ //$NON-NLS-2$
  }

  IFolder webInf = project.getFolder("src/main/webapp/WEB-INF"); //$NON-NLS-1$
  createChildFile("web.xml", Templates.WEB_XML_TEMPLATE, webInf, //$NON-NLS-1$
      properties, monitor);
}
 
Example 13
Source File: SARLProjectConfigurator.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static IFolder ensureOutputFolder(IProject project, String folderPath, boolean isIFolderRequired,
		boolean createFolder, IProgressMonitor monitor) throws CoreException {
	final IFolder folder = project.getFolder(Path.fromPortableString(folderPath));
	if (!folder.exists()) {
		if (createFolder) {
			CoreUtility.createFolder(folder, true, true, monitor);
		} else if (!isIFolderRequired) {
			monitor.done();
			return null;
		}
	}
	setDerived(folder);
	monitor.done();
	return folder;
}
 
Example 14
Source File: AppEngineConfigurationUtil.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to resolve the given file within the project's {@code WEB-INF}. Note that this method
 * may return a file that is in a build location (e.g., {@code
 * target/m2e-wtp/web-resources/WEB-INF}) which may be frequently removed or regenerated.
 *
 * @return the file location or {@code null} if not found
 */
public static IFile findConfigurationFile(IProject project, IPath relativePath) {
  IFolder appengineFolder = project.getFolder(DEFAULT_CONFIGURATION_FILE_LOCATION);
  if (appengineFolder != null && appengineFolder.exists()) {
    IFile destination = appengineFolder.getFile(relativePath);
    if (destination.exists()) {
      return destination;
    }
  }
  return WebProjectUtil.findInWebInf(project, relativePath);
}
 
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: AppEngineConfigurationUtilTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateConfigurationFile_withAppEngineDir() throws CoreException, IOException {
  IProject project = projectCreator.getProject();
  IFolder srcMainAppengine = project.getFolder(new Path("src/main/appengine"));
  ResourceUtils.createFolders(srcMainAppengine, null);
  IFile file =
      AppEngineConfigurationUtil.createConfigurationFile(
          project, new Path("index.yaml"), ByteSource.empty().openStream(), true, null);
  assertEquals(new Path("src/main/appengine/index.yaml"), file.getProjectRelativePath());
}
 
Example 17
Source File: AbstractBuilderParticipantTest.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/***/
protected IFolder createFolder(IProject project, String path) throws CoreException {
	IFolder folder = project.getFolder(path);
	if (!folder.exists()) {
		createParentFolder(folder);
		folder.create(true, true, null);
	}
	return folder;
}
 
Example 18
Source File: TmfProjectElement.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
static IFolder createFolderStructure(IProject project, IProject shadowProject) throws CoreException {
    if (shadowProject != null) {
        createFolderStructure(shadowProject);
    }
    IFolder parentFolder = project.getFolder(TRACECOMPASS_PROJECT_FILE);
    createFolder(parentFolder);
    return parentFolder;
}
 
Example 19
Source File: PythonSourceFolderWizard.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException {
    IProject project = filePage.getValidatedProject();
    String name = filePage.getValidatedName();
    if (project == null || !project.exists()) {
        throw new RuntimeException("The project selected does not exist in the workspace.");
    }
    IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
    if (pathNature == null) {
        IPythonNature nature = PythonNature.addNature(project, monitor, null, null, null, null, null);
        pathNature = nature.getPythonPathNature();
        if (pathNature == null) {
            throw new RuntimeException("Unable to add the nature to the seleted project.");
        }
    }
    IFolder folder = project.getFolder(name);
    if (folder.exists()) {
        Log.log("Source folder already exists. Nothing new was created");
        return null;
    }
    folder.create(true, true, monitor);
    String newPath = folder.getFullPath().toString();

    String curr = pathNature.getProjectSourcePath(false);
    if (curr == null) {
        curr = "";
    }
    if (curr.endsWith("|")) {
        curr = curr.substring(0, curr.length() - 1);
    }
    String newPathRel = PyStructureConfigHelpers.convertToProjectRelativePath(
            project.getFullPath().toString(), newPath);
    if (curr.length() > 0) {
        //there is already some path
        Set<String> projectSourcePathSet = pathNature.getProjectSourcePathSet(true);
        if (!projectSourcePathSet.contains(newPath)) {
            //only add to the path if it doesn't already contain the new path
            curr += "|" + newPathRel;
        }
    } else {
        //there is still no other path
        curr = newPathRel;
    }
    pathNature.setProjectSourcePath(curr);
    PythonNature.getPythonNature(project).rebuildPath();
    return null;
}
 
Example 20
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;
}