Malthan
Malthan

Reputation: 7695

How to invoke an existing IDE action in IntelliJ?

I've written a plugin that adds a group to the "new file" menu, and I want my action to launch a specific file template. From what I understand the action is called "NewFromTemplate", but I've encoutered two problems:

  1. How to invoke an already existing action, in this case NewFromTemplate?

  2. How to pass arguments to it, since I want it to create a file from a specific template.

The best I've come up with it is:

ActionManager am = ActionManager.getInstance();
AnAction ftAction = am.getAction("NewFromTemplate");
ActionManager.getInstance().tryToExecute(
        ftAction, 
        ActionCommand.getInputEvent("NewFromTemplate"), 
        null, 
        ActionPlaces.UNKNOWN, 
        true);

But it doesn't seem to open anything - the code executes but no new window is opened.

Upvotes: 3

Views: 721

Answers (1)

Malthan
Malthan

Reputation: 7695

This is how I've done it (just an example using a hardcoded Template name to show the required methods)

public class FooAction extends AnAction {

public void actionPerformed(AnActionEvent e) {
    FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance();
    FileTemplate[] templates = fileTemplateManager.getAllTemplates();

    for(FileTemplate ft : templates){

        if(ft.getName().equals("Singleton")){
            AnAction action = new CreateFromTemplateAction(ft);
            action.actionPerformed(e);
        }


    }

}

}

Upvotes: 4

Related Questions