Skip to main content



      Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Open TextEditor for filename(Open TextEditor for a virtual File given as IDocument object)
Open TextEditor for filename [message #675749] Wed, 01 June 2011 07:48 Go to next message
Eclipse UserFriend
I want to write a plugin where I have file content stored in a IDocument object. And I have a filename. Both I get over the network.

Now I want to create the proper Editor for the given filename extension and fill the Editor with the IDocument object.

I look for something like this:

String filename = "myFile.java";
IDocument document = new Document("content");

ITextEditor editor = PlatformUI
  .getWorkbench()
  .getActiveWorkbenchWindow()
  .getActivePage()
  .createEditorByFileExtension(filename);

editor.setDocument(document);

[Updated on: Wed, 01 June 2011 07:51] by Moderator

Re: Open TextEditor for filename [message #675845 is a reply to message #675749] Wed, 01 June 2011 13:14 Go to previous messageGo to next message
Eclipse UserFriend
A plugin is binded to particular extension(s). This is configured in the plugin.xml of the plugin :

<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type id="xxxx" name="xxxxxxxxxx"
base-type="org.eclipse.core.runtime.text"
priority="high"
file-extensions="ext1,ext2"
default-charset="ISO-8859-1"/>
</extension>

So when you open a file with one of these extensions, the related editor is picked by itself. Programmatic behind a menu-item or function-key it's something like this :

IWorkspace ws = ResourcesPlugin.getWorkspace();
IProject project = ws.getRoot().getProject("MyProject");
String name = "c:\\file.ext1";
IPath location = new Path(name);
IFile file = project.getFile(location.lastSegment());
file.createLink(location, IResource.NONE, null);
IEditorInput editorInput = new FileEditorInput(file);
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();

IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());

page.openEditor(editorInput, desc.getId() );
Re: Open TextEditor for filename [message #676698 is a reply to message #675845] Mon, 06 June 2011 04:29 Go to previous messageGo to next message
Eclipse UserFriend
Thank you for your answer.

I tried it, but it doesn't work like my idea. I want to open a remote file: All I have is its content and its filename. Now I want to try to replace the class File with another class that implements the IFile interface. Then I will post my results.
Re: Open TextEditor for filename [message #708034 is a reply to message #675845] Tue, 02 August 2011 07:22 Go to previous message
Eclipse UserFriend
Kitesurfer wrote on Wed, 01 June 2011 13:14

IWorkspace ws = ResourcesPlugin.getWorkspace();
IProject project = ws.getRoot().getProject("MyProject");
String name = "c:\\file.ext1";
IPath location = new Path(name);
IFile file = project.getFile(location.lastSegment());
file.createLink(location, IResource.NONE, null);
IEditorInput editorInput = new FileEditorInput(file);
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();

IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());

page.openEditor(editorInput, desc.getId() );


I used above code (in a plugin) to open an external file in a projekt in Eclipse ... it work oke.
But how do I open (in a plugin) an external file in Eclipse, but not in a projekt ? ... so just in the editorwindow.

(It must be possible, because when I do a "Remote search" f.e. on my C-drive ... and I double click on one of the search-result ... it opens the file outside a projekt in the editorwindow).
Previous Topic:How to add my own wizards directly to the "new" menu
Next Topic:Menu for EditorPart
Goto Forum:
  


Current Time: Fri Jul 04 20:17:11 EDT 2025

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

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

Back to the top