2

I develop an application on the NetBeans Platform (version 8.1). I define an action as the following example:

@ActionID(
    category = "MyCategory",
    id = "my.action.id"
)
@ActionRegistration(
    displayName = "My Action", lazy = false
)
public final class MyAction extends AbstractAction implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
        // Do some works
    }
}

I want to add this action to multiple categories. In other words, I want to access this action from multiple categories. Is it possible without creating another class? For example something like this:

@ActionID(
    category = {"Category1", "Category2"},
    id = "my.action.id"
)
...
...
hadi.mansouri
  • 828
  • 11
  • 25
  • Why do you not want to create another class? You can also just route the action to another method after its done its first job – Joe Jul 07 '19 at 20:39
  • @Joe Only for reusability considerations. I have many actions which will be accessed from more than one place (category). So I want to create one class for each action to better error handling and maintenance. – hadi.mansouri Jul 08 '19 at 03:13

1 Answers1

0

I have many actions which will be accessed from more than one place (category)

If you mean than one action can be called from multiple places in the UI, e.g. the Edit action can be accessed from a menu item AND a toolbar button, then you should use @ActionReferences() in your action:

@ActionID(
     category = "MyCategory",
     id = "my.action.id"
)
@ActionRegistration(
    displayName = "Edit", 
    lazy = false)
@ActionReferences(
{
    @ActionReference(path="Toolbar/Edit", position=300),
    @ActionReference(path="Menu/Edit", position=500),
    @ActionReference(path="Shortcuts", name="C-F2"),
})

The ActionId category and id just define the location of the action reference in your .xml layer file : "Actions/category/id". The category is typically used to group actions belonging to a popupmenu, because Netbeans lets you easily build one using Utilities.actionsForPath("Actions/category) then Utilities.actionsToPopup() with the returned actions.

jjazzboss
  • 1,261
  • 8
  • 14