1

I am writing a simple Eclipse-plugin that displays a UI after right-mouse click on a specific element, located inside an xml file. In the handler, i need to get first the file path, which contains the selected object. I tried the following posts unfortunately did not work for me.

How to get the Path of current selected file in Eclipse?

IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj,
            IFile.class);

Eclipse Plugin Get Code from Current Open File

IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);

note: the file need not be opened in active editor.

  • What is the selected object, and how did you obtain it? What happens instead of what you expected? – nitind Sep 17 '18 at 12:23
  • Tnx @nitind, the selected object is of type EObject from Ecore, selected using ` IStructuredSelection ssel = (IStructuredSelection) selection; Object obj = ssel.getFirstElement();` The file returns null in the first approach and returns the active file in the second approach. Note: i want to get the file path regardless of active or not, but only on the object selected. – Nesredin Mahmud Sep 17 '18 at 15:11

1 Answers1

1

you need to retrieve the EResource from the EObject. Please note that EObject might be in-memory and in this case there is no EResource/File associated with it.

See https://www.eclipse.org/forums/index.php?t=msg&th=796431&goto=1404419&#msg_1404419

Regards,

Aurélien Pupier
  • 724
  • 1
  • 8
  • 23