Skip to main content

[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

Thank you Jonah.

I tried a couple of more varieties and the following worked.
Appreciate your quick reply here.

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

 

             IEditorReference[] refs = page.getEditorReferences();

 

             for (IEditorReference reference : refs)

             {

                IEditorInput input = null;

                try

                {

                       input = reference.getEditorInput();

                }

                catch (PartInitException e)

                {

                       // TODO Auto-generated catch block

                       e.printStackTrace();

                }

 

                path = getPathFromEditorInput(input);

             }

 

             if (path != null)

           {

                    selectedFile = path.toOSString();

           }

Regards,
Harish

On Wed, Oct 11, 2017 at 6:19 PM, Jonah Graham <jonah@xxxxxxxxxxxxxxxx> wrote:
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
_______________________________________________
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


Back to the top