Search in sources :

Example 1 with CreateFromTemplateDialog

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

the class CreateFromTemplateActionBase method actionPerformed.

@Override
public final void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null)
        return;
    PsiDirectory dir = getTargetDirectory(dataContext, view);
    if (dir == null)
        return;
    Project project = dir.getProject();
    FileTemplate selectedTemplate = getTemplate(project, dir);
    if (selectedTemplate != null) {
        AnAction action = getReplacedAction(selectedTemplate);
        if (action != null) {
            action.actionPerformed(e);
        } else {
            FileTemplateManager.getInstance(project).addRecentName(selectedTemplate.getName());
            AttributesDefaults defaults = getAttributesDefaults(dataContext);
            Properties properties = defaults != null ? defaults.getDefaultProperties() : null;
            CreateFromTemplateDialog dialog = new CreateFromTemplateDialog(project, dir, selectedTemplate, defaults, properties);
            PsiElement createdElement = dialog.create();
            if (createdElement != null) {
                elementCreated(dialog, createdElement);
                view.selectElement(createdElement);
                if (selectedTemplate.isLiveTemplateEnabled() && createdElement instanceof PsiFile) {
                    Map<String, String> defaultValues = getLiveTemplateDefaults(dataContext, ((PsiFile) createdElement));
                    startLiveTemplate((PsiFile) createdElement, notNull(defaultValues, Collections.emptyMap()));
                }
            }
        }
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IdeView(com.intellij.ide.IdeView) AnAction(com.intellij.openapi.actionSystem.AnAction) CreateFromTemplateDialog(com.intellij.ide.fileTemplates.ui.CreateFromTemplateDialog) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 2 with CreateFromTemplateDialog

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

the class JavaDirectoryServiceImpl method createClassFromTemplate.

private static PsiClass createClassFromTemplate(@NotNull PsiDirectory dir, String name, String templateName, boolean askToDefineVariables, @NotNull Map<String, String> additionalProperties) throws IncorrectOperationException {
    if (askToDefineVariables) {
        LOG.assertTrue(!ApplicationManager.getApplication().isWriteAccessAllowed());
    }
    //checkCreateClassOrInterface(dir, name);
    Project project = dir.getProject();
    FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate(templateName);
    Properties defaultProperties = FileTemplateManager.getInstance(project).getDefaultProperties();
    Properties properties = new Properties(defaultProperties);
    properties.setProperty(FileTemplate.ATTRIBUTE_NAME, name);
    for (Map.Entry<String, String> entry : additionalProperties.entrySet()) {
        properties.setProperty(entry.getKey(), entry.getValue());
    }
    String ext = StdFileTypes.JAVA.getDefaultExtension();
    String fileName = name + "." + ext;
    PsiElement element;
    try {
        element = askToDefineVariables ? new CreateFromTemplateDialog(project, dir, template, null, properties).create() : FileTemplateUtil.createFromTemplate(template, fileName, properties, dir);
    } catch (IncorrectOperationException e) {
        throw e;
    } catch (Exception e) {
        LOG.error(e);
        return null;
    }
    if (element == null)
        return null;
    final PsiJavaFile file = (PsiJavaFile) element.getContainingFile();
    PsiClass[] classes = file.getClasses();
    if (classes.length < 1) {
        throw new IncorrectOperationException(getIncorrectTemplateMessage(templateName, project));
    }
    return classes[0];
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Properties(java.util.Properties) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CreateFromTemplateDialog(com.intellij.ide.fileTemplates.ui.CreateFromTemplateDialog) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Map(java.util.Map)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)2 CreateFromTemplateDialog (com.intellij.ide.fileTemplates.ui.CreateFromTemplateDialog)2 Project (com.intellij.openapi.project.Project)2 IdeView (com.intellij.ide.IdeView)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Map (java.util.Map)1 Properties (java.util.Properties)1