0

In Java, I am trying to programatically open a file on Editor using eclipse. It is important to me that the editor is instance of IFileEditorInput. I used the following code:

 IPath path = new Path(filePath);
 System.out.println("PATH:"+path);
 IFile ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path);
 System.out.println("IFILE: "+ifile);
 //IFileEditorInput editorInput= new FileEditorInput(ifile);
 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
 try {
 IDE.openEditor(page, ifile);
 } catch (PartInitException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

Later at some point, I need to access the file, specifically in the init() method of the Editor class, as the following,

@Override //IEditor
 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
 if (input instanceof IFileEditorInput) {
 try {
 setSite(site);
 setInput(input); 
 IFile ifile=((IFileEditorInput)input).getFile();
 File file=null;
 if(ifile.getLocation()==null)
 {
 System.out.println("file location is NULL..exiting");
 System.exit(0);
 }
 else
 file = ifile.getLocation().toFile();

The problem is that ifile.getLocation() always returns null, and hence, I cannot access the file using File class. What did I do wrong? Thank You.

EDIT: The output of my program is:

PATH:D:/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
IFILE: L/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
file location is NULL..exiting
asked Mar 15, 2018 at 9:43
3
  • is the file already existing in the filesystem or did you create the file in your program but didn't write to disk? also from this docs i can't find the method ifile.getLocation() Commented Mar 15, 2018 at 9:56
  • What is the value of filePath? Is it a path relative to the root of the workspace? Commented Mar 15, 2018 at 9:57
  • The file already exists in the file system. The value of filePath is a file system path that is like, D:/Project... Commented Mar 15, 2018 at 9:59

1 Answer 1

1

The path you give to

ResourcesPlugin.getWorkspace().getRoot().getFile(path);

must be a path relative to the workspace root of a file which exists in the workspace.

You can use getFileForLocation to specify a path which is a full file system path:

ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);

but this will only work if the resulting file is in the workspace.

In general IFile will only work for files in the workspace - but you can create 'links' to files outside the workspace.

If you want to edit a file which is not in the workspace use the

public static IEditorPart openEditor(IWorkbenchPage page, URI uri,
 String editorId, boolean activate)

IDE method passing in the URI of the file to edit and the editor id.

In this case in the editor the IEditorInput will be an instance of IURIEditorInput, not IFileEditorInput.

answered Mar 15, 2018 at 11:14
Sign up to request clarification or add additional context in comments.

Comments

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.