abo abo - 5 months ago 29
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

IObjectActionDelegate


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.

Answer

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);