简体   繁体   English

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

[英]Instatiate an IFile for a file existing in Eclipse Workspace

I have a Java project in which I need to create a window (possibly using SWT) that prompts the user to select a file already existent in the current workspace. 我有一个Java项目,我需要在其中创建一个窗口(可能使用SWT),提示用户选择当前工作空间中已存在的文件。 Afterwards, it should create an instance of said file (IFile) for the user to perform operations on it, namely to extract information regarding the file's contents. 之后,它应该为用户创建所述文件(IFile)的实例以对其执行操作,即提取关于文件内容的信息。 I'm kinda clueless at this point... 在这一点上,我有点无能为力......

Thanks for the help! 谢谢您的帮助!

You never instantiate an IFile instance, you request one for a path from the IWorkspaceRoot or another IContainer . 您永远不会实例化IFile实例,而是从IWorkspaceRoot或其他IContainer请求一个路径。

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

This is what the ElementTreeSelectionDialog is made for. 这就是ElementTreeSelectionDialog的用途。 You can use it for example like this: 您可以使用它,例如:

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();

截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM