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

The following examples show how to use org.eclipse.core.resources.IProject#getNature() . 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: SiriusServiceConfigurator.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the {@link ModelingProject} for the given {@link IProject}.
 * 
 * @param project
 *            the {@link IProject}
 * @return the {@link ModelingProject} for the given {@link IProject} if any, <code>null</code> otherwise
 */
private ModelingProject getModelingProject(IProject project) {
    ModelingProject modelingProject = null;
    try {
        for (String natureId : project.getDescription().getNatureIds()) {
            IProjectNature nature = project.getNature(natureId);
            if (nature instanceof ModelingProject) {
                modelingProject = (ModelingProject) nature;
                break;
            }
        }
    } catch (CoreException e) {
        /* does nothing */
    }
    return modelingProject;
}
 
Example 2
Source File: DocumentUimaImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the project class loader.
 *
 * @param project the project
 * @return the project class loader
 * @throws CoreException the core exception
 */
public static ClassLoader getProjectClassLoader(IProject project) throws CoreException {
  IProjectNature javaNature = project.getNature(JAVA_NATURE);
  if (javaNature != null) {
    JavaProject javaProject = (JavaProject) JavaCore.create(project);
    
    String[] runtimeClassPath = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
    List<URL> urls = new ArrayList<>();
    for (String cp : runtimeClassPath) {
      try {
        urls.add(Paths.get(cp).toUri().toURL());
      } catch (MalformedURLException e) {
        CasEditorPlugin.log(e);
      }
    }
    return new URLClassLoader(urls.toArray(new URL[0]));
  } 
  return null;
}
 
Example 3
Source File: DjangoNature.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param project the project we want to know about (if it is null, null is returned)
 * @return the django nature for a project (or null if it does not exist for the project)
 * 
 * @note: it's synchronized because more than 1 place could call getDjangoNature at the same time and more
 * than one nature ended up being created from project.getNature().
 */
public static DjangoNature getDjangoNature(IProject project) {
    if (project != null && project.isOpen()) {
        try {
            if (project.hasNature(DJANGO_NATURE_ID)) {
                synchronized (lockGetNature) {
                    IProjectNature n = project.getNature(DJANGO_NATURE_ID);
                    if (n instanceof DjangoNature) {
                        return (DjangoNature) n;
                    }
                }
            }
        } catch (CoreException e) {
            Log.logInfo(e);
        }
    }
    return null;
}
 
Example 4
Source File: SelectModulaSourceFileDialog.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Image getImage(Object element) {
    if (element instanceof ListItem) {
        IProject ip = ((ListItem)element).getProject();
        if (ip != null) {
            element = ip;
            try {
                if (ip.getNature(NatureIdRegistry.MODULA2_SOURCE_PROJECT_NATURE_ID) != null) {
                    // icon with transparent backgroung (prj. explorer needs white background in its icon)
                    return ImageUtils.getImage(ImageUtils.M2_PRJ_FOLDER_TRANSP);
                }
            } catch (Exception e){}
        }
        
    }
    return workbenchLabelProvider.getImage(element);
}
 
Example 5
Source File: XdsModel.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public XdsProject getXdsProjectBy(IProject p) {
    IProjectNature nature;
    try {
        nature = p.isOpen() ? p.getNature(NatureIdRegistry.MODULA2_SOURCE_PROJECT_NATURE_ID) : null;
        if (nature == null){
            return null;
        }
    } catch (CoreException e) {
        LogHelper.logError(e);
        return null;
    }
    Lock readLock = instanceLock.readLock();
    try{
    	readLock.lock();
    	return name2XdsProject.get(p.getName());
    }
    finally {
    	readLock.unlock();
    }
}
 
Example 6
Source File: JavaUtils.java    From developer-studio with Apache License 2.0 6 votes vote down vote up
public static IJavaProject addJavaNature(IProject project, boolean rawClasspath) throws CoreException, JavaModelException {
 
	 IProjectNature nature = project.getNature(ProjectConstants.JAVA_NATURE_ID);
	 if(nature==null){
	    ProjectUtils.addNatureToProject(project, true, ProjectConstants.JAVA_NATURE_ID);
	 }
	IJavaProject javaProject = JavaCore.create(project);
	IFolder targetFolder = project.getFolder("target");
	targetFolder.create(false, true, null);
	javaProject.setOutputLocation(targetFolder.getFullPath(), null);
	Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
	if(rawClasspath){
		entries.addAll(Arrays.asList(getClasspathEntries(javaProject)));
	}
	if(nature==null){
		entries.add(JavaRuntime.getDefaultJREContainerEntry());
	 }
	javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
	return javaProject;
}
 
Example 7
Source File: ActionUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isOnBuildPath(IJavaElement element) {
       //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
       if (element.getElementType() == IJavaElement.JAVA_PROJECT)
           return true;
	IJavaProject project= element.getJavaProject();
	try {
		if (!project.isOnClasspath(element))
			return false;
		IProject resourceProject= project.getProject();
		if (resourceProject == null)
			return false;
		IProjectNature nature= resourceProject.getNature(JavaCore.NATURE_ID);
		// We have a Java project
		if (nature != null)
			return true;
	} catch (CoreException e) {
	}
	return false;
}
 
