Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Opening a file in a TextEditor via FileEditorInput
Opening a file in a TextEditor via FileEditorInput [message #290747] Wed, 31 August 2005 07:07 Go to next message
Eclipse UserFriend
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.

My problem is: Step 3 fails because I get a NULL as a result of findMember
but the file is definitely there.

Here is a code snippet I used for testing

IPath path = new Path("bla.m");
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
System.out.println("[HEADER]: " + root.getFullPath().toString());

IFile file = (IFile) root.getProject().findMember(path);

if (file != null)
{
System.out.println("[HEADER]: " + file.getFullPath());
input = new FileEditorInput(file);
}
else
{
System.out.println("[HEADER]: Error. file is null!");
input = getEditorInput();
}

int index = addPage(editor, input);
setPageText(index, "[Class Declaration]: " + input.getName());


Greetings

Matthias

--
Re: Opening a file in a TextEditor via FileEditorInput [message #290752 is a reply to message #290747] Wed, 31 August 2005 09:16 Go to previous message
Eclipse UserFriend
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
Previous Topic:Team API for setting SyncInfo
Next Topic:Default editors ?
Goto Forum:
  


Current Time: Tue Jul 08 22:05:10 EDT 2025

Powered by FUDForum. Page generated in 0.06745 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top