Monday, April 19, 2010

IFile, IFileStore and opening an EditorPart

Eclipse's engines turn around the idea of a project.

If I want to open an Editor on a file, I would have to do:


IFile iFile = myIFile;
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.openEditor(iFile, FormEditor.ID);


apart for the fact that IFile refers on a file inside the project, which makes sense inside eclipse, but perhaps not always in an RCP product basing on eclipse.

So to open an editor on an external (external == wherever in the filesystem != in the eclipse project) file, here is what is possible since 3.3:


File f = new File(path);

IPath ipath = new Path(f.getAbsolutePath());
IFileStore fileLocation = EFS.getLocalFileSystem().getStore(ipath);
FileStoreEditorInput fileStoreEditorInput = new FileStoreEditorInput(
fileLocation);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage();
page.openEditor(fileStoreEditorInput, FormEditor.ID);

No comments: