An Action class that also implements ProjectComponent?
I want to implement an action that displays some project specific information by default. I have inferred from the community posts that the right way to do it is by implementing and registering a ProjectComponent. So can I make AnAction a ProjectComponent or not? Or is there a better way to do that?
I already know that I can get the project instance from AnActionEvent but that would happen only on interaction with the presentation. I want to be able to initialize the presentation with information from the project.
Please sign in to leave a comment.
No, usually plugins shouldn't register ProjectComponents at all, see its javadoc for details.
And even if you register an action class as a ProjectComponent this won't properly, because the platform will create a separate instance of AnAction for each opened project and a separate instance which will be used by the action system.
What do you mean by 'displays some project specific information by default'? How this information will be displayed? Will you change the action text, its tooltip or something else?
My action presentation consists of a dropdown menu, much like a "Run Configuration" dropdown. I intend to have a list of all installable modules generated by my project displayed in the toolbar.
I am beginning to realize that that might be against the plugin design principles, which is why its not very straightforward. After a lot of failed attempts, I think I might need to use custom run configurations to achieve what I need. Although I have no idea how to do that at this point.
Dropdown menus are represented by com.intellij.openapi.actionSystem.ActionGroup. Its 'getChildren' method has 'AnActionEvent' parameter so you can obtain the project from it and dynamically generate AnAction instances.