Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 11:48 Go to next message
Eclipse User is currently offline Eclipse UserFriend
Messages: 2
Registered: June 2011
Junior Member
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 11:51]

Report message to a moderator

Re: Open TextEditor for filename [message #675845 is a reply to message #675749] Wed, 01 June 2011 17:14 Go to previous messageGo to next message
Kitesurfer () is currently offline Kitesurfer ()Friend
Messages: 84
Registered: December 2009
Member
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 08:29 Go to previous messageGo to next message
Eclipse User is currently offline Eclipse UserFriend
Messages: 2
Registered: June 2011
Junior Member
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 11:22 Go to previous message
Kitesurfer () is currently offline Kitesurfer ()Friend
Messages: 84
Registered: December 2009
Member
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: Sat Nov 29 01:44:18 GMT 2014

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

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