【问题标题】:Programming eclipse plugin for searching file within a project (basic help needed)用于在项目中搜索文件的编程 eclipse 插件(需要基本帮助)
【发布时间】:2013-12-09 08:38:17
【问题描述】:

我必须编写一个 Eclipse 插件,但我以前从未这样做过,所以我有一些问题。
当您在 Eclipse 的项目资源管理器中右键单击 Java 项目时,该插件应该出现在上下文菜单中。它应该会打开一个对话框,用户可以在其中输入他在所选项目中查找的文件名,然后文件会突出显示(如果存在具有此名称的文件)。
到目前为止,我所做的是设置插件开发项目、插件的扩展点和对话框。
但现在我不知道如何访问所选项目。您能告诉我这是如何完成的或提供相应 API 的链接吗?
在此先感谢:)

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

    我假设您的插件中有一个用于右键单击操作的 Handler 类。 Handler 扩展了 AbstractHandler 并覆盖了方法 execute(..)。

    然后你可以这样做:

    public class YourHandler extends AbstractHandler {
    
    private ExecutionEvent event;
    
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
    
        // First get the tree of the right-clicked project.
        ISelection sel = HandlerUtil.getActiveMenuSelection(event);
    
        IResource resource = null;
        IProject project = null;
    
        try {
            IStructuredSelection selection = (IStructuredSelection) sel;
    
            // Get the first element of the tree (return type Object).
            Object firstElement = selection.getFirstElement();
    
            // Get the IResource and from this the IProject of the selection.
            if (firstElement instanceof IAdaptable) {
                IResource resource = (IResource) (((IAdaptable) firstElement)
                    .getAdapter(IResource.class));
    
                project = res.getProject();
            }
        } catch (ClassCastException e) {
            // Do nothing.
        }
    
        // Then you can do something with the project.
    
        return project;
    }
    

    还可以查看 IProject 的 Eclipse API,了解您可以做什么:http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IProject.html

    例如从名称中获取文件:

    IFile getFile(字符串名称)

    返回此项目中具有给定名称的文件的句柄。

    希望这会有所帮助。

    顺便说一句:如果你需要一些关于开发 Eclipse 插件的好教程,我可以推荐这个网站http://www.vogella.com/eclipse.html

    干杯。

    【讨论】:

    • 谢谢,这看起来对我很有帮助。一旦有机会尝试,我会再次回答。
    • 嗨弗兰克。我的回答解决了你的问题吗?如果是这样,请接受答案。谢谢。
    【解决方案2】:

    我编写了一些实用程序类来完成这项工作。希望对你有帮助

        public class SelectionUtil {
        private IWorkbenchWindow window;
        private IWorkbenchPage activePage;
        private TreeSelection treeSelection;
        private TreePath[] treePaths;
        HashMap<Object, Object> selectData;
        private IProject theProject;
        private IResource theResource;
        private IFile theFile;
        private IPackageFragment theFragment;
        private String workspaceName;
        private String projectName;
        private String fileName;
        private String fileNameFile;
        private String fragmentName;
        private TreePath treePath;
        public SelectionUtil(ExecutionEvent event) {
            this.window = HandlerUtil.getActiveWorkbenchWindow(event);
            // Get the active WorkbenchPage
            this.activePage = this.window.getActivePage();
    
            // Get the Selection from the active WorkbenchPage page
            ISelection selection = this.activePage.getSelection();
            if (selection instanceof ITreeSelection) {
                this.treeSelection = (TreeSelection) selection;
                this.treePaths = treeSelection.getPaths();
                this.treePath = treePaths[0];
                selectData = new ProjectSelectionUtil()
                        .populatePojectData(treePath);
                setData();
            } else {
                String selectionClass = selection.getClass().getSimpleName();
                MessageDialog
                        .openError(
                                this.window.getShell(),
                                "Unexpected Selection Class",
                                String.format(
                                        "Expected a TreeSelection but got a %s instead.\nProcessing Terminated.",
                                        selectionClass));
            }
        }
       public void setData() {
            this.theProject = (IProject) selectData.get("Project");
            this.theResource = (IResource) selectData.get("Resource");
            this.theFragment = (IPackageFragment) selectData.get("Fragment");
            this.workspaceName = this.theResource.getWorkspace().getRoot()
                    .getLocation().toOSString();
            this.projectName = this.theProject.getName();
            if (this.theFragment != null)
                this.fragmentName = this.theFragment.getElementName();
            try {
                if (!this.theResource.getName().isEmpty()
                        && this.theResource.getName().length() > 5)
                    this.fileName = this.theResource.getName().substring(0,
                            this.theResource.getName().length() - 5);
            } catch (NullPointerException e) {
                System.out
                        .println(" GactusWindowSelectionUtil SetData NullPointerException"
                                + e.getMessage() + e.getLocalizedMessage());
            } catch (StringIndexOutOfBoundsException e) {
                System.out
                        .println(" StringIndexOutOfBoundsException SetData NullPointerException"
                                + e.getMessage() + e.getLocalizedMessage());
            }
    
        }
    
    
    
    
    
    
        public String toString() {
            ProjectInformation myProject = new ProjectInformation(theProject);
            return "Segment Count " + treePath.getSegmentCount() + " Iproject"
                    + myProject.toString();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2014-06-25
      相关资源
      最近更新 更多