简体   繁体   English

Eclipse插件-通过模板以编程方式创建文件

[英]Eclipse plugin - Programatically create file from template

I have a Template as org.eclipse.jface.text.templates.Template: 我有一个模板作为org.eclipse.jface.text.templates.Template:

Template template = new Template(name, description, contextTypeId, pattern, true);

or better the org.eclipse.cdt.core.model.Template: 或更好的org.eclipse.cdt.core.model.Template:

Template template = new Template(templateString);

Or let's say that the template already is in the Preferences -> C/C++ -> Code Style -> Code Templates and I can get it directly from there. 或者说模板已经位于“首选项”->“ C / C ++”->“代码样式”->“代码模板”中,我可以直接从那里获取它。

Now I want to create a file from it like how you do in the wizard but programmatically: 现在,我想像从向导中那样通过该文件创建一个文件,但以编程方式:

    IFile mainTestFile = testFolder.getFile(resource.getName() + ".myfile");
    if (!mainTestFile.exists()) {
        mainTestFile.create(template, true, null);
    }

Unfortunately it only works for InputStream not for Template. 不幸的是,它仅适用于InputStream而不适用于Template。

So how can I programmatically create a file in an eclipse project from a template I have? 那么,如何从已有的模板中以编程方式在Eclipse项目中创建文件?

Ok so the way I did it might seem a bit counter intuitive but it worked: 好的,所以我这样做的方式似乎有点反直觉,但确实有效:

First you need a class that extends the CDT NewSourceFileCreationWizardPage. 首先,您需要一个扩展CDT NewSourceFileCreationWizardPage的类。 Then overload the createFile method to use a org.eclipse.jface.text.templates.Template that you define yourself. 然后重载createFile方法以使用您自己定义的org.eclipse.jface.text.templates.Template。 The template code will be sotred in a string which can be obtained from a resource file. 模板代码将以字符串形式存储,该字符串可以从资源文件中获取。

        Template myTemplate = new Template("My Template", "My Template for file", "com.my.template.contenttype.contenttype_context", templateString, true);

        MyNewFileWizardPage myTemplateWizard = new MyNewFileWizardPage ();

        mainTestFile = myTemplateWizard.createMyFile(myTemplate, mainTestFile.getLocation(), null);

And the custom new file wizard page: 和自定义新文件向导页面:

class MyNewFileWizardPage extends NewSourceFileCreationWizardPage{

   private ITranslationUnit fNewFileTU = null;

   @SuppressWarnings("restriction")
   public IFile createMyFile(Template template, IPath filePath, IProgressMonitor monitor) throws CoreException {
    if (filePath != null) {
        if (monitor == null)
            monitor = new NullProgressMonitor();
        try {
            fNewFileTU = null;
            IFile newFile = NewSourceFileGenerator.createSourceFile(filePath, true, monitor);
            if (newFile != null) {
                fNewFileTU = (ITranslationUnit) CoreModel.getDefault().create(newFile);
                if (fNewFileTU != null) {
                    String lineDelimiter = StubUtility.getLineDelimiterUsed(fNewFileTU);
                    String content = CodeGeneration.getBodyFileContent(template, fNewFileTU, null, null, lineDelimiter);
                    if (content != null) {
                        fNewFileTU.getBuffer().setContents(content.toCharArray());
                        fNewFileTU.save(monitor, true);
                        return newFile;
                    }
                }
            }
        } finally {
            monitor.done();
        }
    }
    return null;
  }
}

Then with the created IFile you can modify it further if you want anything changed in it. 然后,如果您希望对其进行任何更改,则可以使用创建的IFile对其进行进一步修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM