简体   繁体   中英

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. 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. 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 .

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

截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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