开发者

how to get file location when user select file/folder in eclipse navigator

i am developing one eclipse p开发者_Python百科lugin. now when user click on any file/folder and click icon(added by me) then i have to get that file location.

can you please help me regarding this one.

Thanks in advance.


Every IWorkbenchPartSite has a method

    /**
     * Sets the selection provider for this workbench site.
     * 
     * @param provider
     *            the selection provider, or <code>null</code> to clear it
     */
    public void setSelectionProvider(ISelectionProvider provider);

When something is selected inside the part site you should get a notification to handle the event.

You can get the Selection by calling

    /**
     * Returns the current selection for this provider.
     * 
     * @return the current selection
     */
    public ISelection getSelection();

I your case the ISelection object should be an instance of IStructuredSelection and the selected objects you can get by calling

public interface IStructuredSelection extends ISelection {
    /**
     * Returns the first element in this selection, or <code>null</code>
     * if the selection is empty.
     *
     * @return an element, or <code>null</code> if none
     */
    public Object getFirstElement();

    /**
     * Returns an iterator over the elements of this selection.
     *
     * @return an iterator over the selected elements
     */
    public Iterator iterator();
...
}

You should get the IResource directly, when the user selects it in the package explorer for example.

You also can get the current used ISelectionProvider from the PartSite the IResource is selected, after the user pressed your Icon-Button.

You can get some Provider of some open view by calling

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("VIEW-ID").getViewSite().getSelectionProvider();

Views:

  • org.eclipse.jdt.ui.PackageExplorer
  • org.eclipse.jdt.ui.ProjectsView
  • org.eclipse.jdt.ui.PackagesView


package com.packpub.e4.menu.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class SampleHandler extends AbstractHandler {
    /**
     * The constructor.
     */
    public SampleHandler() {
    }

    /**
     * the command has been executed, so extract extract the needed information
     * from the application context.
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {     
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);   
        ISelection selection = window.getSelectionService().getSelection();     
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection ssel = (IStructuredSelection) selection;
            Object obj = ssel.getFirstElement();
            IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj, IFile.class);
            if (file != null) {
                if (obj instanceof IAdaptable) {
                      file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
                      String path = file.getRawLocation().toOSString();                    
                      String fileName = "File=" + file.getName();
                      String filePath = "\nPath: " + path;                                     
                      MessageDialog.openInformation(
                              window.getShell(),
                              "Menu",
                              fileName+ filePath);
                }
            }

        }
        return null;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