Search in sources :

Example 6 with StrutsFileSet

use of com.intellij.struts2.facet.ui.StrutsFileSet in project intellij-plugins by JetBrains.

the class StrutsFacetConfiguration method writeExternal.

public void writeExternal(final Element element) throws WriteExternalException {
    for (final StrutsFileSet fileSet : myFileSets) {
        final Element setElement = new Element(FILESET);
        setElement.setAttribute(SET_ID, fileSet.getId());
        setElement.setAttribute(SET_NAME, fileSet.getName());
        setElement.setAttribute(SET_REMOVED, Boolean.toString(fileSet.isRemoved()));
        element.addContent(setElement);
        for (final VirtualFilePointer fileName : fileSet.getFiles()) {
            final Element fileElement = new Element(FILE);
            fileElement.setText(fileName.getUrl());
            setElement.addContent(fileElement);
        }
    }
    final Element propertiesElement = new Element(PROPERTIES_KEYS);
    propertiesElement.setAttribute(PROPERTIES_KEYS_DISABLED, Boolean.toString(myPropertiesKeysDisabled));
    element.addContent(propertiesElement);
}
Also used : Element(org.jdom.Element) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer)

Example 7 with StrutsFileSet

use of com.intellij.struts2.facet.ui.StrutsFileSet 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 8 with StrutsFileSet

use of com.intellij.struts2.facet.ui.StrutsFileSet in project intellij-plugins by JetBrains.

the class Struts2GraphFileEditorProvider method accept.

public boolean accept(@NotNull final Project project, @NotNull final VirtualFile file) {
    if (!file.isValid()) {
        return false;
    }
    final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    if (!(psiFile instanceof XmlFile)) {
        return false;
    }
    if (psiFile instanceof JspFile) {
        return false;
    }
    if (!StrutsManager.getInstance(project).isStruts2ConfigFile((XmlFile) psiFile)) {
        return false;
    }
    final Module module = ModuleUtilCore.findModuleForFile(file, project);
    if (module == null) {
        return false;
    }
    final Set<StrutsFileSet> fileSets = StrutsManager.getInstance(project).getAllConfigFileSets(module);
    for (final StrutsFileSet fileSet : fileSets) {
        if (fileSet.hasFile(file)) {
            return true;
        }
    }
    return false;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) JspFile(com.intellij.psi.jsp.JspFile) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet)

Aggregations

StrutsFileSet (com.intellij.struts2.facet.ui.StrutsFileSet)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 XmlFile (com.intellij.psi.xml.XmlFile)5 Module (com.intellij.openapi.module.Module)4 PsiFile (com.intellij.psi.PsiFile)4 VirtualFilePointer (com.intellij.openapi.vfs.pointers.VirtualFilePointer)2 JspFile (com.intellij.psi.jsp.JspFile)2 StrutsFacet (com.intellij.struts2.facet.StrutsFacet)2 Element (org.jdom.Element)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 BaseIntentionAction (com.intellij.codeInsight.intention.impl.BaseIntentionAction)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 Filter (com.intellij.javaee.model.xml.web.Filter)1 FilterMapping (com.intellij.javaee.model.xml.web.FilterMapping)1 WebApp (com.intellij.javaee.model.xml.web.WebApp)1 WebFacet (com.intellij.javaee.web.facet.WebFacet)1 Annotation (com.intellij.lang.annotation.Annotation)1 Notification (com.intellij.notification.Notification)1 NotificationListener (com.intellij.notification.NotificationListener)1 Editor (com.intellij.openapi.editor.Editor)1