Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Code to get the Selected File's "Full Path" from Eclipse workspace

Hello Developers,

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.

Any clue as to where am going wrong?

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


Back to the top