why "FileEditorInput" not found? [message #526793] |
Mon, 12 April 2010 18:40  |
Eclipse User |
|
|
|
Hi everybody,
I'm trying to open the default editor for a certain file programmatically (not a fresh issue) and stunned by a problem which is never encountered in any old posts related to this issue: the "FileEditorInput" class is not found. Is any one so kind to tell me where it went wrong? Perhaps which plugins I still lack? Many thanks...
My code is pasted as follows. FIY, I'm using Eclipse 3.5.2 - Modelling, and additionally installed feature org.eclilpse.platform v3.5.2 from "software update".
File file = new File(filename);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
IEditorDescriptor desc = PlatformUI.getWorkbench().
getEditorRegistry().getDefaultEditor(file.getName());
try {
page.openEditor(new FileEditorInput(file), desc.getId()); //if I replaced "FileEditorInput" instance with "NullEditorInput", it would work correctly: an editor opened saying there is an exception because of the lack of an input file
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[Updated on: Mon, 12 April 2010 18:45] by Moderator
|
|
|
|
Re: why "FileEditorInput" not found? [message #526867 is a reply to message #526793] |
Tue, 13 April 2010 07:10  |
Eclipse User |
|
|
|
Thanks very much, it got solved following your words. Pasted below is a working and simpler version, all aiming at programmatically opening an external file with type-default editor. (Originally found in eclipse.org - FAQ)
//precondition is org.eclipse.ui.ide plugin should be in dependencies
import java.io.File;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
... ...
... ...
private void openExternalFile(String filename) {
File fileToOpen = new File(filename);
if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
e.printStackTrace();
}
} else {
System.err.println("File " + filename + " does not exist.");
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.04040 seconds