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()));
}
}
}
}
}
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];
}
Aggregations