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

标签 java eclipse eclipse-plugin

我必须编写一个 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(String name)

Returns a handle to the file with the given name in this project.

希望这有帮助。

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

干杯。

关于java - 编程 Eclipse 插件用于在项目中搜索文件(需要基本帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20466388/

相关文章:

java - 当新值不正确时重置 JTable 单元格中的旧值

java - 使用 SimpleDateFormat 获取月份名称错误

java - Eclipse,Java - 从所有验证中排除文件夹

java - 使用 <Loader delegate = true/> 配置 tomcat 上下文文件的替代方法是什么

java - Eclipse连接MySQL时出现 "No suitable driver found"异常如何处理?

java - 为什么我的 Eclipse 插件 ICommand IParameter 的值没有出现在 Preferences/Keys 设置中?

eclipse - 查找 JavaModelUtil.getResolvedTypeName 的替代品

java - 无法使用 Glassfish 设置 SSL

java - Apache Axis 库

android - 无法打开应用程序 "Eclipse"。 -10810