Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 24

Download

How can I reuse and/or organise actions only by using annotations (@ActionReferences)?

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.