Search in sources :

Example 1 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class BasicDomElementComponent method bindProperties.

protected final void bindProperties(final DomElement domElement) {
    if (domElement == null)
        return;
    DomElementAnnotationsManager.getInstance(domElement.getManager().getProject()).addHighlightingListener(new DomElementAnnotationsManager.DomHighlightingListener() {

        @Override
        public void highlightingFinished(@NotNull final DomFileElement element) {
            ApplicationManager.getApplication().invokeLater(() -> {
                if (getComponent().isShowing() && element.isValid()) {
                    updateHighlighting();
                }
            });
        }
    }, this);
    for (final AbstractDomChildrenDescription description : domElement.getGenericInfo().getChildrenDescriptions()) {
        final JComponent boundComponent = getBoundComponent(description);
        if (boundComponent != null) {
            if (description instanceof DomFixedChildDescription && DomUtil.isGenericValueType(description.getType())) {
                if ((description.getValues(domElement)).size() == 1) {
                    final GenericDomValue element = domElement.getManager().createStableValue(() -> domElement.isValid() ? (GenericDomValue) description.getValues(domElement).get(0) : null);
                    doBind(DomUIFactory.createControl(element, commitOnEveryChange(element)), boundComponent);
                } else {
                //todo not bound
                }
            } else if (description instanceof DomCollectionChildDescription) {
                doBind(DomUIFactory.getDomUIFactory().createCollectionControl(domElement, (DomCollectionChildDescription) description), boundComponent);
            }
        }
    }
    reset();
}
Also used : DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomFileElement(com.intellij.util.xml.DomFileElement) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription) DomElementAnnotationsManager(com.intellij.util.xml.highlighting.DomElementAnnotationsManager) GenericDomValue(com.intellij.util.xml.GenericDomValue)

Example 2 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class MavenModulePsiReference method getVariants.

@NotNull
public Object[] getVariants() {
    List<DomFileElement<MavenDomProjectModel>> files = MavenDomUtil.collectProjectModels(getProject());
    List<Object> result = new ArrayList<>();
    for (DomFileElement<MavenDomProjectModel> eachDomFile : files) {
        VirtualFile eachVFile = eachDomFile.getOriginalFile().getVirtualFile();
        if (Comparing.equal(eachVFile, myVirtualFile))
            continue;
        PsiFile psiFile = eachDomFile.getFile();
        String modulePath = calcRelativeModulePath(myVirtualFile, eachVFile);
        result.add(LookupElementBuilder.create(psiFile, modulePath).withPresentableText(modulePath));
    }
    return result.toArray();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) DomFileElement(com.intellij.util.xml.DomFileElement) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class PluginDescriptorChooser method show.

public static void show(final Project project, final Editor editor, final PsiFile file, final Consumer<DomFileElement<IdeaPlugin>> consumer) {
    final Module module = ModuleUtilCore.findModuleForPsiElement(file);
    assert module != null;
    List<DomFileElement<IdeaPlugin>> elements = DomService.getInstance().getFileElements(IdeaPlugin.class, project, module.getModuleWithDependentsScope());
    elements = ContainerUtil.filter(elements, element -> {
        VirtualFile virtualFile = element.getFile().getVirtualFile();
        return virtualFile != null && ProjectRootManager.getInstance(project).getFileIndex().isInContent(virtualFile);
    });
    elements = findAppropriateIntelliJModule(module.getName(), elements);
    if (elements.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, "Cannot find plugin descriptor");
        return;
    }
    if (elements.size() == 1) {
        consumer.consume(elements.get(0));
        return;
    }
    final BaseListPopupStep<PluginDescriptorCandidate> popupStep = new BaseListPopupStep<PluginDescriptorCandidate>("Choose Plugin Descriptor", createCandidates(module, elements)) {

        @Override
        public boolean isSpeedSearchEnabled() {
            return true;
        }

        @Override
        public Icon getIconFor(PluginDescriptorCandidate candidate) {
            return candidate.getIcon();
        }

        @NotNull
        @Override
        public String getTextFor(PluginDescriptorCandidate candidate) {
            return candidate.getText();
        }

        @Nullable
        @Override
        public ListSeparator getSeparatorAbove(PluginDescriptorCandidate candidate) {
            final String separatorText = candidate.getSeparatorText();
            if (separatorText != null) {
                return new ListSeparator(separatorText);
            }
            return null;
        }

        @Override
        public PopupStep onChosen(PluginDescriptorCandidate selectedValue, boolean finalChoice) {
            consumer.consume(selectedValue.myDomFileElement);
            return FINAL_CHOICE;
        }
    };
    JBPopupFactory.getInstance().createListPopup(popupStep).showInBestPositionFor(editor);
}
Also used : IncludedXmlTag(com.intellij.xml.util.IncludedXmlTag) AllIcons(com.intellij.icons.AllIcons) VirtualFile(com.intellij.openapi.vfs.VirtualFile) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ListSeparator(com.intellij.openapi.ui.popup.ListSeparator) ContainerUtil(com.intellij.util.containers.ContainerUtil) IdeaPlugin(org.jetbrains.idea.devkit.dom.IdeaPlugin) Comparing(com.intellij.openapi.util.Comparing) Extensions(org.jetbrains.idea.devkit.dom.Extensions) Map(java.util.Map) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) EmptyIcon(com.intellij.util.ui.EmptyIcon) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) DomFileElement(com.intellij.util.xml.DomFileElement) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) ModulesAlphaComparator(com.intellij.openapi.roots.ui.configuration.ModulesAlphaComparator) List(java.util.List) ModuleGrouper(com.intellij.openapi.module.ModuleGrouper) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) DomService(com.intellij.util.xml.DomService) Function(com.intellij.util.Function) PopupStep(com.intellij.openapi.ui.popup.PopupStep) HintManager(com.intellij.codeInsight.hint.HintManager) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Consumer(com.intellij.util.Consumer) javax.swing(javax.swing) VirtualFile(com.intellij.openapi.vfs.VirtualFile) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) ListSeparator(com.intellij.openapi.ui.popup.ListSeparator) DomFileElement(com.intellij.util.xml.DomFileElement) Module(com.intellij.openapi.module.Module)

