views:

27

answers:

2

Is there any API to get all the files of particular content type in a Eclipse project?

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.

A: 

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();
}
VonC
Thanks for the answer but my question is related to files of particular content type. Perhaps my question is not clear, I edited the question with example.
Adi
+1  A: 

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

nitind
I realized it. IResourceVisitor and IResource.accept(IResourceVisitor) came handy for me.
Adi