Search in sources :

Example 1 with EditFileTemplatesAction

use of com.intellij.ide.actions.EditFileTemplatesAction 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)

Aggregations

EditFileTemplatesAction (com.intellij.ide.actions.EditFileTemplatesAction)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 Project (com.intellij.openapi.project.Project)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1