为Eclipse Workspace中存在的文件实例化IFile

时间:2017-03-06 12:24:46

标签: java eclipse swt

我有一个Java项目,我需要创建一个窗口(可能使用SWT),提示用户选择当前工作区中已存在的文件。之后,它应该为用户创建所述文件(IFile)的实例以对其执行操作,即提取关于文件内容的信息。在这一点上,我有点无能为力......

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您永远不会实例化IFile个实例,而是从IWorkspaceRoot或另一个IContainer申请一个路径。

http://help.eclipse.org/neon/topic/org.eclipse.platform.doc.isv/guide/resInt.htm?cp=2_0_10

答案 1 :(得分:0)

这就是ElementTreeSelectionDialog的用途。 您可以使用它,例如:

ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
    shell, new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.setTitle("File selection");
dialog.setMessage("Choose a file");
dialog.setAllowMultiple(false);
// ...
dialog.addFilter(new ViewerFilter() {
    @Override
    public boolean select(Viewer viewer, Object parentElement, Object element) {
        return true;  // adapt to your need
    }
});
dialog.open();
IFile selectedFile = (IFile) dialog.getFirstResult();

screenshot