Search in sources :

Example 31 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project android by JetBrains.

the class AndroidStudioInitializer method setUpNewProjectActions.

private static void setUpNewProjectActions() {
    replaceAction("NewClass", new CreateClassAction());
    // Update the text for the file creation templates.
    FileTemplateManager fileTemplateManager = FileTemplateManager.getDefaultInstance();
    fileTemplateManager.getTemplate("Singleton").setText(fileTemplateManager.getJ2eeTemplate("Singleton").getText());
    for (String templateName : new String[] { "Class", "Interface", "Enum", "AnnotationType" }) {
        FileTemplate template = fileTemplateManager.getInternalTemplate(templateName);
        template.setText(fileTemplateManager.getJ2eeTemplate(templateName).getText());
    }
}
Also used : CreateClassAction(com.android.tools.idea.actions.CreateClassAction) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager)

Example 32 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class CreateHtmlDescriptionFix method createDescription.

private void createDescription(VirtualFile root) {
    if (!root.isDirectory())
        return;
    final PsiManager psiManager = PsiManager.getInstance(myModule.getProject());
    final PsiDirectory psiRoot = psiManager.findDirectory(root);
    if (psiRoot == null)
        return;
    PsiDirectory descrRoot = StreamEx.of(psiRoot.getSubdirectories()).findFirst(dir -> getDescriptionFolderName().equals(dir.getName())).orElse(null);
    try {
        descrRoot = descrRoot == null ? psiRoot.createSubdirectory(getDescriptionFolderName()) : descrRoot;
        if (isFixedDescriptionFilename()) {
            PsiDirectory dir = descrRoot.findSubdirectory(myFilename);
            if (dir == null) {
                descrRoot = descrRoot.createSubdirectory(myFilename);
            }
        }
        final FileTemplate descrTemplate = FileTemplateManager.getInstance(myModule.getProject()).getJ2eeTemplate(TEMPLATE_NAME);
        final PsiElement template = FileTemplateUtil.createFromTemplate(descrTemplate, getNewFileName(), null, descrRoot);
        if (template instanceof PsiFile) {
            final VirtualFile file = ((PsiFile) template).getVirtualFile();
            if (file != null) {
                FileEditorManager.getInstance(myModule.getProject()).openFile(file, true);
            }
        }
    } catch (Exception e) {
    //
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Arrays(java.util.Arrays) AllIcons(com.intellij.icons.AllIcons) ArrayUtil(com.intellij.util.ArrayUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) DescriptionCheckerUtil(org.jetbrains.idea.devkit.inspections.DescriptionCheckerUtil) PsiManager(com.intellij.psi.PsiManager) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) JBList(com.intellij.ui.components.JBList) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) JavaResourceRootType(org.jetbrains.jps.model.java.JavaResourceRootType) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) DescriptionType(org.jetbrains.idea.devkit.inspections.DescriptionType) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Iconable(com.intellij.openapi.util.Iconable) File(java.io.File) DevKitBundle(org.jetbrains.idea.devkit.DevKitBundle) List(java.util.List) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) StreamEx(one.util.streamex.StreamEx) FileTemplateUtil(com.intellij.ide.fileTemplates.FileTemplateUtil) LayeredIcon(com.intellij.ui.LayeredIcon) ApplicationManager(com.intellij.openapi.application.ApplicationManager) PsiDirectory(com.intellij.psi.PsiDirectory) NotNull(org.jetbrains.annotations.NotNull) JavaModuleSourceRootTypes(org.jetbrains.jps.model.java.JavaModuleSourceRootTypes) javax.swing(javax.swing) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 33 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class CreateFromTemplateGroup method getChildren.

@Override
@NotNull
public AnAction[] getChildren(@Nullable AnActionEvent e) {
    if (e == null)
        return EMPTY_ARRAY;
    Project project = e.getProject();
    if (project == null || project.isDisposed())
        return EMPTY_ARRAY;
    FileTemplateManager manager = FileTemplateManager.getInstance(project);
    FileTemplate[] templates = manager.getAllTemplates();
    boolean showAll = templates.length <= FileTemplateManager.RECENT_TEMPLATES_SIZE;
    if (!showAll) {
        Collection<String> recentNames = manager.getRecentNames();
        templates = new FileTemplate[recentNames.size()];
        int i = 0;
        for (String name : recentNames) {
            templates[i] = manager.getTemplate(name);
            i++;
        }
    }
    Arrays.sort(templates, (template1, template2) -> {
        // java first
        if (template1.isTemplateOfType(StdFileTypes.JAVA) && !template2.isTemplateOfType(StdFileTypes.JAVA)) {
            return -1;
        }
        if (template2.isTemplateOfType(StdFileTypes.JAVA) && !template1.isTemplateOfType(StdFileTypes.JAVA)) {
            return 1;
        }
        // group by type
        int i = template1.getExtension().compareTo(template2.getExtension());
        if (i != 0) {
            return i;
        }
        // group by name if same type
        return template1.getName().compareTo(template2.getName());
    });
    List<AnAction> result = new ArrayList<>();
    for (FileTemplate template : templates) {
        if (canCreateFromTemplate(e, template)) {
            AnAction action = replaceAction(template);
            if (action == null) {
                action = new CreateFromTemplateAction(template);
            }
            result.add(action);
        }
    }
    if (!result.isEmpty() || !showAll) {
        if (!showAll) {
            result.add(new CreateFromTemplatesAction(IdeBundle.message("action.from.file.template")));
        }
        result.add(Separator.getInstance());
        result.add(new EditFileTemplatesAction(IdeBundle.message("action.edit.file.templates")));
    }
    return result.toArray(new AnAction[result.size()]);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) EditFileTemplatesAction(com.intellij.ide.actions.EditFileTemplatesAction) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class SelectTemplateDialog method loadCombo.