Example 4 with DomFileElement

use of com.intellij.util.xml.DomFileElement in project intellij-community by JetBrains.

the class PluginDescriptorChooser method createCandidates.

private static List<PluginDescriptorCandidate> createCandidates(final Module currentModule, List<DomFileElement<IdeaPlugin>> elements) {
    ModuleGrouper grouper = ModuleGrouper.instanceFor(currentModule.getProject());
    final List<String> groupPath = grouper.getGroupPath(currentModule);
    elements.sort((o1, o2) -> {
        // current module = first group
        final Module module1 = o1.getModule();
        final Module module2 = o2.getModule();
        if (currentModule.equals(module1))
            return -1;
        if (currentModule.equals(module2))
            return 1;
        if (module1 != null && module2 != null) {
            int groupComparison = Comparing.compare(groupMatchLevel(groupPath, grouper.getGroupPath(module2)), groupMatchLevel(groupPath, grouper.getGroupPath(module1)));
            if (groupComparison != 0) {
                return groupComparison;
            }
        }
        return ModulesAlphaComparator.INSTANCE.compare(module1, module2);
    });
    elements.sort((o1, o2) -> {
        if (!Comparing.equal(o1.getModule(), o2.getModule()))
            return 0;
        String pluginId1 = o1.getRootElement().getPluginId();
        String pluginId2 = o2.getRootElement().getPluginId();
        if (pluginId1 == null && pluginId2 == null) {
            return o1.getFile().getName().compareTo(o2.getFile().getName());
        }
        if (pluginId1 == null)
            return 1;
        if (pluginId2 == null)
            return -1;
        return Comparing.compare(pluginId1, pluginId2);
    });
    return ContainerUtil.map(elements, new Function<DomFileElement<IdeaPlugin>, PluginDescriptorCandidate>() {

        private Module myLastModule = currentModule;

        @Override
        public PluginDescriptorCandidate fun(DomFileElement<IdeaPlugin> element) {
            final Module module = element.getModule();
            boolean startsNewGroup = !myLastModule.equals(module);
            myLastModule = module;
            return new PluginDescriptorCandidate(element, startsNewGroup);
        }
    });
}
Also used : ModuleGrouper(com.intellij.openapi.module.ModuleGrouper) IdeaPlugin(org.jetbrains.idea.devkit.dom.IdeaPlugin) DomFileElement(com.intellij.util.xml.DomFileElement) Module(com.intellij.openapi.module.Module)

Example 5 with DomFileElement

use of com.intellij.util.xml.DomFileElement 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)

Aggregations

DomFileElement (com.intellij.util.xml.DomFileElement)10 NotNull (org.jetbrains.annotations.NotNull)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 Nullable (org.jetbrains.annotations.Nullable)4 Comparing (com.intellij.openapi.util.Comparing)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 XmlFile (com.intellij.psi.xml.XmlFile)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 Collections (java.util.Collections)3 List (java.util.List)3 ParamValue (com.intellij.javaee.model.xml.ParamValue)2 WebApp (com.intellij.javaee.model.xml.web.WebApp)2 WebFacet (com.intellij.javaee.web.facet.WebFacet)2 ModuleGrouper (com.intellij.openapi.module.ModuleGrouper)2 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)2 Condition (com.intellij.openapi.util.Condition)2 PsiClass (com.intellij.psi.PsiClass)2