|
Re: Opening a file in a TextEditor via FileEditorInput [message #290752 is a reply to message #290747] |
Wed, 31 August 2005 13:16 |
|
Matthias Muenzner wrote:
> Hi
>
> I have a MultiPageEditorPart including two TextEditors. When I open a file
> in my workspace I get the editor input and assign it together with the
> instance of my TextEditor to a page with setPageText.
>
> Now I want to open another file automatically in the second page (basically
> the same as the first page) - this other file differs just in the file
> extension. The purpose of doing this is to implement a fast switching
> between Header and Implementation files.
>
> Up to now I used following plan:
>
> 1. Get the filename including path and extension
> 2. Replace the extension with the one for the header or implementation
> 3. Try to find the new file via
> ResourcesPlugin.getWorkspace().getRoot().findMember(<pathToFileString > |
> <path object with the same content>)
> 4. construct a FileEditorInput with the information gathered in step 3
> 5. bind this input to the editor as described above.
>
Given an editor input, I've had luck with this kind of code for
switching from .h to .cpp:
IEditorPart editor = window.getActivePage().getActiveEditor();
IEditorInput input = editor.getEditorInput();
IFile file = (IFile) input.getAdapter(IFile.class);
IPath path = file.getFullPath();
path = path.removeFileExtension();
path = path.addFileExtension("cpp");
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile newFile = (IFile)root.findMember(path);
That being said, if you want to provide a convenience to switch between
headers and source files, why use a MultiPageEditorPart? You can
provide an editorAction. When the action (menu, toolbar, whatever)
runs, calculate your "other" path, and then either:
1. get your current editor and switch inputs ... this works with editors
like the TextEditor (example at end) ... you'll always switch files in
your current editor ... you have to take care of saving any changes or
they'll be lost.
2. just switch between editors in the workbench page. Once you've
calculated your IEditorInput, you can use
IWorkbenchPage#findEditor(IEditorInput input) to find your editor and
then use IWorkbenchPage#activate(editor) to bring it to the top. Or if
you don't find it, you can open your editor with your newly calculated
IEditorInput.
The advantage of either of these approaches is you're just using
whatever editors are convenient.
Switching action for a TextEditor (quick example, obviously you'd put
more checks in real code):
public void run(IAction action) {
IEditorPart editor = window.getActivePage().getActiveEditor();
IEditorInput input = editor.getEditorInput();
IFile file = (IFile) input.getAdapter(IFile.class);
IPath path = file.getFullPath();
String oldExt = path.getFileExtension();
String newExt = "h";
if (oldExt.equals(newExt)) {
newExt = "cpp";
}
path = path.removeFileExtension();
path = path.addFileExtension(newExt);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile newFile = (IFile)root.findMember(path);
TextEditor text = (TextEditor)editor;
text.setInput(new FileEditorInput(newFile));
}
Later,
PW
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
|
|
|
Powered by
FUDForum. Page generated in 0.02664 seconds