简体   繁体   English

用于在项目中搜索文件的eclipse插件编程(需要基本帮助)

[英]Programming eclipse plugin for searching file within a project (basic help needed)

I have to program an Eclipse-Plugin but I have never done this before so I have some questions. 我必须对Eclipse-Plugin进行编程,但之前从未做过此操作,因此我有一些疑问。
The plugin should appear in the context menu when you right click a Java project in the project explorer of Eclipse. 在Eclipse的项目浏览器中右键单击Java项目时,该插件应显示在上下文菜单中。 It should open a dialog where the user can enter a file name he is looking for within the selected project and then the file gets highlighted (if there is a file with this name). 它应该打开一个对话框,用户可以在其中输入所选项目中要查找的文件名,然后突出显示该文件(如果存在具有该名称的文件)。
What I managed to do so far is to setup the plugin development project, the extension point for the plugin and the dialog. 到目前为止,我设法完成的工作是设置插件开发项目,插件的扩展点和对话框。
But now I don't know how to get access to the selected project. 但是现在我不知道如何访问所选项目。 Can you tell me how this is done or a link to the corresponding API? 您能告诉我这是如何完成的,还是可以链接到相应的API?
Thanks in advance :) 提前致谢 :)

I assume you have a Handler class for the right-click action in your plugin. 我假设您在插件中有一个用于右键单击操作的Handler类。 The Handler extends the AbstractHandler and overrides the method execute(..). Handler扩展AbstractHandler并覆盖方法execute(..)。

Then you can do something like this: 然后,您可以执行以下操作:

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

Look also at the Eclipse API for IProject for what you can do: http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IProject.html 还要查看IProject的Eclipse API,以了解您可以做什么: http : //help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/ IProject.html

For example getting a file from name: 例如,从名称获取文件:

IFile getFile(String name) IFile getFile(字符串名称)

Returns a handle to the file with the given name in this project. 返回此项目中具有给定名称的文件的句柄。

Hope this helps. 希望这可以帮助。

By the way: if you need some nice tutorials about developing Eclipse plugins, I can recommend this website http://www.vogella.com/eclipse.html 顺便说一句:如果您需要一些有关开发Eclipse插件的不错的教程,我可以推荐这个网站http://www.vogella.com/eclipse.html

Cheers. 干杯。

I write some util class to do the job. 我编写了一些util类来完成这项工作。 Hope it help you 希望对你有帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM