2015-12-15 48 views
1

我正在開發一個IntelliJ-IDEA插件。FileChooser僅適用於當前項目中的單個目錄

我想使用他們的FileChooser類來選擇當前項目中的單個目錄。

這是我到目前爲止有:

public class MyAction extends AnAction { 
    public void actionPerformed(AnActionEvent actionEvent) { 
     com.intellij.openapi.project.Project project = actionEvent.getProject(); 
     final DataContext dataContext = actionEvent.getDataContext(); 
     assert project != null; 
     final PsiFile currentFile = DataKeys.PSI_FILE.getData(dataContext); 
     VirtualFile chooseFile = project.getBaseDir(); 
     if (currentFile != null) { 
      chooseFile = currentFile.getVirtualFile(); 
     } 
     FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); 
     chooseFile = FileChooser.chooseFile(descriptor, project, chooseFile); 
     if (chooseFile == null) { 
      return; 
     } 
     ... do stuff with the file 
    } 
} 

我想限制文件選擇將僅在當前的IDEA項目顯示文件。 FileChooser應該使其不可能選擇其他任何東西,或者如果用戶選擇其他東西,則顯示錯誤。有沒有辦法做到這一點?

回答

1

你可能會想要做這樣的事情:

descriptor.setRoots(ProjectRootManager.getInstance(project).getContentRoots()); 
+0

我將不得不嘗試這個!我確認它可以工作後我會回來。 – michaelsnowden

相關問題