Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Open a file programatically in Eclipse editor -- please help if possible
Open a file programatically in Eclipse editor -- please help if possible [message #511208] Sun, 31 January 2010 01:06 Go to next message
Rohit  is currently offline Rohit Friend
Messages: 24
Registered: July 2009
Junior Member
Hello guys,

This may be a well discussed topic for most of you. I am trying to open a file(a .c file to be specific) external to Eclipse programatically in Eclipse editor. I went through a lot of posts and forums/google search. I tried a few ways but to no avail.
Most of the things I tried I realised were for older versions of Eclipse API's.
Finally I found a way which required org.eclipse.core.runtime plugin. However the version was 2.1. I tried to include that and the code gave me no compile errors, but a runtime error saying
Exception in thread "main" java.lang.VerifyError: (class: org/eclipse/core/runtime/Plugin, method: <init> signature: (Lorg/eclipse/core/runtime/IPluginDescriptor;)V) Incompatible object argument for function call
at com.example.mouse.FileOpen.main(FileOpen.java:85)


Line 85 was
IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
Here are some of my code snippets I tried but didnt work

1 )
   File file = new File("/home/rohit/func.c");
		IFileStore fileOnLocalDisk = EFS.getLocalFileSystem().getStore(file.toURI());
		FileStoreEditorInput editorInput = new FileStoreEditorInput(fileOnLocalDisk);
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		try {
			page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEdtior");
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/



2)
  IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
		IPath location = new Path("/home/rohit/func.c");
		IFile file = ws.getFileForLocation(location);
		IProject project = ws.getProject("External Files");
		
		if (!project.exists())
			try {
				project.create(null);
			
		if (!project.isOpen())
		    project.open(null);
		
		//IFile file = project.getFile(location.lastSegment());
		//file.createLink(location, IResource.NONE, null);
		FileEditorInput fileEditorInput = new FileEditorInput(file);
		IWorkbench workbench = PlatformUI.getWorkbench();
		IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(file.getName());
		IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
		page.openEditor(fileEditorInput, desc.getId());
		
		
		
		
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}



Well there are few more but no point listing them because all of them gave me errors.
The point is how do I open an external file in Eclipse editor. Please point me to some article/post for Eclipse 3.5.1. There are many out there for older versions but nothing complete. I have spent a lot of time on this and have proceeded no where...
Any help would be greatly appreciated. Thank you for your time.


Rohit

[Updated on: Mon, 01 February 2010 17:12]

Report message to a moderator

Re: Open files programatically in Eclipse [message #511634 is a reply to message #511208] Tue, 02 February 2010 11:00 Go to previous message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Rohit wrote:
> Hello guys,
Please don't cross-post. You asked the same on the newcomer newsgroup.
See my answer there.

Dani
>
> This may be a well discussed topic for most of you. I am trying to
> open a file(a .c file to be specific) external to Eclipse
> programatically in Eclipse editor. I went through a lot of posts and
> forums/google search. I tried a few ways but to no avail.
> Most of the things I tried I realised were for older versions of
> Eclipse API's.
> Finally I found a way which required org.eclipse.core.runtime plugin.
> However the version was 2.1. I tried to include that and the code gave
> me no compile errors, but a runtime error saying
> Exception in thread "main" java.lang.VerifyError: (class:
> org/eclipse/core/runtime/Plugin, method: <init> signature:
> (Lorg/eclipse/core/runtime/IPluginDescriptor;)V) Incompatible object
> argument for function call
> at com.example.mouse.FileOpen.main(FileOpen.java:85)
>
> Line 85 was
> IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
> Here are some of my code snippets I tried but didnt work
>
> 1 ) File file = new File("/home/rohit/func.c");
> IFileStore fileOnLocalDisk =
> EFS.getLocalFileSystem().getStore(file.toURI());
> FileStoreEditorInput editorInput = new
> FileStoreEditorInput(fileOnLocalDisk);
> IWorkbenchWindow window =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow();
> IWorkbenchPage page = window.getActivePage();
> try {
> page.openEditor(editorInput,
> "org.eclipse.ui.DefaultTextEdtior");
> } catch (PartInitException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }*/
>
>
> 2) IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
> IPath location = new Path("/home/rohit/func.c");
> IFile file = ws.getFileForLocation(location);
> IProject project = ws.getProject("External Files");
>
> if (!project.exists())
> try {
> project.create(null);
>
> if (!project.isOpen())
> project.open(null);
>
> //IFile file = project.getFile(location.lastSegment());
> //file.createLink(location, IResource.NONE, null);
> FileEditorInput fileEditorInput = new FileEditorInput(file);
> IWorkbench workbench = PlatformUI.getWorkbench();
> IEditorDescriptor desc =
> workbench.getEditorRegistry().getDefaultEditor(file.getName( ));
> IWorkbenchPage page =
> workbench.getActiveWorkbenchWindow().getActivePage();
> page.openEditor(fileEditorInput, desc.getId());
>
>
>
>
> } catch (CoreException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
>
>
> Well there are few more but no point listing them because all of them
> gave me errors. The point is how do I open an external file in Eclipse
> editor. Please point me to some article/post for Eclipse 3.5.1. There
> are many out there for older versions but nothing complete. I have
> spent a lot of time on this and have proceeded no where...
> Any help would be greatly appreciated. Thank you for your time.
>
>
> Rohit
Previous Topic:Eclipse crashes when cutting text in arbitrary editor
Next Topic:Workspace launcher change in behavior
Goto Forum:
  


Current Time: Wed Apr 24 18:41:58 GMT 2024

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

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

Back to the top