Triggering AnAction instances.

Hello,

Recently, I've started working on Smardec's IdeaMouseGestures plugin. For those who don't know this plugin, basically what it does is enable the user to map some mouse gestures to specific actions within IntelliJ IDEA.

The technique formerly used to list the available actions was to dynamically discover them by "looking" at the menu items of IDEA's main menu bar.

Then, upon a recognised mouse gesture, those actions were (and still are) triggered by a doClick on the corresponding menu item.

What I did to populate the action tree was to use the ActionManager and have recursively give me the available AnAction instances starting from IdeaActions.GROUP_MAIN_MENU.

What I would like to do now is be able to trigger those AnActions programatically by calling their actionPerformed method, in order to avoid having to use the MenuItem.doClick() trick. The problem is that I don't really know what to put in the AnActionEvent instance I have to pass as parameter. So far, my attempts have failed.

Thanks in advance for any help.

Pierre.

8 comments
Comment actions Permalink

Considering the overwhelming number of responses, I might have to clarify a little bit : I am as much interested in a positive response to the problem as in a negative one (such as : this is not possible).

Pierre.

0
Comment actions Permalink

Hello Pierre,

What I would like to do now is be able to trigger those AnActions
programatically by calling their actionPerformed method, in order to
avoid having to use the MenuItem.doClick() trick. The problem is that
I don't really know what to put in the AnActionEvent instance I have
to pass as parameter. So far, my attempts have failed.


You can find a sample code snippet which triggers AnAction programmatically
in the CheckboxAction.createCustomComponent() method in OpenAPI sources.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Dmitry,

thank you very much for the tip.

Pierre.

0
Comment actions Permalink

Dmitry,

I tested the solution you gave me. As I didn't have any equivalent to the myCheckBox instance used in the example you gave me (in my case, actions are triggered by a mouse gesture so there really is no component instance I can rely upon), I used the default no-parameter method (DataManager.getInstance().getDataContext()) which uses the focused component instead. As far as I can tell from the tests I did, it works like a charm.

Thanks again.

Pierre.

0
Comment actions Permalink

Hello David,

Is this source still available in the latest API? I am not able to
find it.


Yes, this is still available. The class is in the com.intellij.openapi.actionSystem.ex
package.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Reading the source code of AnActionEvent there is a comment, that nobody should create AnActionEvent directly. If this is true, how can you ever trigger an action programmatically?

0
Comment actions Permalink

Hello Alexander,

This comment is not quite true. For triggering actions programmatically,
yo do have to create the instances manually.

Reading the source code of AnActionEvent there is a comment, that
nobody should create AnActionEvent directly. If this is true, how can
you ever trigger an action programmatically?


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.