Passing data to Actions

Hi,

I'm trying to build my first IDEA Plugin so please excuse me, if the question is a bit basic. Maybe someone can help me with this:

I have a Tree. On right click a context menu with an Action should be displayed. To this point everything works fine. Now the Problem: In the called Action, I need access to some data, but I have no idea, how to solve this.

    private void addTreeListener()
    {
        myTree.addMouseListener(new PopupHandler()
        {
            public void invokePopup(Component comp, int x, int y)
            {
               /* No idea, how to pass the data to the action
                DataContext dataContext = DataManager.getInstance().getDataContext(myTree);
                DataManager.getInstance().saveInDataContext(dataContext, CreateBeansAction.DOCTYPE_MODEL, doctypeModel);
                DataManager.getInstance().saveInDataContext(dataContext, CreateBeansAction.DOCTYPE, getSelectedDoctype());
                 */
                AnAction action = ActionManager.getInstance().getAction("com.monday.cmidea.CreateBeansAction");

                DefaultActionGroup group = new DefaultActionGroup();
                group.add(action);

                ActionPopupMenu menu = ActionManager.getInstance().createActionPopupMenu(ActionPlaces.UNKNOWN, group);
                menu.getComponent().show(comp, x, y);
            }
        });
    }

Thanks in advance!
Jan

3 comments
Comment actions Permalink

Hi Jan,

1. It's possible to automate context menu actions processing by defining an action group with target actions at your plugin xml and using ide helper functions:

 
<action id="MyActionId"
        class="MyActionClass"
        icon="MyIcon"/>
<group id="My.Tree.Context">
  <reference id="MyActionId"/>
</group>


CustomizationUtil.installPopupHandler(<your tree ref>, "My.Tree.Context", "My.Tree.Place");'

2. Your actions should retrieve target info from the given DataContext object. Your tree should implement com.intellij.openapi.actionSystem.DataProvider then

Denis
1
Comment actions Permalink

Thank you very much! I refactored the Tree in an own class to make your suggested changes.

Now everything works. Thanks!

0
Comment actions Permalink

You are welcome

0

Please sign in to leave a comment.