Eclipse Plugin: how to get the path to the currently selected project

前端 未结 3 2024
迷失自我
迷失自我 2020-12-31 08:17

I am writing an Eclipse plugin that will display a menu item in the context menu for a Java Project. I have written the plugin.xml as follows:



        
3条回答
  •  庸人自扰
    2020-12-31 08:44

    Had a very long day working my way through the answers here and a whole lot of other answers and found the bit that finally got me there was to do a Java Reflection loop to study the Class and Interface structure of the selection from the Tree in the navigator. The Class introspection loop is in its final resting place but was originally much closer to the top of the execute method.

    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.ui.IWorkbenchPage;
    import org.eclipse.ui.IWorkbenchWindow;
    import org.eclipse.ui.handlers.HandlerUtil;
    import org.eclipse.jface.viewers.ISelection;
    import org.eclipse.jface.viewers.IStructuredSelection;
    import org.eclipse.jface.viewers.ITreeSelection;
    import org.eclipse.jface.viewers.TreePath;
    import org.eclipse.jface.viewers.TreeSelection;
    
    public class MyHandler extends AbstractHandler {
    /**
     * The constructor.
     */
    public MyHandler() {
    }
    
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        IWorkbenchPage activePage = window.getActivePage();
        ISelection selection = activePage.getSelection();
        if (selection != null) {
            System.out.println("Got selection");
            if (selection instanceof IStructuredSelection) {
                System.out.println("Got a structured selection");
                if (selection instanceof ITreeSelection) {
                    TreeSelection treeSelection = (TreeSelection) selection;
                    TreePath[] treePaths = treeSelection.getPaths();
                    TreePath treePath = treePaths[0];
    
                    System.out.println("Last");
                    Object lastSegmentObj = treePath.getLastSegment();
                    Class currClass = lastSegmentObj.getClass();
                    while(currClass != null) {
                        System.out.println("  Class=" + currClass.getName());
                        Class[] interfaces = currClass.getInterfaces();
                        for(Class interfacey : interfaces) {
                            System.out.println("   I=" + interfacey.getName());
                        }
                        currClass = currClass.getSuperclass();
                    }
                    if(lastSegmentObj instanceof IAdaptable) {
                        IFile file = (IFile) ((IAdaptable) lastSegmentObj).getAdapter(IFile.class);
                        if(file != null) {
                            System.out.println("File=" + file.getName());
                            String path = file.getRawLocation().toOSString();
                            System.out.println("path: " + path);
                        }
                    }
                }
            }
        }
        return null;
    }
    }
    

提交回复
热议问题