Java Netbeans操作已启用,具体取决于节点';s州

Java Netbeans操作已启用,具体取决于节点';s州,java,action,netbeans-7,netbeans-platform,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 =

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

@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();
在选择节点时,如何使用前面的代码片段启用/禁用操作,就像在选择多个节点时禁用此操作一样

有什么建议吗?

我找到了一个解决方案:

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);
    }
}
public void resultChanged(LookupEvent ev) {
    for(ShowAuditEventsCapability capability : lkpInfo.allInstances()) {
        if(!capability.isEnabled()) {
            setEnabled(false);
            return;
        }
    }
    setEnabled(true);
}