当用户在eclipse导航器中选择文件/文件夹时如何获取文件位置

时间:2010-04-06 12:54:24

标签: eclipse-plugin

我正在开发一个eclipse插件。现在,当用户点击任何文件/文件夹并单击图标(由我添加)时,我必须获取该文件位置。

你可以帮我解决这个问题。

提前致谢。

2 个答案:

答案 0 :(得分:1)

每个 IWorkbenchPartSite 都有一个方法

    /**
     * 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);

如果在零件网站内选择了某些内容,您应该收到处理该活动的通知。

您可以通过调用

来获取选择
    /**
     * Returns the current selection for this provider.
     * 
     * @return the current selection
     */
    public ISelection getSelection();

我的情况是,ISelection对象应该是IStructuredSelection的一个实例,你可以通过调用

获得所选对象
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();
...
}

当用户在包浏览器中选择IResource时,您应直接获取IResource。

在用户按下Icon按钮后,您还可以从PartSite中获取当前使用的ISelectionProvider IResource。

您可以通过调用

获取一些开放视图的提供者
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("VIEW-ID").getViewSite().getSelectionProvider();

查看:

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

答案 1 :(得分:0)

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;
    }
}