You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Eclipse RCP中实现.bps文件自动引用搜索Handler

问题描述

我开发了一个基于Eclipse RCP的应用,其中包含.bps文件。目前右键点击.bps项目时会弹出下拉菜单,我已在菜单中添加了类似Eclipse中Java文件的“Reference”选项。
我希望实现一个Handler,当用户点击该“Reference”选项时,若选中的文件名为MyBps.bps,自动以“MyBps”作为文本执行搜索并在搜索栏展示结果。

用户原尝试代码:

public class ReferencesSearchHandler extends AbstractHandler {
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        if (window != null) {
            ICommandService commandService = window.getService(ICommandService.class);
            if (commandService != null) {
                Command command = commandService.getCommand("org.eclipse.search.ui.openSearchDialog");
                if (command != null) {
                    // Check if the command exists and can be executed before executing
                    if (command.isDefined() && command.isEnabled()) {
                        try {
                            command.executeWithChecks(new ExecutionEvent());
                        } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (NotDefinedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (NotEnabledException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (NotHandledException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }
}
解决方案

修改后的Handler代码可直接实现自动提取文件名、填充搜索文本并展示结果:

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.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.SearchUI;
import org.eclipse.search.ui.text.TextSearchQueryProvider;
import org.eclipse.search.ui.text.TextSearchScope;
import org.eclipse.ui.handlers.HandlerUtil;

public class ReferencesSearchHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        // 获取当前选中的文件对象
        ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) {
            return null;
        }

        Object selectedObj = ((IStructuredSelection) selection).getFirstElement();
        if (!(selectedObj instanceof IFile)) {
            return null;
        }

        IFile selectedFile = (IFile) selectedObj;
        String fileName = selectedFile.getName();
        // 提取去掉.bps后缀的搜索文本
        String searchText = fileName.substring(0, fileName.lastIndexOf('.'));

        // 创建文本搜索查询并自动执行
        TextSearchQueryProvider provider = TextSearchQueryProvider.getPreferred();
        TextSearchScope scope = TextSearchScope.newWorkspaceScope();
        org.eclipse.search.ui.text.IQuery query = provider.createQuery(searchText, scope);
        
        try {
            // 后台执行搜索
            NewSearchUI.runQueryInBackground(query);
            // 打开搜索视图并展示结果
            SearchUI.openSearchDialog(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), query);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

关键逻辑说明

  • 获取选中文件:通过HandlerUtil.getCurrentSelection获取右键选中的对象,校验是否为IFile类型,确保操作对象是.bps文件。
  • 提取搜索文本:截取文件名去掉.bps后缀,得到目标搜索内容。
  • 自动执行搜索:使用Eclipse原生SearchUI API创建查询,后台执行搜索并直接打开搜索视图展示结果,无需用户手动输入。

如果需要保留打开搜索对话框并自动填充文本的逻辑,可替换原命令执行部分为以下代码(依赖搜索命令支持参数传递):

Map<String, String> parameters = new HashMap<>();
parameters.put("org.eclipse.search.ui.searchText", searchText);
ExecutionEvent newEvent = new ExecutionEvent(command, parameters, null, null);
command.executeWithChecks(newEvent);

内容的提问来源于stack exchange,提问作者user22061831

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 04:20:14