Search in sources :

Example 51 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidFindStyleApplicationsAction method getStyleData.

@Nullable
static MyStyleData getStyleData(@NotNull XmlTag tag) {
    final DomElement element = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    if (!(element instanceof Style)) {
        return null;
    }
    final Style style = (Style) element;
    final GenericAttributeValue<String> styleNameDomAttr = style.getName();
    final String styleName = styleNameDomAttr.getStringValue();
    final XmlAttributeValue styleNameAttrValue = styleNameDomAttr.getXmlAttributeValue();
    if (styleName == null || styleName.length() == 0 || styleNameAttrValue == null) {
        return null;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(tag);
    if (facet == null) {
        return null;
    }
    return new MyStyleData(style, styleName, facet, styleNameAttrValue);
}
Also used : DomElement(com.intellij.util.xml.DomElement) Style(org.jetbrains.android.dom.resources.Style) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidFindStyleApplicationsProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final Set<Pair<String, String>> attrsInStyle = new HashSet<Pair<String, String>>();
    for (AndroidAttributeInfo info : myAttrMap.keySet()) {
        attrsInStyle.add(Pair.create(info.getNamespace(), info.getName()));
    }
    for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        final DomElement domElement = element instanceof XmlTag ? DomManager.getDomManager(myProject).getDomElement((XmlTag) element) : null;
        if (domElement instanceof LayoutViewElement) {
            final List<XmlAttribute> attributesToDelete = new ArrayList<XmlAttribute>();
            for (XmlAttribute attribute : ((XmlTag) element).getAttributes()) {
                if (attrsInStyle.contains(Pair.create(attribute.getNamespace(), attribute.getLocalName()))) {
                    attributesToDelete.add(attribute);
                }
            }
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    for (XmlAttribute attribute : attributesToDelete) {
                        attribute.delete();
                    }
                    ((LayoutViewElement) domElement).getStyle().setStringValue("@style/" + myStyleName);
                }
            });
        }
    }
    final PsiFile file = myStyleTag.getContainingFile();
    if (file != null) {
        UndoUtil.markPsiFileForUndo(file);
    }
    if (myContext != null) {
        UndoUtil.markPsiFileForUndo(myContext);
    }
}
Also used : LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) PsiFile(com.intellij.psi.PsiFile) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet) Pair(com.intellij.openapi.util.Pair) XmlTag(com.intellij.psi.xml.XmlTag)

Example 53 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidInlineUtil method getLayoutUsageData.

@Nullable
static LayoutUsageData getLayoutUsageData(@NotNull XmlTag tag) {
    final Project project = tag.getProject();
    final DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
    if (domElement instanceof Include) {
        final GenericAttributeValue<ResourceValue> layoutAttribute = ((Include) domElement).getLayout();
        final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(layoutAttribute, true);
        if (reference != null) {
            return new LayoutUsageData(project, tag, reference);
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) Include(org.jetbrains.android.dom.layout.Include) Nullable(org.jetbrains.annotations.Nullable)

Example 54 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidInlineUtil method getInlinableStyleData.

@Nullable
static MyStyleData getInlinableStyleData(@NotNull XmlTag tag) {
    final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    if (!(domElement instanceof Style)) {
        return null;
    }
    final Style style = (Style) domElement;
    final XmlAttributeValue nameAttrValue = style.getName().getXmlAttributeValue();
    if (nameAttrValue == null) {
        return null;
    }
    final String styleName = style.getName().getStringValue();
    if (styleName == null || styleName.length() == 0) {
        return null;
    }
    return new MyStyleData(styleName, style, nameAttrValue);
}
Also used : DomElement(com.intellij.util.xml.DomElement) Style(org.jetbrains.android.dom.resources.Style) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with DomElement

use of com.intellij.util.xml.DomElement in project android by JetBrains.

the class AndroidExtractAsIncludeAction method isEnabledForTags.

@Override
protected boolean isEnabledForTags(@NotNull XmlTag[] tags) {
    if (tags.length == 0) {
        return false;
    }
    final DomManager domManager = DomManager.getDomManager(tags[0].getProject());
    boolean containsViewElement = false;
    for (XmlTag tag : tags) {
        final DomElement domElement = domManager.getDomElement(tag);
        if (!isSuitableDomElement(domElement)) {
            return false;
        }
        if (domElement instanceof LayoutViewElement) {
            containsViewElement = true;
        }
    }
    if (!containsViewElement) {
        return false;
    }
    final PsiElement parent = tags[0].getParent();
    if (!(parent instanceof XmlTag) || parent.getContainingFile() == null) {
        return false;
    }
    for (int i = 1; i < tags.length; i++) {
        if (tags[i].getParent() != parent) {
            return false;
        }
    }
    return true;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) DomManager(com.intellij.util.xml.DomManager) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

DomElement (com.intellij.util.xml.DomElement)97 XmlTag (com.intellij.psi.xml.XmlTag)34 PsiElement (com.intellij.psi.PsiElement)19 Nullable (org.jetbrains.annotations.Nullable)19 NotNull (org.jetbrains.annotations.NotNull)15 Project (com.intellij.openapi.project.Project)11 PsiFile (com.intellij.psi.PsiFile)11 XmlFile (com.intellij.psi.xml.XmlFile)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)9 ArrayList (java.util.ArrayList)8 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)7 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)6 DomManager (com.intellij.util.xml.DomManager)5 Style (org.jetbrains.android.dom.resources.Style)5 AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)4 XmlElement (com.intellij.psi.xml.XmlElement)4 DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 TextRange (com.intellij.openapi.util.TextRange)3