Search in sources :

Example 1 with StrutsModel

use of com.intellij.struts2.dom.struts.model.StrutsModel in project intellij-plugins by JetBrains.

the class JspActionAnnotator method annotate.

private static void annotate(@NotNull final PsiElement element, @NotNull final Collection<LineMarkerInfo> lineMarkerInfos) {
    if (!(element instanceof XmlTag)) {
        return;
    }
    final XmlTag xmlTag = (XmlTag) element;
    // any of our tags?
    final String tagName = xmlTag.getLocalName();
    if (Arrays.binarySearch(TAGS_WITH_ACTION_ATTRIBUTE, tagName) < 0) {
        return;
    }
    if (!Comparing.equal(xmlTag.getNamespace(), StrutsConstants.TAGLIB_STRUTS_UI_URI)) {
        return;
    }
    // short exit when Struts 2 facet not present
    final Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module == null) {
        return;
    }
    if (StrutsFacet.getInstance(module) == null) {
        return;
    }
    // special case for <action>
    final String actionPath = Comparing.equal(tagName, ACTION_ATTRIBUTE_NAME) ? xmlTag.getAttributeValue("name") : xmlTag.getAttributeValue(ACTION_ATTRIBUTE_NAME);
    if (actionPath == null) {
        return;
    }
    final StrutsModel strutsModel = StrutsManager.getInstance(element.getProject()).getCombinedModel(module);
    if (strutsModel == null) {
        return;
    }
    final String namespace = xmlTag.getAttributeValue("namespace");
    final List<Action> actions = strutsModel.findActionsByName(actionPath, namespace);
    if (actions.isEmpty()) {
        return;
    }
    // resolve to action method should be exactly 1
    final NavigationGutterIconBuilder<PsiElement> gutterIconBuilder = NavigationGutterIconBuilder.create(StrutsIcons.ACTION_CLASS).setAlignment(GutterIconRenderer.Alignment.LEFT).setTooltipText(StrutsBundle.message("annotators.jsp.goto.action.method")).setEmptyPopupText(StrutsBundle.message("annotators.jsp.goto.action.method.not.found")).setTargets(new NotNullLazyValue<Collection<? extends PsiElement>>() {

        @NotNull
        protected Collection<PsiMethod> compute() {
            return ContainerUtil.mapNotNull(actions, ACTION_METHOD_FUNCTION);
        }
    });
    lineMarkerInfos.add(gutterIconBuilder.createLineMarkerInfo(xmlTag));
}
Also used : Action(com.intellij.struts2.dom.struts.action.Action) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) Collection(java.util.Collection) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with StrutsModel

use of com.intellij.struts2.dom.struts.model.StrutsModel 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 3 with StrutsModel

use of com.intellij.struts2.dom.struts.model.StrutsModel in project intellij-plugins by JetBrains.

the class StrutsConstantManagerImpl method getStringValue.

/**
   * Returns the plain String value for the given constant.
   *
   * @param context     Current context.
   * @param strutsModel StrutsModel.
   * @param name        Name of constant.
   * @return {@code null} if no value could be resolved.
   */
