Question

I am trying to implement an action that is enabled or disabled (in the application top menu, and in popup menu on right click on node) depending on the current node/nodes state.

@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). I want the same behaviour, but also based on the selected node's state.

All my nodes implements my interface:

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

so i can get the node's state in my action in some way like this:

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

How can I use the previous fragment of code to enabling/disabling the action when i select the node, in the same way in wich this action is disabled when multiple nodes are selected?

Any suggestions?

Was it helpful?

Solution

I've found a solution 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);
    }
}

It works! But i think that should be better to put the method isEnabled() in the Capability and not in Node, because is the Capability that have the property being enabled:

public void resultChanged(LookupEvent ev) {
    for(ShowAuditEventsCapability capability : lkpInfo.allInstances()) {
        if(!capability.isEnabled()) {
            setEnabled(false);
            return;
        }
    }
    setEnabled(true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top