2010-02-05 45 views
4

是否有任何API可以獲取Eclipse項目中特定內容類型的所有文件?獲取Eclipse項目中所有內容類型資源的API

一種選擇是訪問所有資源並收集內容類型的文件。

我正在查看以IProject和內容類型ID作爲參數並返回IPath或IFile或IResource對象的API。例如獲取項目中的所有Java文件。

在此先感謝。

回答

2

不,沒有。你的想法一般是如何完成的。

+1

我意識到了。 IResourceVisitor和IResource.accept(IResourceVisitor)對我來說非常方便。 – Adi 2010-02-08 04:17:59

0

release notes of eclipse3.1當時(2005年6月)提到了內容類型匹配啓發式的改變。
它是在bug 90218相關的bug 82986部分(增強在3.1匹配),它引用bug 86862(「需要相關的定製API對象查找」)

該API沒能成功,但code is available爲你重用。

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

感謝您的回答,但我的問題是有關特定內容類型的文件。也許我的問題不清楚,我用例子編輯了這個問題。 – Adi 2010-02-05 09:29:43

3

那是什麼我用來尋找所有的C文件在當前項目:

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; 
} 
相關問題