[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [cdt-dev] Code to get the Selected File's "Full Path" from Eclipse workspace
|
The selection you are getting from the selection service is of a
TextSelection (probably what is selected in the editor). Your
exception is caused by doing a blind cast to IStructuredSelection.
You probably want something like
https://stackoverflow.com/a/11060895/2796832 -- there are a bunch of
other answers on Stack Overflow depending on your exact constraints
(e.g. get selected file in editor vs project explorer, have a solution
that works when UI is open or not).
---
As an aside, don't check and cast to IAdaptable, just call
org.eclipse.core.runtime.Adapters.adapt(Object, Class<T>). Some
classes can be adaptable and not have IAdaptable interface.
Jonah
~~~
Jonah Graham
Kichwa Coders Ltd.
www.kichwacoders.com
On 11 October 2017 at 12:09, Harish Mitty <harishmitty@xxxxxxxxx> wrote:
> 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
>
>
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from
> this list, visit
> https://dev.eclipse.org/mailman/listinfo/cdt-dev