Search in sources :

Example 1 with StrutsFacet

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

the class BasicLightHighlightingTestCase method tearDown.

@Override
protected final void tearDown() throws Exception {
    try {
        performTearDown();
    } finally {
        // clear existing StrutsFacet configuration before running next test
        final StrutsFacet existing = StrutsFacet.getInstance(myModule);
        if (existing != null) {
            existing.getConfiguration().getFileSets().clear();
        }
        super.tearDown();
    }
}
Also used : StrutsFacet(com.intellij.struts2.facet.StrutsFacet)

Example 2 with StrutsFacet

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

the class BasicLightHighlightingTestCase method createStrutsFileSet.

/**
   * For files located in JAR: {@code [PATH_TO_JAR]!/[PATH_TO_STRUTS_XML]}.
   *
   * @param strutsXmlPaths Paths to files.
   */
protected void createStrutsFileSet(@NonNls final String... strutsXmlPaths) {
    final StrutsFacet strutsFacet = StrutsFacet.getInstance(myModule);
    assertNotNull(strutsFacet);
    final StrutsFacetConfiguration facetConfiguration = strutsFacet.getConfiguration();
    final StrutsFileSet fileSet = new StrutsFileSet("test", "test", facetConfiguration);
    for (final String fileName : strutsXmlPaths) {
        final VirtualFile file;
        final String path;
        if (fileName.contains("!")) {
            path = PathManager.getHomePath() + TEST_DATA_PATH + "/" + fileName;
            file = JarFileSystem.getInstance().refreshAndFindFileByPath(path);
        } else {
            path = fileName;
            file = myFixture.copyFileToProject(fileName);
        }
        assertNotNull("could not find file: '" + path + "'", file);
        fileSet.addFile(file);
    }
    final Set<StrutsFileSet> strutsFileSetSet = facetConfiguration.getFileSets();
    strutsFileSetSet.add(fileSet);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StrutsFacetConfiguration(com.intellij.struts2.facet.StrutsFacetConfiguration) StrutsFacet(com.intellij.struts2.facet.StrutsFacet) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet)

Example 3 with StrutsFacet

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

the class Struts2IconProvider method getIcon.

@Nullable
public Icon getIcon(@NotNull final PsiElement element, final int flags) {
    if (!(element instanceof PsiClass)) {
        return null;
    }
    // IconProvider queries non-physical PSI as well (e.g. completion items); check validity
    if (!element.isPhysical() || !element.isValid()) {
        return null;
    }
    // no icons when no facet present
    final Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module == null) {
        return null;
    }
    final StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
    if (strutsFacet == null) {
        return null;
    }
    // handle suitable JAVA classes --> overlay icon
    final PsiClass psiClass = (PsiClass) element;
    if (psiClass.isInterface() || psiClass.isEnum() || !psiClass.hasModifierProperty(PsiModifier.PUBLIC) || psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || !JamCommonUtil.isPlainJavaFile(psiClass.getContainingFile())) {
        return null;
    }
    final StrutsModel strutsModel = StrutsManager.getInstance(psiClass.getProject()).getCombinedModel(module);
    if (strutsModel == null || !strutsModel.isActionClass(psiClass)) {
        return null;
    }
    final LayeredIcon layeredIcon = new LayeredIcon(2);
    final Icon original = PsiClassImplUtil.getClassIcon(flags, psiClass);
    layeredIcon.setIcon(original, 0);
    layeredIcon.setIcon(Struts2Icons.Action_small, 1, StrutsIcons.OVERLAY_X_OFFSET, StrutsIcons.OVERLAY_Y_OFFSET);
    return JBUI.scale(layeredIcon);
}
Also used : LayeredIcon(com.intellij.ui.LayeredIcon) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) PsiClass(com.intellij.psi.PsiClass) LayeredIcon(com.intellij.ui.LayeredIcon) Module(com.intellij.openapi.module.Module) StrutsFacet(com.intellij.struts2.facet.StrutsFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with StrutsFacet

use of com.intellij.struts2.facet.StrutsFacet 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 5 with StrutsFacet

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

the class StrutsFileSetCheckingAnnotator method annotate.

public void annotate(@NotNull final PsiElement psiElement, @NotNull final AnnotationHolder holder) {
    if (!(psiElement instanceof XmlFile)) {
        return;
    }
    if (psiElement instanceof JspFile) {
        return;
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(psiElement);
    if (module == null) {
        return;
    }
    // do not run when facet not enabled
    if (StrutsFacet.getInstance(module) == null) {
        return;
    }
    final XmlFile xmlFile = (XmlFile) psiElement;
    final Project project = psiElement.getProject();
    final StrutsManager strutsManager = StrutsManager.getInstance(project);
    if (!strutsManager.isStruts2ConfigFile(xmlFile)) {
        return;
    }
    final VirtualFile currentVirtualFile = xmlFile.getVirtualFile();
    assert currentVirtualFile != null;
    final Set<StrutsFileSet> allConfigFileSets = strutsManager.getAllConfigFileSets(module);
    for (final StrutsFileSet configFileSet : allConfigFileSets) {
        if (configFileSet.hasFile(currentVirtualFile)) {
            return;
        }
    }
    final boolean fileSetAvailable = allConfigFileSets.size() != 0;
    final Annotation annotation = holder.createWarningAnnotation(xmlFile, fileSetAvailable ? StrutsBundle.message("annotators.fileset.file.not.registered") : StrutsBundle.message("annotators.fileset.no.file.sets"));
    annotation.setFileLevelAnnotation(true);
    if (fileSetAvailable) {
        final AddToFileSetFix addToFileSetFix = new AddToFileSetFix(xmlFile.getName());
        annotation.registerFix(addToFileSetFix);
    } else {
        annotation.registerFix(new IntentionAction() {

            @SuppressWarnings("DialogTitleCapitalization")
            @NotNull
            public String getText() {
                return StrutsBundle.message("annotators.fileset.edit.facet.settings");
            }

            @NotNull
            public String getFamilyName() {
                return StrutsBundle.message("intentions.family.name");
            }

            public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile psiFile) {
                return true;
            }

            public void invoke(@NotNull final Project project, final Editor editor, final PsiFile psiFile) throws IncorrectOperationException {
                final StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
                assert strutsFacet != null;
                ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null);
            }

            public boolean startInWriteAction() {
                return false;
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) NotNull(org.jetbrains.annotations.NotNull) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) Annotation(com.intellij.lang.annotation.Annotation) Project(com.intellij.openapi.project.Project) JspFile(com.intellij.psi.jsp.JspFile) BaseIntentionAction(com.intellij.codeInsight.intention.impl.BaseIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) Editor(com.intellij.openapi.editor.Editor) StrutsFacet(com.intellij.struts2.facet.StrutsFacet)

Aggregations

StrutsFacet (com.intellij.struts2.facet.StrutsFacet)4 Module (com.intellij.openapi.module.Module)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 StrutsFileSet (com.intellij.struts2.facet.ui.StrutsFileSet)3 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)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 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiDirectory (com.intellij.psi.PsiDirectory)1