Change a Foreign Action's Display Text

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Thu, 30 Jun 2011 09:01:22 -0700 Indexed on 2011/06/30 16:27 UTC
Read the original article Hit count: 314

Filed under:
I want the display text on an Action on a Node to show something about the underlying object. But the Action is registered somewhere in the layer (i.e., in the registry), i.e., I have no control over it. How do I change the display text in this scenario?

Here's how. Below I look in the Actions/Events folder, iterate through all the Actions registered there, look for an Action with display text starting with "Edit", change it to display something from the underlying object, wrap a new Action around that Action, build up a new list of Actions, and return those (together with all the other Actions in that folder) from "getActions" on my Node:

@Override
public Action[] getActions(boolean context) {
    List<Action> newEventActions = new ArrayList<Action>();
    List<? extends Action> eventActions = Utilities.actionsForPath("Actions/Events");
    for (final Action action : eventActions) {
        String value = action.getValue(Action.NAME).toString();
        if (value.startsWith("Edit")) {
            Action editAction = new AbstractAction("Edit " + getLookup().lookup(Event.class).getPlace()) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    action.actionPerformed(e);
                }
            };
            newEventActions.add(editAction);
        } else {
            newEventActions.add(action);
        }
    }
    return newEventActions.toArray(new Action[eventActions.size()]);
}

If someone knows of a better way, please let me know.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE