X

Geertjan's Blog

  • June 30, 2011

Change a Foreign Action's Display Text

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 6 )
  • Jesse Glick Thursday, June 30, 2011

    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.


  • Geertjan Thursday, June 30, 2011

    You mean NbBundle.setBranding should suffice? Or what exactly? I.e., how would the above be done via regular branding?


  • Matteo Di Giovinazzo Friday, July 1, 2011

    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>


  • David Strupl Friday, July 1, 2011

    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


  • Toni Friday, July 1, 2011

    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.


  • Geertjan Friday, July 1, 2011

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.