use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-plugins by JetBrains.
the class StrutsFrameworkSupportProvider method onFacetCreated.
@Override
protected void onFacetCreated(final StrutsFacet strutsFacet, final ModifiableRootModel modifiableRootModel, final FrameworkVersion version) {
final Module module = strutsFacet.getModule();
StartupManager.getInstance(module.getProject()).runWhenProjectIsInitialized(() -> DumbService.getInstance(module.getProject()).runWhenSmart(() -> {
final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
if (sourceRoots.length <= 0) {
return;
}
final PsiDirectory directory = PsiManager.getInstance(module.getProject()).findDirectory(sourceRoots[0]);
if (directory == null || directory.findFile(StrutsConstants.STRUTS_XML_DEFAULT_FILENAME) != null) {
return;
}
final StrutsFileTemplateProvider templateProvider = new StrutsFileTemplateProvider(module);
final FileTemplate strutsXmlTemplate = templateProvider.determineFileTemplate(directory.getProject());
try {
final StrutsFacetConfiguration strutsFacetConfiguration = strutsFacet.getConfiguration();
// create empty struts.xml & fileset with all found struts-*.xml files (struts2.jar, plugins)
final PsiElement psiElement = FileTemplateUtil.createFromTemplate(strutsXmlTemplate, StrutsConstants.STRUTS_XML_DEFAULT_FILENAME, null, directory);
final Set<StrutsFileSet> empty = Collections.emptySet();
final StrutsFileSet fileSet = new StrutsFileSet(StrutsFileSet.getUniqueId(empty), StrutsFileSet.getUniqueName("Default File Set", empty), strutsFacetConfiguration);
fileSet.addFile(((XmlFile) psiElement).getVirtualFile());
final StrutsConfigsSearcher searcher = new StrutsConfigsSearcher(module);
DumbService.getInstance(module.getProject()).runWhenSmart(() -> searcher.search());
final MultiMap<VirtualFile, PsiFile> jarConfigFiles = searcher.getJars();
for (final VirtualFile virtualFile : jarConfigFiles.keySet()) {
final Collection<PsiFile> psiFiles = jarConfigFiles.get(virtualFile);
for (final PsiFile psiFile : psiFiles) {
fileSet.addFile(psiFile.getVirtualFile());
}
}
strutsFacetConfiguration.getFileSets().add(fileSet);
// create filter & mapping in web.xml (if present)
new WriteCommandAction.Simple(modifiableRootModel.getProject()) {
protected void run() throws Throwable {
final WebFacet webFacet = strutsFacet.getWebFacet();
final ConfigFile configFile = webFacet.getWebXmlDescriptor();
if (configFile == null)
return;
final XmlFile webXmlFile = configFile.getXmlFile();
final WebApp webApp = JamCommonUtil.getRootElement(webXmlFile, WebApp.class, null);
if (webApp == null)
return;
if (!FileModificationService.getInstance().prepareFileForWrite(webXmlFile))
return;
final Filter strutsFilter = webApp.addFilter();
strutsFilter.getFilterName().setStringValue("struts2");
@NonNls final String filterClass = templateProvider.is21orNewer() ? StrutsConstants.STRUTS_2_1_FILTER_CLASS : StrutsConstants.STRUTS_2_0_FILTER_CLASS;
strutsFilter.getFilterClass().setStringValue(filterClass);
final FilterMapping filterMapping = webApp.addFilterMapping();
filterMapping.getFilterName().setValue(strutsFilter);
filterMapping.addUrlPattern().setStringValue("/*");
}
}.execute();
final NotificationListener showFacetSettingsListener = new NotificationListener() {
public void hyperlinkUpdate(@NotNull final Notification notification, @NotNull final HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
notification.expire();
ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null);
}
}
};
Notifications.Bus.notify(new Notification("Struts 2", "Struts 2 Setup", "Struts 2 Facet has been created, please check <a href=\"more\">created fileset</a>", NotificationType.INFORMATION, showFacetSettingsListener), module.getProject());
} catch (Exception e) {
LOG.error("error creating struts.xml from template", e);
}
}));
}
use of com.intellij.ide.fileTemplates.FileTemplate in project google-cloud-intellij by GoogleCloudPlatform.
the class AppEngineStandardSupportProvider method createFileFromTemplate.
@Nullable
public static VirtualFile createFileFromTemplate(final String templateName, final VirtualFile parent, final String fileName) {
parent.refresh(false, false);
VirtualFile file = parent.findChild(fileName);
if (file != null) {
return file;
}
final FileTemplate template = FileTemplateManager.getDefaultInstance().getJ2eeTemplate(templateName);
try {
final String text = template.getText(FileTemplateManager.getDefaultInstance().getDefaultProperties());
file = parent.createChildData(AppEngineStandardSupportProvider.class, fileName);
VfsUtil.saveText(file, text);
return file;
} catch (IOException ioe) {
LOG.error(ioe);
return null;
}
}
use of com.intellij.ide.fileTemplates.FileTemplate in project yii2support by nvlad.
the class MissedViewLocalQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final String projectUrl = YiiApplicationUtils.getYiiRootUrl(project);
final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
VirtualFile virtualFile = virtualFileManager.findFileByUrl(projectUrl + myPath);
if (virtualFile != null) {
System.out.println("File " + projectUrl + myPath + " already exist.");
return;
}
VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
if (yiiRoot == null) {
return;
}
PsiDirectory directory = PsiManager.getInstance(project).findDirectory(yiiRoot);
if (directory == null) {
return;
}
List<String> pathElements = StringUtil.split(StringUtil.trimLeading(myPath, '/'), "/");
if (pathElements.size() == 0) {
return;
}
final String fileName = pathElements.get(pathElements.size() - 1);
pathElements.remove(pathElements.size() - 1);
for (String pathElement : pathElements) {
if (directory.findSubdirectory(pathElement) == null) {
directory.createSubdirectory(pathElement);
}
directory = directory.findSubdirectory(pathElement);
if (directory == null) {
return;
}
}
final PsiFile viewPsiFile = directory.createFile(fileName);
FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(Files.getFileExtension(myPath));
String templateName = getFileTemplateName(fileType);
if (templateName == null) {
System.out.println("Template for \"" + myPath + "\" not detected.");
return;
}
templateName = templateName.toLowerCase();
FileEditorManager.getInstance(project).openFile(viewPsiFile.getVirtualFile(), true);
final FileTemplate[] templates = FileTemplateManager.getDefaultInstance().getTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY);
FileTemplate template = null;
for (FileTemplate fileTemplate : templates) {
if (fileTemplate.isTemplateOfType(fileType) && fileTemplate.getName().toLowerCase().equals(templateName)) {
template = fileTemplate;
break;
}
}
if (template != null && viewPsiFile.getViewProvider().getDocument() != null) {
final Properties properties = FileTemplateManager.getDefaultInstance().getDefaultProperties();
if (myParameters != null) {
Set<String> parameters = new LinkedHashSet<>(myParameters.size());
for (Map.Entry<String, PhpType> parameter : myParameters.entrySet()) {
parameters.add(parameter.getKey() + ' ' + parameter.getValue());
}
properties.setProperty("YII2_VIEW_PARAMETERS", StringUtil.join(parameters, ";"));
}
template.setLiveTemplateEnabled(true);
template.setReformatCode(true);
try {
viewPsiFile.getViewProvider().getDocument().insertString(0, template.getText(properties));
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.intellij.ide.fileTemplates.FileTemplate in project Perl5-IDEA by Camelcade.
the class PerlFileFromTemplateAction method buildDialog.
@Override
protected void buildDialog(Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
builder.setTitle(ACTION_TITLE).addKind("Package", PerlIcons.PM_FILE, "Perl5 package").addKind("Script", PerlIcons.PERL_SCRIPT_FILE_ICON, "Perl5 script").addKind("Test", PerlIcons.TEST_FILE, "Perl5 test").addKind("POD file", PerlIcons.POD_FILE, "Perl5 pod").addKind("Mojolicious Template File", PerlIcons.MOJO_FILE, "Perl5 mojolicious").addKind("Embedded Perl5 File", PerlIcons.EMBEDDED_PERL_FILE, "Perl5 embedded");
FileTypeManagerEx fileTypeManager = FileTypeManagerEx.getInstanceEx();
for (FileTemplate fileTemplate : FileTemplateManager.getInstance(project).getAllTemplates()) {
if (PerlCreateFileFromTemplateHandler.INSTANCE.handlesTemplate(fileTemplate)) {
builder.addKind(fileTemplate.getName(), fileTypeManager.getFileTypeByExtension(fileTemplate.getExtension()).getIcon(), fileTemplate.getName());
}
}
}
Aggregations