Question about FileTemplateManager

Hi. Because of I can't find JavaDoc for idea.jar, I have troubles with creating file templates from my code.

So.
I have some Project class, wich implements ProjectComponent:

public class SomeTestProject implements ProjectComponent {
...
}

Next, in "public void initComponent()" I implement adding new template:

File templateFile = new File(SomeTestProject.class.getResource("SomeTest.ft").getPath());
FileInputStream fis = new FileInputStream(templateFile);
byte[] bytes = new byte[(int) templateFile.length()];
fis.read(bytes);
fis.close();

String templateContent = new String(bytes);
FileTemplate ft = FileTemplateManager.getInstance().getTemplate("SomeTest");
if (ft == null) {
ft = FileTemplateManager.getInstance().addTemplate("SomeTest", "SomeTest.ft");
ft.setExtension("java");
ft.setText(templateContent);
ft.setAdjust(true);
FileTemplateManager.getInstance().saveAll(); <--- this code throws exception!
}

When I starting test of this plugin, IDEA on start throw such error:

Assertion failed:
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:89)
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:96)
at com.intellij.ide.fileTemplates.impl.FileTemplateImpl.writeExternal(FileTemplateImpl.java:41)
at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.c(FileTemplateManagerImpl.java:92)
at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.saveAll(FileTemplateManagerImpl.java:145)
at ru.andremoniy.plugins.idea.sometest.SomeTestProject .addTemplate(SomeTestProject .java:178)
at ru.andremoniy.plugins.idea.sometest.SomeTestProject .initComponent(SomeTestProject .java:58)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:68)
at com.intellij.openapi.components.impl.ComponentManagerImpl.access$1100(ComponentManagerImpl.java:112)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter$1.getComponentInstance(ComponentManagerImpl.java:5)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter.getComponentInstance(ComponentManagerImpl.java:6)
at com.intellij.util.pico.DefaultPicoContainer.getLocalInstance(DefaultPicoContainer.java:199)
at com.intellij.util.pico.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:186)
at com.intellij.util.pico.DefaultPicoContainer.getComponentInstance(DefaultPicoContainer.java:168)
at org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer.getComponentInstance(AbstractDelegatingMutablePicoContainer.java:75)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:178)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:12)
at com.intellij.openapi.components.impl.ComponentManagerImpl.initComponents(ComponentManagerImpl.java:162)
at com.intellij.openapi.components.impl.ComponentManagerImpl.init(ComponentManagerImpl.java:74)
at com.intellij.openapi.project.impl.ProjectImpl.init(ProjectImpl.java:100)
at com.intellij.openapi.components.impl.stores.ProjectStoreImpl.loadProject(ProjectStoreImpl.java:29)
at com.intellij.openapi.project.impl.ProjectManagerImpl.a(ProjectManagerImpl.java:7)
at com.intellij.openapi.project.impl.ProjectManagerImpl.access$200(ProjectManagerImpl.java:231)
at com.intellij.openapi.project.impl.ProjectManagerImpl$4.run(ProjectManagerImpl.java:21)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:8)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:79)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:50)
at com.intellij.openapi.application.impl.ApplicationImpl$7$1.run(ApplicationImpl.java:8)
at com.intellij.openapi.application.impl.ApplicationImpl$5.run(ApplicationImpl.java:9)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:1)



java.lang.NullPointerException
at com.intellij.ide.fileTemplates.impl.FileTemplateImpl.writeExternal(FileTemplateImpl.java:152)
at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.c(FileTemplateManagerImpl.java:92)
at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.saveAll(FileTemplateManagerImpl.java:145)
at ru.andremoniy.plugins.idea.sometest.SomeTestProject.addTemplate(SomeTestProject.java:178)
at ru.andremoniy.plugins.idea.sometest.SomeTestProject.initComponent(SomeTestProject.java:58)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:68)
at com.intellij.openapi.components.impl.ComponentManagerImpl.access$1100(ComponentManagerImpl.java:112)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter$1.getComponentInstance(ComponentManagerImpl.java:5)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter.getComponentInstance(ComponentManagerImpl.java:6)
at com.intellij.util.pico.DefaultPicoContainer.getLocalInstance(DefaultPicoContainer.java:199)
at com.intellij.util.pico.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:186)
at com.intellij.util.pico.DefaultPicoContainer.getComponentInstance(DefaultPicoContainer.java:168)
at org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer.getComponentInstance(AbstractDelegatingMutablePicoContainer.java:75)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:178)
at com.intellij.openapi.components.impl.ComponentManagerImpl.a(ComponentManagerImpl.java:12)
at com.intellij.openapi.components.impl.ComponentManagerImpl.initComponents(ComponentManagerImpl.java:162)
at com.intellij.openapi.components.impl.ComponentManagerImpl.init(ComponentManagerImpl.java:74)
at com.intellij.openapi.project.impl.ProjectImpl.init(ProjectImpl.java:100)
at com.intellij.openapi.components.impl.stores.ProjectStoreImpl.loadProject(ProjectStoreImpl.java:29)
at com.intellij.openapi.project.impl.ProjectManagerImpl.a(ProjectManagerImpl.java:7)
at com.intellij.openapi.project.impl.ProjectManagerImpl.access$200(ProjectManagerImpl.java:231)
at com.intellij.openapi.project.impl.ProjectManagerImpl$4.run(ProjectManagerImpl.java:21)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:8)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:79)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:50)
at com.intellij.openapi.application.impl.ApplicationImpl$7$1.run(ApplicationImpl.java:8)
at com.intellij.openapi.application.impl.ApplicationImpl$5.run(ApplicationImpl.java:9)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:1)


The most strange is the fact, that this template adds to template list and it can be found in "File Template".

2 comments
Comment actions Permalink

I don't know why your getting that exception, but why are you registering file templates like that ?

I think the correct way to register file templates is to provide a class that implements FileTemplateGroupDescriptorFactory.
In the getFileTemplatesDescriptor() you should have something like:

FileTemplateGroupDescriptor root = new FileTemplateGroupDescriptor("myGroup", myGroupIcon);
root.addTemplate(new FileTemplateDescriptor("My Template Name", StdFileTypes.JAVA.getIcon()));
return root;

0
Comment actions Permalink

Ok, I'm sorry for my this question, but I found the decision. It is:

Adding new template must be called after adding any toolwindows (I mean "toolWindowManager.registerToolWindow"... in other case it will throw Exception with no message (why?)). So, that is why I placed code block with adding new template in "projectOpened()" method:

FileTemplate ft = FileTemplateManager.getInstance().getTemplate("Test");
if (ft == null) {
ft = FileTemplateManager.getInstance().addTemplate("Test", "java");
ft.setText(templateContent);
ft.setAdjust(true);
FileTemplateManager.getInstance().saveAll();
}

FileTemplateManager.getInstance().addTemplate() has such description, as I understood:

// templateName - name of the template
// templateExtension - extension of the template
FileTemplateManager.getInstance().addTemplate(String templateName, String templateExtension);

any other usage of this params (I mean incorrect filling) will throw to strange exceptions, as I wrote in my question.

0

Please sign in to leave a comment.