繁体   English   中英

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

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

我必须对Eclipse-Plugin进行编程,但之前从未做过此操作,因此我有一些疑问。
在Eclipse的项目浏览器中右键单击Java项目时,该插件应显示在上下文菜单中。 它应该打开一个对话框,用户可以在其中输入所选项目中要查找的文件名,然后突出显示该文件(如果存在具有该名称的文件)。
到目前为止,我设法完成的工作是设置插件开发项目,插件的扩展点和对话框。
但是现在我不知道如何访问所选项目。 您能告诉我这是如何完成的,还是可以链接到相应的API?
提前致谢 :)

我假设您在插件中有一个用于右键单击操作的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

干杯。

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

    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