开发者

API to get all the resources of a content type in a Eclipse project

开发者 https://www.devze.com 2022-12-19 10:54 出处:网络
Is there any API to get all the files of particular content type in a Eclipse pro开发者_开发百科ject?

Is there any API to get all the files of particular content type in a Eclipse pro开发者_开发百科ject?

One option is to visit all resources and collect the files of a content type.

I am looking at API which takes IProject and content type id as parameters and returns IPath or IFile or IResource objects. For example get all Java files in a project.

Thanks in advance.


Thats what I used to find all c files in a the current project:

    public static ArrayList<IResource> getAllCFilesInProject(){
    ArrayList<IResource> allCFiles = new ArrayList<IResource>();
    IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = FileParaviserUtils.getCurrentProject();

    IPath path = project.getLocation();

    recursiveFindCFiles(allCFiles,path,myWorkspaceRoot);
    return allCFiles;
}

private static void recursiveFindCFiles(ArrayList<IResource> allCFiles,IPath path, IWorkspaceRoot myWorkspaceRoot){
    IContainer  container =  myWorkspaceRoot.getContainerForLocation(path);

    try {
        IResource[] iResources;
        iResources = container.members();
        for (IResource iR : iResources){
            // for c files
            if ("c".equalsIgnoreCase(iR.getFileExtension()))
                allCFiles.add(iR);
            if (iR.getType() == IResource.FOLDER){
                IPath tempPath = iR.getLocation();
                recursiveFindCFiles(allCFiles,tempPath,myWorkspaceRoot);
            }
        }
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static IProject getCurrentProject(){
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window != null)
    {
        IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
        Object firstElement = selection.getFirstElement();
        if (firstElement instanceof IAdaptable)
        {
            IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
            return project;
        }
    }
    return null;
}


No, there is not. Your idea is generally how that would be done.


The release notes of eclipse3.1 did mention at the time (June 2005) a change in the heuristics for content type matching.
It was related on bug 90218, part of bug 82986 (enhancements to matching in 3.1), which references bug 86862 ("need API for related custom objects lookup")

That API didn't make it, but the code is available for you to reuse.

public Object[] findRelatedObjects(IContentType type, String fileName, IRelatedRegistry registry) {
  List allRelated = new ArrayList();
  // first add any objects directly related to the content type
  Object[] related = registry.getRelatedObjects(type);
  for (int i = 0; i < related.length; i++) {
    allRelated.add(related[i]);
  }
  // backward compatibility requested - add any objects related to the file name
  if (fileName != null) {
    related = registry.getRelatedObjects(fileName);
    for (int i = 0; i < related.length; i++) {
      if (!allRelated.contains(related[i])) {
        // we don't want to return duplicates
        allRelated.add(related[i]);
      }
    }
  }
  // now add any indirectly related objects, walking up the content type hierarchy 
  while ((type = type.getBaseType()) != null) {
    related = registry.getRelatedObjects(type);
    for (int i = 0; i < related.length; i++) {
      if (!allRelated.contains(related[i])) {
        // we don't want to return duplicates          
        allRelated.add(related[i]);
      }
    }
  }
  return allRelated.toArray();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号