I'm trying to write a small code to get the currently selected file's "Full/Absolute Path" in the workspace.
I have following code, but it is throwing an exception at the line highlighted in yellow below.
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null)
{
IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IAdaptable)
{
@SuppressWarnings("cast")
IProject
project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
IPath path = project.getFullPath();
System.out.println(path);
}
}
Exception:
java.lang.ClassCastException:
org.eclipse.jface.text.TextSelection cannot be cast to
org.eclipse.jface.viewers.IStructuredSelection
at
org.eclipse.cdt.internal.ui.refactoring.executeTC.ExecuteTCRefactoringRunner.run(ExecuteTCRefactoringRunner.java:44)
at
org.eclipse.cdt.ui.refactoring.actions.ExecuteTC.run(ExecuteTC.java:34)
Regards,
Harish