Netbeans Action根据节点的状态启用

时间:2013-08-22 15:03:06

标签: java action netbeans-7 netbeans-platform

我正在尝试实现启用或禁用的操作(在应用程序顶部菜单中,右键单击节点上的弹出菜单中),具体取决于当前节点/节点状态。

@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();
        }
    }
}

例如:默认情况下,此操作仅在选择了一个节点时启用,否则将被禁用(可见但禁用)。我想要相同的行为,但也要基于所选节点的状态。

我的所有节点都实现了我的界面:

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

所以我可以通过以下某种方式获取节点的状态:

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

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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案here

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);
}