DevFaqHowOrganizeOrReuseExistingActionsWithAnnotations

How can we reuse and organise existing actions in our menus or pop menus? Usually you need to know the original location of the action you want to use and then refer to it as shadow in your layer.xml file. This requires you to create such as file what you want less and less.

Fortunately there is solution: use annotations such as @ActionReference, @ActionReferences and @ActionID. Their usage is obvious when you create your own actions, but how can they be used to refer to existing actions?

Create an empty class that you will include in your module:

  public class ActionsRefs {
  }

Next annotate this class with @ActionReference, @ActionReferences:

  @ActionReferences(value = {
      @ActionReference(id = @ActionID(category = "System", 
                            id =   "org.openide.actions.FileSystemRefreshAction"), 
                       path = "Console/Service/Brokers/Actions/MessageBus", 
                       position = 200)})
  class ActionsRefs {
  }

This class will be used as action reference configuration and the annotation preprocessor will create the proper entries the the generated layer.xml file. Notice the embeded @ActionID? This refers to the existing Action.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo