Search in sources :

Example 36 with NotificationListener

use of com.intellij.notification.NotificationListener 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)

Aggregations

NotificationListener (com.intellij.notification.NotificationListener)36 HyperlinkEvent (javax.swing.event.HyperlinkEvent)28 Notification (com.intellij.notification.Notification)26 NotNull (org.jetbrains.annotations.NotNull)23 Project (com.intellij.openapi.project.Project)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GuardedBy (com.android.annotations.concurrency.GuardedBy)2 IDevice (com.android.ddmlib.IDevice)2 com.android.tools.idea.fd (com.android.tools.idea.fd)2 RestartActivityAction (com.android.tools.idea.fd.actions.RestartActivityAction)2 ConsolePrinter (com.android.tools.idea.run.ConsolePrinter)2 LaunchStatus (com.android.tools.idea.run.util.LaunchStatus)2 BrowserUtil (com.intellij.ide.BrowserUtil)2 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)2 NotificationType (com.intellij.notification.NotificationType)2 ShowSettingsUtil (com.intellij.openapi.options.ShowSettingsUtil)2 MultiMap (com.intellij.util.containers.MultiMap)2 File (java.io.File)2 Set (java.util.Set)2 Nullable (org.jetbrains.annotations.Nullable)2