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