Calling Back to NetBeans Platform Actions

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.

Comments:

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 ?

Posted by guest on December 20, 2011 at 12:14 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
19
21
22
23
24
25
26
27
28
29
30
   
       
Today