0

我正在尝试根据当前节点/节点状态实现启用或禁用的操作(在应用程序顶部菜单中,以及右键单击节点的弹出菜单中)。

@ActionID(
        category = "Application",
        id = "it.cre.app.tree.actions.ShowEventsAction")
@ActionRegistration(
        iconBase = "it/cre/app/tree/actions/show.png",
        displayName = "#CTL_ShowAction")
@ActionReferences({
    @ActionReference(path = "Menu/Edit", position = 100),
    @ActionReference(path = "Toolbars/File", position = 300),
    @ActionReference(path = "Application/edit", position = 0)})
@NbBundle.Messages("CTL_ShowAction=Show Events")
public class ShowEventsAction implements ActionListener {

    private ShowAuditEventsCapability showAuditEventsCapability;

    public ShowEventsAction(ShowAuditEventsCapability context) {
        showAuditEventsCapability = context;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (showAuditEventsCapability != null) {
            doSomething();
        }
    }
}

For example: This action, to default, is enabled only if one node is selected otherwise is disabled (visible but disabled). 我想要相同的行为,但也基于所选节点的状态。

我所有的节点都实现了我的接口:

public interface INode {
    //obviously the state of the node could change at runtime
    public boolean someState(); 
}

所以我可以通过以下方式在我的操作中获取节点的状态:

boolean state = Utilities.actionsGlobalContext().lookup(INode.class).someState();

当我选择节点时,如何使用前面的代码片段来启用/禁用操作,就像选择多个节点时禁用此操作一样?

有什么建议么?

4

1 回答 1

0

我在这里找到了解决方案:

public class ShowEventsAction extends AbstractAction implements LookupListener, ContextAwareAction {

    private Lookup context;
    Lookup.Result<ShowAuditEventsCapability> lkpInfo;

    public ShowEventsAction() {
        this(Utilities.actionsGlobalContext());
    }

    public ShowEventsAction(Lookup context) {
        super("Show Audit Events", ImageUtilities.loadImageIcon("it/cre/myapp/audittree/actions/show.png", false));
        this.context = context;
    }

    void init() {
        assert SwingUtilities.isEventDispatchThread() : "this shall be called just from AWT thread";

        if (lkpInfo != null) {
            return;
        }

        //The thing we want to listen for the presence or absence of
        //on the global selection
        lkpInfo = context.lookupResult(ShowAuditEventsCapability.class);
        lkpInfo.addLookupListener(this);
        resultChanged(null);
    }

    @Override
    public boolean isEnabled() {
        init();
        return super.isEnabled();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        init();
        for (ShowAuditEventsCapability showAuditEventsCapability : lkpInfo.allInstances()) {
            showAuditEventsCapability.doSomething();
        }
    }

    @Override
    public void resultChanged(LookupEvent ev) {
        int selected = lkpInfo.allInstances().size();

        if (selected == 0) {
            setEnabled(false);
            return;
        }

        for (EasyDbNode node : Utilities.actionsGlobalContext().lookupAll(INode.class)) {
            if (!node.isEnabled()) {
                setEnabled(false);
                return;
            }
        }
        setEnabled(true);
    }

    @Override
    public Action createContextAwareInstance(Lookup actionContext) {
        return new ShowEventsAction(context);
    }
}

有用!但我认为将方法 isEnabled() 放在 Capability 中而不是 Node 中应该更好,因为启用了该属性的 Capability 是:

public void resultChanged(LookupEvent ev) {
    for(ShowAuditEventsCapability capability : lkpInfo.allInstances()) {
        if(!capability.isEnabled()) {
            setEnabled(false);
            return;
        }
    }
    setEnabled(true);
}
于 2013-09-03T10:36:36.827 回答