@Nullable
private static String getStringValue(@NotNull final PsiFile context, @NotNull final StrutsModel strutsModel, @NotNull @NonNls final String name) {
    final Project project = context.getProject();
    final Module module = ModuleUtilCore.findModuleForPsiElement(context);
    assert module != null : context;
    // collect all properties with matching key
    final List<IProperty> properties = PropertiesImplUtil.findPropertiesByKey(project, name);
    String value = null;
    // 1. default.properties from struts2-core.jar
    final IProperty strutsDefaultProperty = ContainerUtil.find(properties, property -> {
        final VirtualFile virtualFile = property.getPropertiesFile().getVirtualFile();
        return virtualFile != null && virtualFile.getFileSystem() instanceof JarFileSystem && StringUtil.endsWith(virtualFile.getPath(), STRUTS_DEFAULT_PROPERTIES) && ModuleUtilCore.moduleContainsFile(module, virtualFile, true);
    });
    if (strutsDefaultProperty != null) {
        value = strutsDefaultProperty.getValue();
    }
    // 2. <constant> from StrutsModel
    final Condition<Constant> constantNameCondition = constant -> Comparing.equal(constant.getName().getStringValue(), name);
    final List<DomFileElement<StrutsRoot>> domFileElements = new ArrayList<>();
    collectStrutsXmls(domFileElements, strutsModel, "struts-default.xml", true);
    collectStrutsXmls(domFileElements, strutsModel, "struts-plugin.xml", true);
    collectStrutsXmls(domFileElements, strutsModel, "struts.xml", false);
    for (final DomFileElement<StrutsRoot> domFileElement : domFileElements) {
        final Constant constant = ContainerUtil.find(domFileElement.getRootElement().getConstants(), constantNameCondition);
        final String strutsXmlValue = constant != null ? constant.getValue().getStringValue() : null;
        if (strutsXmlValue != null) {
            value = strutsXmlValue;
        }
    }
    // 3. struts.properties in current module
    final IProperty strutsProperty = ContainerUtil.find(properties, property -> {
        final VirtualFile virtualFile = property.getPropertiesFile().getVirtualFile();
        return virtualFile != null && Comparing.equal(virtualFile.getName(), STRUTS_PROPERTIES_FILENAME) && ModuleUtilCore.moduleContainsFile(module, virtualFile, false);
    });
    if (strutsProperty != null) {
        value = strutsProperty.getValue();
    }
    // 4. web.xml
    final WebFacet webFacet = WebUtil.getWebFacet(context);
    if (webFacet == null) {
        // should not happen in real projects..
        return value;
    }
    final WebApp webApp = webFacet.getRoot();
    if (webApp == null) {
        // no web.xml
        return value;
    }
    final Filter filter = ContainerUtil.find(webApp.getFilters(), WEB_XML_STRUTS_FILTER_CONDITION);
    if (filter != null) {
        final ParamValue initParam = ContainerUtil.find(filter.getInitParams(), (Condition<ParamValue>) paramValue -> Comparing.equal(paramValue.getParamName().getStringValue(), name));
        if (initParam != null) {
            value = initParam.getParamValue().getStringValue();
        }
    }
    return value;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WebFacet(com.intellij.javaee.web.facet.WebFacet) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) ArrayList(java.util.ArrayList) FilteringProcessor(com.intellij.util.FilteringProcessor) PsiClass(com.intellij.psi.PsiClass) StrutsRoot(com.intellij.struts2.dom.struts.StrutsRoot) Comparing(com.intellij.openapi.util.Comparing) PsiElement(com.intellij.psi.PsiElement) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) WebUtil(com.intellij.javaee.web.WebUtil) Module(com.intellij.openapi.module.Module) StrutsConstants(com.intellij.struts2.StrutsConstants) CommonProcessors(com.intellij.util.CommonProcessors) Converter(com.intellij.util.xml.Converter) Extensions(com.intellij.openapi.extensions.Extensions) Constant(com.intellij.struts2.dom.struts.constant.Constant) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) DomFileElement(com.intellij.util.xml.DomFileElement) ConvertContextFactory(com.intellij.util.xml.impl.ConvertContextFactory) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PropertiesImplUtil(com.intellij.lang.properties.PropertiesImplUtil) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) WebApp(com.intellij.javaee.model.xml.web.WebApp) ParamValue(com.intellij.javaee.model.xml.ParamValue) ConvertContext(com.intellij.util.xml.ConvertContext) NotNull(org.jetbrains.annotations.NotNull) Filter(com.intellij.javaee.model.xml.web.Filter) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Constant(com.intellij.struts2.dom.struts.constant.Constant) ArrayList(java.util.ArrayList) DomFileElement(com.intellij.util.xml.DomFileElement) ParamValue(com.intellij.javaee.model.xml.ParamValue) Project(com.intellij.openapi.project.Project) IProperty(com.intellij.lang.properties.IProperty) Filter(com.intellij.javaee.model.xml.web.Filter) StrutsRoot(com.intellij.struts2.dom.struts.StrutsRoot) Module(com.intellij.openapi.module.Module) WebFacet(com.intellij.javaee.web.facet.WebFacet) WebApp(com.intellij.javaee.model.xml.web.WebApp) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with StrutsModel

use of com.intellij.struts2.dom.struts.model.StrutsModel in project intellij-plugins by JetBrains.

the class StrutsJamUtils method getStrutsModel.

/**
   * Gets the model for the given attribute element.
   *
   * @param attributeElement Attribute element.
   * @return {@code null} if no StrutsModel could be determined.
   */
@Nullable
public static StrutsModel getStrutsModel(final JamAttributeElement attributeElement) {
    final PsiElement value = attributeElement.getPsiElement();
    if (value == null) {
        return null;
    }
    final StrutsManager instance = StrutsManager.getInstance(value.getProject());
    return instance.getCombinedModel(value);
}
Also used : StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with StrutsModel

use of com.intellij.struts2.dom.struts.model.StrutsModel in project intellij-plugins by JetBrains.

the class GoToPackageSymbolProvider method addItems.

protected void addItems(@NotNull final Module module, final String name, final List<NavigationItem> result) {
    final StrutsModel strutsModel = StrutsManager.getInstance(module.getProject()).getCombinedModel(module);
    if (strutsModel == null) {
        return;
    }
    final List<StrutsPackage> strutsPackageList = strutsModel.getStrutsPackages();
    for (final StrutsPackage strutsPackage : strutsPackageList) {
        if (Comparing.strEqual(name, strutsPackage.getName().getStringValue())) {
            final NavigationItem item = createNavigationItem(strutsPackage.getXmlTag(), name, StrutsIcons.STRUTS_PACKAGE);
            result.add(item);
        }
    }
}
Also used : NavigationItem(com.intellij.navigation.NavigationItem) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage)

Aggregations

StrutsModel (com.intellij.struts2.dom.struts.model.StrutsModel)24 StrutsManager (com.intellij.struts2.dom.struts.model.StrutsManager)10 Action (com.intellij.struts2.dom.struts.action.Action)8 NotNull (org.jetbrains.annotations.NotNull)8 Module (com.intellij.openapi.module.Module)6 PsiElement (com.intellij.psi.PsiElement)6 PsiFile (com.intellij.psi.PsiFile)6 Nullable (org.jetbrains.annotations.Nullable)6 XmlFile (com.intellij.psi.xml.XmlFile)5 PathReference (com.intellij.openapi.paths.PathReference)4 PsiReference (com.intellij.psi.PsiReference)4 Result (com.intellij.struts2.dom.struts.action.Result)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiClass (com.intellij.psi.PsiClass)3 StrutsRoot (com.intellij.struts2.dom.struts.StrutsRoot)3 ArrayList (java.util.ArrayList)3 NavigationItem (com.intellij.navigation.NavigationItem)2 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)2 PsiManager (com.intellij.psi.PsiManager)2 XmlTag (com.intellij.psi.xml.XmlTag)2