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.
If you just want to change one static label (or label format) to another, regular branding should suffice.
In the code above, you are failing to delegate isEnabled and getValue to the original Action, which may or may not matter.
You mean NbBundle.setBranding should suffice? Or what exactly? I.e., how would the above be done via regular branding?
Guys, I did not have a chance to try, but would something like this below work to change the name of the action?
@Override
public Action[] getActions(boolean context) {
return Utilities.actionsForPath("Actions/Events2").toArray(new Action[0]);
}
Given this in layer.xml:
<folder name="Action">
<folder name="Events">
<file name="org-mycom-MyAction.instance"/>
</folder>
<folder name="Events2">
<file name="org-mycom-MyAction2.shadow">
<attr name="originalFile"
stringvalue="Actions/Events/org-mycom-MyAction.instance"/>
<!-- this way you can change the name -->
<attr name="SystemFileSystem.localizingBundle"
stringvalue="org.mycom.events2.Bundle"/>
</file>
</folder>
</folder>
What Jesse probably meant is to find out from which bundle the text you wanted to change originates and you just supply the branding bundle file. Please read more about branding here: http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/doc-files/i18n-branding.html
You could simply use the new branding editor added in NB 6.9 to locate the actions text and override it. I couldn't find info ybout it, but I described it in an article:
http://it-republik.de/jaxenter/artikel/NetBeans-6.9---Was-ist-neu-3173.html
You need to be careful though when simply using the search functionality to look for the bundle, because there might be an additional symbol in the text for the shortcut.
Thanks, but I know about the branding editor...
That's really not the point here. What I want is that when I right-click a parent node named "Jack", I want to see an action display text "Edit Jack". But when I right-click on a child node of "Jack" (e.g., "Son of Jack"), I want the display text to say "Edit Son of Jack".
I.e., the same action should have different display texts depending on the current context.