Example 8
Source File: WorkbenchUtils.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isXdsProject(IProject prj, String projectNatureId) {
    try {
        return prj != null && prj.isOpen() && prj.getNature(projectNatureId) != null;
    } catch (CoreException e) {
        LogHelper.logError(e);
    }
    return false;
}
 
Example 9
Source File: JavaProjectModulesManagerCreator.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method will check the given project and if it's a java project, will create a project modules manager
 * that can be used to get things from it as we need in pydev.
 */
public static IModulesManager createJavaProjectModulesManagerIfPossible(IProject project) {
    if (JDTSupported == false) {
        return null;
    }

    try {
        if (project.isOpen()) {
            IProjectNature nature = project.getNature(JavaCore.NATURE_ID);
            if (nature instanceof IJavaProject) {
                IJavaProject javaProject = (IJavaProject) nature;
                return new JavaProjectModulesManager(javaProject);
            }
        }
    } catch (Throwable e) {
        if (JythonModulesManagerUtils.isOptionalJDTClassNotFound(e)) {
            //ignore it at this point: we don't have JDT... set the static variable to it and don't even
            //try to get to this point again (no need to log it or anything).
            JDTSupported = false;
            return null;
        } else {
            Log.log(e);
        }
    }

    return null;
}
 
Example 10
Source File: RadlNewWizardPage.java    From RADL with Apache License 2.0 5 votes vote down vote up
private boolean isJavaProject(IProject project) {
  try {
    return !project.isHidden() && project.isOpen() && project.getNature("org.eclipse.jdt.core.javanature") != null;
  } catch (CoreException e) {
    return false;
  }
}
 
Example 11
Source File: TraceUtils.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Get the opened (accessible) projects with Tmf nature
 *
 * @return the Tmf projects
 */
public static List<IProject> getOpenedTmfProjects() {
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    List<IProject> tmfProjects = new ArrayList<>();
    for (IProject project : projects) {
        try {
            if (project.isAccessible() && project.getNature(TmfProjectNature.ID) != null && !TmfProjectModelHelper.isShadowProject(project)) {
                tmfProjects.add(project);
            }
        } catch (CoreException e) {
            Activator.getDefault().logError("Error getting opened tmf projects", e); //$NON-NLS-1$
        }
    }
    return tmfProjects;
}
 
Example 12
Source File: LauncherTabSettings.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
private IProject getXdsProject(ILaunchConfiguration config) {
    try {
        String prjname = config.getAttribute(ILaunchConfigConst.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
        if (prjname.length() > 0) {
            IProject ip = ResourcesPlugin.getWorkspace().getRoot().getProject(prjname);
            if (ip.exists() && ip.getNature(NatureIdRegistry.MODULA2_SOURCE_PROJECT_NATURE_ID) != null) {
                return ip;
            }
        }
    } catch (CoreException e) {}
    return null;
}
 
Example 13
Source File: LauncherTabArguments.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
private IProject getXdsProject(ILaunchConfiguration config) {
	try {
		String prjname = config.getAttribute(ILaunchConfigConst.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
		if (prjname.length() > 0) {
			IProject ip = ResourcesPlugin.getWorkspace().getRoot().getProject(prjname);
			if (ip.exists() && ip.getNature(NatureIdRegistry.MODULA2_SOURCE_PROJECT_NATURE_ID) != null) {
				return ip;
			}
		}
	} catch (CoreException e) {}
	return null;
}
 
Example 14
Source File: NatureUtils.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean hasNature(IProject project, String nature) {
    if (project == null || !project.isOpen()) return false;
    try {
           return project.getNature(nature) != null;
       } catch (CoreException e) {
           LogHelper.logError(e);
       }
    return false;
}
 
Example 15
Source File: SGenNewFileWizard.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
protected void ensureSCTNature(IProject project) throws CoreException {
	if (project.getNature(SCTNature.NATURE_ID) == null) {
		new ToggleSCTNatureAction().toggleNature(project);
	}
}
 
Example 16
Source File: JavaUtils.java    From developer-studio with Apache License 2.0 4 votes vote down vote up
public static boolean isJavaProject(IProject project) throws CoreException{
	return project.getNature(ProjectConstants.JAVA_NATURE_ID)!=null;
}
 
Example 17
Source File: GotoResourceAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * This is the orignal <code>select</code> method. Since
 * <code>GotoResourceDialog</code> needs to extend
 * <code>FilteredResourcesSelectionDialog</code> result of this
 * method must be combined with the <code>matchItem</code> method
 * from super class (<code>ResourceFilter</code>).
 *
 * @param resource
 *            A resource
 * @return <code>true</code> if item matches against given
 *         conditions <code>false</code> otherwise
 */
private boolean select(IResource resource) {
	IProject project= resource.getProject();
	try {
		if (project.getNature(JavaCore.NATURE_ID) != null)
			return fJavaModel.contains(resource);
	} catch (CoreException e) {
		// do nothing. Consider resource;
	}
	return true;
}