Search in sources :

Example 71 with FileTemplate

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);
        }
    }));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) HyperlinkEvent(javax.swing.event.HyperlinkEvent) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FilterMapping(com.intellij.javaee.model.xml.web.FilterMapping) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) Notification(com.intellij.notification.Notification) StrutsFileTemplateProvider(com.intellij.struts2.StrutsFileTemplateProvider) MultiMap(com.intellij.util.containers.MultiMap) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NonNls(org.jetbrains.annotations.NonNls) XmlFile(com.intellij.psi.xml.XmlFile) ConfigFile(com.intellij.util.descriptors.ConfigFile) StrutsConfigsSearcher(com.intellij.struts2.facet.ui.StrutsConfigsSearcher) Filter(com.intellij.javaee.model.xml.web.Filter) Collection(java.util.Collection) Module(com.intellij.openapi.module.Module) WebFacet(com.intellij.javaee.web.facet.WebFacet) WebApp(com.intellij.javaee.model.xml.web.WebApp) NotificationListener(com.intellij.notification.NotificationListener)

Example 72 with FileTemplate

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;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with FileTemplate

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();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) PhpFileType(com.jetbrains.php.lang.PhpFileType) TwigFileType(com.jetbrains.twig.TwigFileType) FileType(com.intellij.openapi.fileTypes.FileType) SmartyFileType(com.jetbrains.smarty.SmartyFileType) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 74 with FileTemplate

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());
        }
    }
}
Also used : FileTypeManagerEx(com.intellij.openapi.fileTypes.ex.FileTypeManagerEx) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)74 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)24 PsiFile (com.intellij.psi.PsiFile)15 IOException (java.io.IOException)15 Properties (java.util.Properties)14 Project (com.intellij.openapi.project.Project)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 PsiDirectory (com.intellij.psi.PsiDirectory)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 Pattern (java.util.regex.Pattern)7 NotNull (org.jetbrains.annotations.NotNull)7 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)6 PsiElement (com.intellij.psi.PsiElement)6 File (java.io.File)6 Matcher (java.util.regex.Matcher)6 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)5 Module (com.intellij.openapi.module.Module)5 Nullable (org.jetbrains.annotations.Nullable)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 Editor (com.intellij.openapi.editor.Editor)3