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:
How to invoke an already existing action, in this case NewFromTemplate?
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.
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);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With