abo abo - 4 months ago 18x
Java Question

How to get selected file name and its package path in eclipse plugin

I want to get the file name and its package path in eclipse when right click menu is clicked.

Action class is implemented


Run method is as follows,

public void run(IAction action)
ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
if (!(sel instanceof IStructuredSelection))
return null;
IStructuredSelection selection = (IStructuredSelection) sel ;
Object obj = selection.getFirstElement();
IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj, IFile.class);

But the "sel" variable is not a instance of IStructuredSelection. Therefore it return null.

I have gone through following link,
How to get the active package path in eclipse workspace

But no result.


The code you show should work for a selection in a view, but if you are dealing with an editor you need to do things a different way.

Something like:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IEditorPart editor = page.getActiveEditor();

IEditorInput input = editor.getEditorInput();

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