Java Code Examples for com.intellij.ide.fileTemplates.FileTemplateManager#getDefaultProperties()

The following examples show how to use com.intellij.ide.fileTemplates.FileTemplateManager#getDefaultProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BashTemplatesFactory.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@NotNull
static PsiFile createFromTemplate(final PsiDirectory directory, String fileName, String templateName) throws IncorrectOperationException {
    Project project = directory.getProject();
    FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
    FileTemplate template = templateManager.getInternalTemplate(templateName);

    Properties properties = new Properties(templateManager.getDefaultProperties());

    String templateText;
    try {
        templateText = template.getText(properties);
    } catch (IOException e) {
        throw new RuntimeException("Unable to load template for " + templateManager.internalTemplateToSubject(templateName), e);
    }

    PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(fileName, BashFileType.BASH_FILE_TYPE, templateText);
    return (PsiFile) directory.add(file);
}
 
Example 2
Source File: AsposeMavenUtil.java    From Aspose.OCR-for-Java with MIT License 5 votes vote down vote up
private static void runOrApplyFileTemplate(Project project,
                                           VirtualFile file,
                                           String templateName,
                                           Properties properties) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    allProperties.putAll(properties);
    String text = fileTemplate.getText(allProperties);
    Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer builder = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(builder, "\\$" + matcher.group(1).toUpperCase() + "\\$");
    }
    matcher.appendTail(builder);
    text = builder.toString();

    TemplateImpl template = (TemplateImpl) TemplateManager.getInstance(project).createTemplate("", "", text);
    for (int i = 0; i < template.getSegmentsCount(); i++) {
        if (i == template.getEndSegmentNumber()) continue;
        String name = template.getSegmentName(i);
        String value = "\"" + properties.getProperty(name, "") + "\"";
        template.addVariable(name, value, value, true);
    }

    VfsUtil.saveText(file, template.getTemplateText());

    PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }

}
 
Example 3
Source File: AbstractCreateElementActionBase.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
protected static PsiFile createFromTemplateInternal( @NotNull PsiDirectory directory,
                                                     @NotNull String name,
                                                     @NotNull String fileName,
                                                     @NotNull String templateName,
                                                     @NonNls String... parameters )
    throws IncorrectOperationException
{
    // Load template
    FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance();
    FileTemplate template = fileTemplateManager.getJ2eeTemplate( templateName );

    // Process template properties
    Properties properties = new Properties( fileTemplateManager.getDefaultProperties() );
    JavaTemplateUtil.setPackageNameAttribute( properties, directory );
    properties.setProperty( NAME_TEMPLATE_PROPERTY, name );

    // Add parameters
    for( int i = 0; i < parameters.length; i += 2 )
    {
        properties.setProperty( parameters[ i ], parameters[ i + 1 ] );
    }

    // Create text from template with specified properties
    String text;
    try
    {
        text = template.getText( properties );
    }
    catch( Exception e )
    {
        String message = "Unable to load template for " +
                         fileTemplateManager.internalTemplateToSubject( templateName );
        throw new RuntimeException( message, e );
    }

    // Serialized text to file
    PsiManager psiManager = PsiManager.getInstance( directory.getProject() );
    PsiFileFactory fileFactory = PsiFileFactory.getInstance( directory.getProject() );
    PsiFile file = fileFactory.createFileFromText( fileName, text );

    // Reformat the file according to project/default style
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance( psiManager );
    codeStyleManager.reformat( file );

    // Add newly created file to directory
    return (PsiFile) directory.add( file );
}