9

I am working on eclipse plugin in which i have to open a file from project explorer. Suppose i have a project ABC in project explorer. after right click on project i got a option to run my plugin tool. after processing i got some result like Check file xyz.java.

Now i want to open this file in IDE by code

i am using this

File absolute = new File("/Decider.java"); 
File file = new File("/Decider.java");
IFileStore fileOnLocalDisk = EFS.getLocalFileSystem().getStore(absolute.toURI() );
FileStoreEditorInput editorInput = new FileStoreEditorInput(fileOnLocalDisk);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
 try { 
 page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEditor"); 
 page.openEditor(editorInput, "MyEditor.editor"); 
 IFileStore fileStore = EFS.getLocalFileSystem().getStore(absolute.toURI() );
 IDE.openEditorOnFileStore( page, fileStore );
 } catch (PartInitException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 try {
 System.out.println(file.getCanonicalPath());
 } catch (IOException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 }
 IPath path = new Path(" /DirectoryReader.java");
 IFile sampleFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
 IEditorInput editorInput1 = new FileEditorInput(sampleFile);
 IWorkbenchWindow window1=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 IWorkbenchPage page1 = window1.getActivePage();
 try {
 page1.openEditor(editorInput1, "org.eclipse.ui.DefaultTextEdtior");
 } catch (PartInitException e1) {
 e1.printStackTrace();
 } 

here it's create a new file named decider in c drive which means it's getting a wrong path.

but when i use path code in some independent java file as a normal JAVA project it's getting the correct path.

asked Oct 8, 2013 at 4:56
4
  • I'm not clear what you are asking. Do you just want to open the default editor on a project file, or a specific editor or what? Commented Oct 8, 2013 at 6:56
  • just want to open default eclipse editor as mention in code which is "org.eclipse.ui.DefaultTextEditor" Commented Oct 8, 2013 at 6:58
  • This problen is like when i run the plugin it's shows some error in a view like console of eclipse. Right now i want to open file which have bug.we have file name as you can see in absolute variable. i want to open this file but by above code it's creating a new file in new location. Commented Oct 8, 2013 at 7:00
  • and this is eclipse editor for development. Commented Oct 8, 2013 at 7:01

1 Answer 1

8

For a file in the workspace you should use IFile. If you have a selection from Project Explorer or another view that should already be an IFile or can be adapted to an IFile.

If you just have a workspace relative path use ResourcesPlugin.getWorkspace().getRoot().getFile(path) (path would include a project).

To open the default editor for the file contents use

IDE.openEditor(page, file, true);

to open a specific editor use

IDE.openEditor(page, file, "editor id");

IDE is org.eclipse.ui.ide.IDE.

answered Oct 8, 2013 at 7:10
Sign up to request clarification or add additional context in comments.

1 Comment

@KrzyH In general, no, you can't open an editor at a specific line - some editors don't even have the concept of a line. IDE.openEditor does return the IEditorPart that was opened, editors which implement ITextEditor have a selectAndReveal method which shows an offset in the file.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.