aplihs
aplihs

Reputation: 31

How to get .java file path to my eclipse plugin

I am developing a eclipse plugin to ease the development using a proprietary version control system.

Right now there is only a command prompt version of the system available for this VCS and its run in terminal. So from my eclipse plugin I want to provide a simple menu options to do the things like check-out and check-in and internally call these commands. But to run these commands I need to pass the argument 'path' of the selected .java file in the editor/project explorer. How can I get the path of the source file to the plugin?

Upvotes: 1

Views: 993

Answers (1)

greg-449
greg-449

Reputation: 111142

Get the current workbench page:

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

the workbench page implements ISelectionService so you can get the current selection:

ISelection selection = page.getSelection();

this will generally by an IStructuredSelection (but you need to check)

IStructuredSelection sel = (IStructuredSelection)selection;

See if this adapts to an IFile:

Object selObject = sel.getFirstElement();  // or iterate through all the selection elements

IFile file = Platform.getAdapterManager().getAdapter(selObject, IFile.class);

if you get a file the full path is:

String location = file.getLocation().toOSString();

If the current part is an editor then the selection you receive might be a text string. So you need to deal with editors separately:

IWorkbenchPart activePart = page.getActivePart();

if (activePart instanceof IEditorPart)
 {
   IEditorInput input = ((IEditorPart)activePart).getEditorInput();

   if (input instanceof IFileEditorInput)
    {
      IFile file = ((IFileEditorInput)input).getFile();

      ...
    }
 }

Upvotes: 3

Related Questions