0

How do I retrieve the name and path of the project selected? (Package Explorer)

example: c:\project\test\projectName


someone has some code that explains how to complete I get the project name or full path of a particular project in my workspace?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
BlaBRA
  • 2,201
  • 4
  • 18
  • 13

2 Answers2

2

Eclipse defines an extension point "org.eclipse.ui.navigator.linkHelper"

If you contribute a class to these EP you have to implement ILinkHelper

The ILInkHelper interface notifies you, when something was selected in the explorer

  • public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection)

You can check the type of the selection

 if (aSelection.getFirstElement() instanceof IFile) {
    // Do something
 }
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
  • 1
    \o/ http://stackoverflow.com/questions/6183366/how-to-get-the-path-of-current-selected-file-in-eclipse – BlaBRA Oct 27 '11 at 14:23
1

Old memories but maybe useful for you. I guess package explorer provides its selection, so you can get the current selection in your code by calling:

ISelectionService service = getSite().getWorkbenchWindow().getSelectionService()

than you can get the package explorer view by its id (plugin.xml for more details):

IStructuredSelection selection = (IStructuredSelection) service.getSelection("org.eclipse.jdt.ui.PackageExplorer");

Please note AFAIK you can always safely cast ISelection to IStructuredSelection. Then call structured.getFirstElement() and I think the first element will be an IFile object. I hope my "pseudo code" whould be enough for you. And IFile has lots of usefule methods for your convenience

Bela Vizer
  • 2,527
  • 22
  • 26