private void loadCombo() {
    DefaultComboBoxModel model = new DefaultComboBoxModel();
    FileTemplate[] allTemplates = FileTemplateManager.getInstance(myProject).getAllTemplates();
    PsiDirectory[] dirs = { myDirectory };
    for (FileTemplate template : allTemplates) {
        if (FileTemplateUtil.canCreateFromTemplate(dirs, template)) {
            model.addElement(template);
        }
    }
    if (myCbxTemplates == null) {
        myCbxTemplates = new JComboBox(model);
        myCbxTemplates.setRenderer(new ListCellRendererWrapper<FileTemplate>() {

            @Override
            public void customize(JList list, FileTemplate fileTemplate, int index, boolean selected, boolean hasFocus) {
                if (fileTemplate != null) {
                    setIcon(FileTemplateUtil.getIcon(fileTemplate));
                    setText(fileTemplate.getName());
                }
            }
        });
    } else {
        Object selected = myCbxTemplates.getSelectedItem();
        myCbxTemplates.setModel(model);
        if (selected != null) {
            myCbxTemplates.setSelectedItem(selected);
        }
    }
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 35 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class TemplateModuleBuilder method processTemplates.

@SuppressWarnings("UseOfPropertiesAsHashtable")
@Nullable
private byte[] processTemplates(@Nullable String projectName, String content, File file, Consumer<VelocityException> exceptionConsumer) throws IOException {
    String patchedContent = content;
    if (!(myTemplate instanceof LocalArchivedTemplate) || ((LocalArchivedTemplate) myTemplate).isEscaped()) {
        for (WizardInputField field : myAdditionalFields) {
            if (!field.acceptFile(file)) {
                return null;
            }
        }
        Properties properties = FileTemplateManager.getDefaultInstance().getDefaultProperties();
        for (WizardInputField field : myAdditionalFields) {
            properties.putAll(field.getValues());
        }
        if (projectName != null) {
            properties.put(ProjectTemplateParameterFactory.IJ_PROJECT_NAME, projectName);
        }
        String merged = FileTemplateUtil.mergeTemplate(properties, content, true, exceptionConsumer);
        patchedContent = merged.replace("\\$", "$").replace("\\#", "#");
    } else {
        int i = content.indexOf(SaveProjectAsTemplateAction.FILE_HEADER_TEMPLATE_PLACEHOLDER);
        if (i != -1) {
            final FileTemplate template = FileTemplateManager.getDefaultInstance().getDefaultTemplate(SaveProjectAsTemplateAction.getFileHeaderTemplateName());
            Properties properties = FileTemplateManager.getDefaultInstance().getDefaultProperties();
            String templateText = template.getText(properties);
            patchedContent = patchedContent.substring(0, i) + templateText + patchedContent.substring(i + SaveProjectAsTemplateAction.FILE_HEADER_TEMPLATE_PLACEHOLDER.length());
        }
    }
    return StringUtilRt.convertLineSeparators(patchedContent, CodeStyleFacade.getInstance().getLineSeparator()).getBytes(CharsetToolkit.UTF8_CHARSET);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Properties(java.util.Properties) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)66 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)20 Project (com.intellij.openapi.project.Project)13 IOException (java.io.IOException)12 PsiFile (com.intellij.psi.PsiFile)10 Properties (java.util.Properties)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 PsiDirectory (com.intellij.psi.PsiDirectory)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiElement (com.intellij.psi.PsiElement)6 Module (com.intellij.openapi.module.Module)5 File (java.io.File)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 Nullable (org.jetbrains.annotations.Nullable)4 Editor (com.intellij.openapi.editor.Editor)3 ConfigurationException (com.intellij.openapi.options.ConfigurationException)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)2