diff --git a/src/main/java/com/intellij/struts2/annotators/StrutsWebFacetCheckingAnnotator.java b/src/main/java/com/intellij/struts2/annotators/StrutsWebFacetCheckingAnnotator.java new file mode 100644 index 0000000..a50ef23 --- /dev/null +++ b/src/main/java/com/intellij/struts2/annotators/StrutsWebFacetCheckingAnnotator.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.struts2.annotators; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.lang.annotation.AnnotationHolder; +import com.intellij.lang.annotation.Annotator; +import com.intellij.lang.annotation.HighlightSeverity; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleUtilCore; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ui.configuration.ModulesConfigurator; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.jsp.JspFile; +import com.intellij.psi.xml.XmlFile; +import com.intellij.struts2.StrutsBundle; +import com.intellij.struts2.dom.struts.model.StrutsManager; +import com.intellij.struts2.facet.StrutsFacet; +import com.intellij.struts2.facet.WebFacetChecker; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; + +/** + * Warns when a {@code struts.xml} file is opened in a module that has a Struts + * facet but no {@link com.intellij.javaee.web.facet.WebFacet}, which prevents + * dispatch-style result paths (JSP etc.) from resolving. + */ +public class StrutsWebFacetCheckingAnnotator implements Annotator { + + @Override + public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder holder) { + if (!(psiElement instanceof XmlFile xmlFile)) { + return; + } + + if (psiElement instanceof JspFile) { + return; + } + + Module module = ModuleUtilCore.findModuleForPsiElement(psiElement); + if (module == null) { + return; + } + + StrutsFacet strutsFacet = StrutsFacet.getInstance(module); + if (strutsFacet == null) { + return; + } + + StrutsManager strutsManager = StrutsManager.getInstance(psiElement.getProject()); + if (!strutsManager.isStruts2ConfigFile(xmlFile)) { + return; + } + + if (!WebFacetChecker.isWebFacetMissing(module)) { + return; + } + + holder.newAnnotation(HighlightSeverity.WARNING, + StrutsBundle.message("annotators.webfacet.missing")) + .range(xmlFile) + .fileLevel() + .withFix(new ConfigureWebFacetFix(strutsFacet)) + .create(); + } + + private static final class ConfigureWebFacetFix implements IntentionAction { + + private final StrutsFacet myStrutsFacet; + + private ConfigureWebFacetFix(@NotNull StrutsFacet strutsFacet) { + myStrutsFacet = strutsFacet; + } + + @Override + @NotNull + public String getText() { + return StrutsBundle.message("annotators.webfacet.configure"); + } + + @Override + @NotNull + public String getFamilyName() { + return StrutsBundle.message("intentions.family.name"); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile psiFile) { + return true; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) + throws IncorrectOperationException { + ModulesConfigurator.showFacetSettingsDialog(myStrutsFacet, null); + } + + @Override + public boolean startInWriteAction() { + return false; + } + } +} diff --git a/src/main/java/com/intellij/struts2/facet/StrutsFrameworkInitializer.java b/src/main/java/com/intellij/struts2/facet/StrutsFrameworkInitializer.java index 278ae0b..6713a5c 100644 --- a/src/main/java/com/intellij/struts2/facet/StrutsFrameworkInitializer.java +++ b/src/main/java/com/intellij/struts2/facet/StrutsFrameworkInitializer.java @@ -32,6 +32,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; +import com.intellij.struts2.StrutsBundle; import com.intellij.struts2.StrutsConstants; import com.intellij.struts2.facet.ui.StrutsConfigsSearcher; import com.intellij.struts2.facet.ui.StrutsFileSet; @@ -127,16 +128,7 @@ public Object execute(@NotNull Project project, @NotNull Continuation super Un return data.strutsFacet; } - // Check for existing facets (project reopened) - no initialization needed - ModuleManager moduleManager = ModuleManager.getInstance(project); - Module[] modules = moduleManager.getModules(); - LOG.debug("Checking " + modules.length + " modules for existing Struts facets"); - for (Module module : modules) { - StrutsFacet facet = StrutsFacet.getInstance(module); - if (facet != null) { - LOG.debug("Found existing Struts facet in module: " + module.getName() + ", no initialization needed"); - } - } + checkWebFacetForAllModules(project); return null; } @@ -295,4 +287,38 @@ private void showErrorNotification(@NotNull Project project, @NotNull Exception NotificationType.ERROR) .notify(project); } + + /** + * Checks all modules with a Struts facet for a missing WebFacet and + * emits one notification per affected module (once per project open). + */ + private void checkWebFacetForAllModules(@NotNull Project project) { + Module[] modules = ModuleManager.getInstance(project).getModules(); + for (Module module : modules) { + if (WebFacetChecker.isWebFacetMissing(module)) { + showMissingWebFacetNotification(project, module); + } + } + } + + private void showMissingWebFacetNotification(@NotNull Project project, @NotNull Module module) { + StrutsFacet strutsFacet = StrutsFacet.getInstance(module); + if (strutsFacet == null) return; + + new Notification("Struts 2", + StrutsBundle.message("annotators.webfacet.notification.title"), + StrutsBundle.message("annotators.webfacet.notification.content", module.getName()), + NotificationType.WARNING) + .addAction(new NotificationAction( + StrutsBundle.message("annotators.webfacet.configure")) { + @Override + public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification notification) { + notification.expire(); + ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null); + } + }) + .notify(project); + + LOG.info("Missing WebFacet notification shown for module: " + module.getName()); + } } \ No newline at end of file diff --git a/src/main/java/com/intellij/struts2/facet/WebFacetChecker.java b/src/main/java/com/intellij/struts2/facet/WebFacetChecker.java new file mode 100644 index 0000000..1640603 --- /dev/null +++ b/src/main/java/com/intellij/struts2/facet/WebFacetChecker.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.struts2.facet; + +import com.intellij.openapi.module.Module; +import com.intellij.javaee.web.WebUtil; +import com.intellij.javaee.web.facet.WebFacet; +import com.intellij.openapi.module.ModuleUtilCore; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; + +/** + * Checks whether a module with a Struts facet has the required {@link WebFacet} + * for JSP/dispatch-style result path resolution. + *
+ * Shared between the file-level annotator and the framework initialization
+ * notification so the condition is defined in one place.
+ */
+public final class WebFacetChecker {
+
+ private WebFacetChecker() {}
+
+ /**
+ * Returns {@code true} when the given module has a Struts facet but lacks
+ * a usable {@link WebFacet}, meaning dispatch-style result paths
+ * (e.g. {@code /index.jsp}) cannot be resolved.
+ */
+ public static boolean isWebFacetMissing(@NotNull Module module) {
+ StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
+ if (strutsFacet == null) return false;
+ return strutsFacet.getWebFacet() == null;
+ }
+
+ /**
+ * Variant that also checks via {@link WebUtil#getWebFacet(PsiElement)},
+ * which searches the module and its dependants.
+ */
+ public static boolean isWebFacetMissing(@NotNull PsiElement element) {
+ Module module = ModuleUtilCore.findModuleForPsiElement(element);
+ if (module == null) return false;
+ StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
+ if (strutsFacet == null) return false;
+ return WebUtil.getWebFacet(element) == null;
+ }
+}
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 0c55621..73e08db 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -155,6 +155,7 @@
implementationClass="com.intellij.struts2.dom.params.ParamNameConverterImpl"/>