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

Eclipse RCP E4删除选中元素后如何调整IStructuredSelection禁用对应按钮

解决方案

你需要从三个地方调整代码,彻底解决按钮状态不更新的问题:

1. 优化@CanExecute校验逻辑

增加包裹是否存在于当前模型的判断,就算selection残留了已删除的包裹引用,也会返回false:

public class SendProposalHandler {
    // 注入模型服务用于校验包裹是否存在
    @Inject IEnviosService model;

    @CanExecute
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
        @Named(IServiceConstants.ACTIVE_PART) MPart part) {
        if (selection == null || part == null || selection.isEmpty())
            return false;
        Object selectObj = selection.getFirstElement();
        if (!(selectObj instanceof Package))
            return false;
        Package pack = (Package) selectObj;
        // 新增校验:包裹必须同时满足 存在于模型中 + 待发送状态
        return model.getPackageList().contains(pack) && pack.isReadyToSendProposal();
    }

    // 其余执行逻辑调整
    @Execute
    public void execute(Shell shell,
        @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, ESelectionService selectionService) {
        Package pack = (Package) selection.getFirstElement();
        model.sendProposal(pack);
        // 直接清空当前选中
        selectionService.setSelection(StructuredSelection.EMPTY);
    }
}

注:getPackageList()替换为你实际获取包裹列表的方法。

2. 修改导航部件的包裹发送事件监听逻辑

视图刷新后主动触发按钮状态重新评估:

@Inject
@Optional
public void sentProposal(@UIEventTopic(MundiaurumEventConstants.PROPOSAL_SENT) Package env) {
    viewer.refresh();
    // 主动触发所有按钮的可用状态重新校验
    broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
}

3. 可选:优化删除后的选中逻辑

如果需要删除包裹后自动选中相邻元素,可以补充如下逻辑:

@Inject
@Optional
public void sentProposal(@UIEventTopic(MundiaurumEventConstants.PROPOSAL_SENT) Package env) {
    int removedIndex = model.getPackageList().indexOf(env);
    viewer.refresh();
    // 删除后优先选中上一个元素,没有上一个就选下一个,列表为空则清空选中
    IStructuredSelection newSelection = StructuredSelection.EMPTY;
    if (!model.getPackageList().isEmpty()) {
        int selectIndex = removedIndex > 0 ? removedIndex -1 : 0;
        newSelection = new StructuredSelection(model.getPackageList().get(selectIndex));
    }
    viewer.setSelection(newSelection);
    selectionService.setSelection(newSelection);
    broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 09:27:04