X

Geertjan's Blog

  • December 18, 2011

Calling Back to NetBeans Platform Actions

Geertjan Wielenga
Product Manager

When registering custom Actions for Cut and Copy, this is the code to use in NetBeans Platform 7.1:

ActionMap actionMap = getActionMap();
actionMap.put("copy-to-clipboard",
        FileUtil.getConfigObject(
        "Actions/Customer/org-shop-editor-MyCustomCopyAction.instance", Action.class));
actionMap.put("cut-to-clipboard",
        FileUtil.getConfigObject(
        "Actions/Customer/org-shop-editor-MyCustomCutAction.instance", Action.class));
associateLookup(new ProxyLookup(
        ExplorerUtils.createLookup(em, actionMap),
        new AbstractLookup(instanceContent)));

However, what about NetBeans Platform actions for which there are no standard macros? Today I discovered that you can, at least in the instances below, use the "id" of the NetBeans Platform Action class as your key, where the Action class must be a CallbackSystemAction, as shown below for the FindAction and the ReplaceAction:

ActionMap actionMap = getActionMap();
actionMap.put("org.openide.actions.FindAction",
        FileUtil.getConfigObject(
        "Actions/Customer/org-shop-editor-MyCustomFindAction.instance", Action.class));
actionMap.put("org.openide.actions.ReplaceAction",
        FileUtil.getConfigObject(
        "Actions/Customer/org-shop-editor-MyCustomReplaceAction.instance", Action.class));
associateLookup(new ProxyLookup(
        ExplorerUtils.createLookup(em, actionMap),
        new AbstractLookup(instanceContent)));

Now, when you look in the Edit menu or Edit toolbar, you'll see that your Find and Replace actions are enabled and will do whatever is defined in your custom action.

Join the discussion

Comments ( 6 )
  • guest Tuesday, December 20, 2011

    I tried your example using MyAction which implements the DeleteAction. DeleteAction has a macro defined so I connected it using "delete" in the ActionMap.

    In this case how should MyAction be declared if it's a context aware action ?

    Mine is declared using annotations:

    @ActionID(category = "Edit", id = "org.MyAction")

    @ActionRegistration(displayName = "My Action")

    public final class MyAction implements ActionListener

    {

    private final List<MyObject> context;

    public MyAction (List<MyObject> context)

    {

    this.context = context;

    }

    @Override

    public void actionPerformed(ActionEvent ev)

    {

    // Do nothing

    }

    }

    I can see in the menu the Delete action gets enabled/disabled OK according to selection, but when I execute it (actionPerformed does nothing), I get a strange Exception

    SEVERE [org.openide.util.RequestProcessor]: Error in RequestProcessor org.openide.util.actions.ActionInvoker$2

    java.lang.AssertionError

    at org.openide.awt.GeneralAction$BaseDelAction.actionPerformed(GeneralAction.java:231)

    Which is Caused: org.openide.util.RequestProcessor$SlowItem: task failed due to...

    Any idea ?


  • cg Thursday, June 9, 2016

    Hi Geertjan --

    I'm working on functional-test projects where TestNG tests live on the "Source Packages" side of the project instead of in "Test Packages" where you would normally find them.

    The test actions in Netbeans, "Test File", and "Run Focused Test File" (grayed-out) will not execute these tests from the IDE. I'm forced to run the test projects via Maven on the CLI. Apparently, IntelliJ can do this, and I am being encouraged strongly to switch IDEs for my current work.

    Do you have any advice on getting tests in "Source" to behave and respond in the IDE like they live in "Tests"?

    thanks for your help,

    cg


  • Geertjan Thursday, June 9, 2016

    Unclear what this has to do with this blog entry. Why not join the NetBeans mailing lists like everyone else who has questions about NetBeans IDE?


  • Geertjan Thursday, June 9, 2016

    PS: For the record, test files, regardless of whether they're functional or not, do not live in Source Packages, but in Test Packages.


  • cg Thursday, June 9, 2016

    Yup, I know ... but ... out of my control.

    I'll dig through the fora to see if this has ever come up. Single-method tests on the test side of projects of course works great.

    Thanks, Geertjan.

    cg


  • Geertjan Thursday, June 9, 2016

    It's beyond your control to put test files in the correct place in your project? No, there's no need to dig around anywhere, since that doesn't make sense at all.


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