Search in sources :

Example 1 with LayoutViewElement

use of org.jetbrains.android.dom.layout.LayoutViewElement in project android by JetBrains.

the class AndroidInlineUtil method getStyleUsageData.

@Nullable
static StyleUsageData getStyleUsageData(@NotNull XmlTag tag) {
    final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    if (domElement instanceof LayoutViewElement) {
        final GenericAttributeValue<ResourceValue> styleAttribute = ((LayoutViewElement) domElement).getStyle();
        final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(styleAttribute, true);
        if (reference != null) {
            return new ViewStyleUsageData(tag, styleAttribute, reference);
        }
    } else if (domElement instanceof Style) {
        final AndroidResourceReferenceBase reference = AndroidDomUtil.getAndroidResourceReference(((Style) domElement).getParentStyle(), true);
        if (reference != null) {
            return new ParentStyleUsageData((Style) domElement, reference);
        }
    }
    return null;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) Style(org.jetbrains.android.dom.resources.Style) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with LayoutViewElement

use of org.jetbrains.android.dom.layout.LayoutViewElement in project android by JetBrains.

the class AndroidFindStyleApplicationsProcessor method isPossibleApplicationOfStyle.

private boolean isPossibleApplicationOfStyle(XmlTag candidate) {
    final DomElement domCandidate = DomManager.getDomManager(myProject).getDomElement(candidate);
    if (!(domCandidate instanceof LayoutViewElement)) {
        return false;
    }
    final LayoutViewElement candidateView = (LayoutViewElement) domCandidate;
    final Map<Pair<String, String>, String> attrsInCandidateMap = new HashMap<Pair<String, String>, String>();
    final List<XmlAttribute> attrsInCandidate = AndroidExtractStyleAction.getExtractableAttributes(candidate);
    if (attrsInCandidate.size() < myAttrMap.size()) {
        return false;
    }
    for (XmlAttribute attribute : attrsInCandidate) {
        final String attrValue = attribute.getValue();
        if (attrValue != null) {
            attrsInCandidateMap.put(Pair.create(attribute.getNamespace(), attribute.getLocalName()), attrValue);
        }
    }
    for (Map.Entry<AndroidAttributeInfo, String> entry : myAttrMap.entrySet()) {
        final String ns = entry.getKey().getNamespace();
        final String name = entry.getKey().getName();
        final String value = entry.getValue();
        final String valueInCandidate = attrsInCandidateMap.get(Pair.create(ns, name));
        if (valueInCandidate == null || !valueInCandidate.equals(value)) {
            return false;
        }
    }
    if (candidateView.getStyle().getStringValue() != null) {
        if (myParentStyleNameAttrValue == null) {
            return false;
        }
        final PsiElement styleNameAttrValueForTag = getStyleNameAttrValueForTag(candidateView);
        if (styleNameAttrValueForTag == null || !myParentStyleNameAttrValue.equals(styleNameAttrValueForTag)) {
            return false;
        }
    } else if (myParentStyleNameAttrValue != null) {
        return false;
    }
    return true;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) HashMap(com.intellij.util.containers.HashMap) HashMap(com.intellij.util.containers.HashMap) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 3 with LayoutViewElement

use of org.jetbrains.android.dom.layout.LayoutViewElement in project android by JetBrains.

the class StructureViewTest method testLayoutStructureOrder2.

public void testLayoutStructureOrder2() throws Exception {
    VirtualFile file = copyFileToProject("layout/structure_view_test_order_2.xml", "/res/layout/layout.xml");
    PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
    assertInstanceOf(psiFile, XmlFile.class);
    DomFileElement<LayoutViewElement> element = DomManager.getDomManager(getProject()).getFileElement(((XmlFile) psiFile), LayoutViewElement.class);
    assertNotNull(element);
    final StructureViewModel model = new LayoutStructureViewBuilder(element).createStructureViewModel(null);
    String expected = "LinearLayout\n" + "  Include\n" + "  Fragment\n";
    assertEquals(expected, model.getRoot().toString());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlFile(com.intellij.psi.xml.XmlFile) StructureViewModel(com.intellij.ide.structureView.StructureViewModel) PsiFile(com.intellij.psi.PsiFile) LayoutStructureViewBuilder(org.jetbrains.android.dom.structure.layout.LayoutStructureViewBuilder)

Example 4 with LayoutViewElement

use of org.jetbrains.android.dom.layout.LayoutViewElement in project android by JetBrains.

the class RtlSupportProcessor method getLayoutRefactoringForTag.

private List<UsageInfo> getLayoutRefactoringForTag(@NotNull XmlTag tag, boolean createV17, int minSdk) {
    final DomElement domElement = DomManager.getDomManager(myProject).getDomElement(tag);
    if (!(domElement instanceof LayoutViewElement)) {
        return Collections.emptyList();
    }
    final List<UsageInfo> result = new ArrayList<UsageInfo>();
    final XmlAttribute[] attributes = tag.getAttributes();
    for (XmlAttribute attributeToMirror : attributes) {
        final String localName = attributeToMirror.getLocalName();
        final String namespacePrefix = attributeToMirror.getNamespacePrefix();
        final String mirroredLocalName = ourMapMirroredAttributeName.get(localName);
        // Check if this is a RTL attribute to mirror or if it is a Gravity attribute
        if (mirroredLocalName != null) {
            // Mirror only attributes that has not been mirrored before
            final XmlAttribute attributeMirrored = tag.getAttribute(namespacePrefix + ":" + mirroredLocalName);
            if (attributeMirrored == null) {
                final int startOffset = 0;
                final int endOffset = attributeToMirror.getTextLength();
                RtlRefactoringUsageInfo usageInfoForAttribute = new RtlRefactoringUsageInfo(attributeToMirror, startOffset, endOffset);
                usageInfoForAttribute.setType(LAYOUT_FILE_ATTRIBUTE);
                usageInfoForAttribute.setCreateV17(createV17);
                usageInfoForAttribute.setAndroidManifestMinSdkVersion(minSdk);
                result.add(usageInfoForAttribute);
            }
        } else if (localName.equals(ATTR_GRAVITY) || localName.equals(ATTR_LAYOUT_GRAVITY)) {
            final String value = attributeToMirror.getValue();
            if (value != null && (value.contains(GRAVITY_VALUE_LEFT) || value.contains(GRAVITY_VALUE_RIGHT))) {
                final int startOffset = 0;
                final int endOffset = attributeToMirror.getTextLength();
                RtlRefactoringUsageInfo usageInfoForAttribute = new RtlRefactoringUsageInfo(attributeToMirror, startOffset, endOffset);
                usageInfoForAttribute.setType(LAYOUT_FILE_ATTRIBUTE);
                usageInfoForAttribute.setCreateV17(createV17);
                result.add(usageInfoForAttribute);
            }
        }
    }
    return result;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo)

Example 5 with LayoutViewElement

use of org.jetbrains.android.dom.layout.LayoutViewElement in project android by JetBrains.

the class AndroidExtractAsIncludeAction method isEnabledForPsiRange.

@Override
protected boolean isEnabledForPsiRange(@NotNull PsiElement from, @Nullable PsiElement to) {
    final DomManager domManager = DomManager.getDomManager(from.getProject());
    PsiElement e = from;
    boolean containsViewElement = false;
    while (e != null) {
        if (e instanceof XmlTag) {
            final DomElement domElement = domManager.getDomElement((XmlTag) e);
            if (!isSuitableDomElement(domElement)) {
                return false;
            }
            if (domElement instanceof LayoutViewElement) {
                containsViewElement = true;
            }
        }
        if (e == to) {
            break;
        }
        e = e.getNextSibling();
    }
    return containsViewElement;
}
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

LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)12 DomElement (com.intellij.util.xml.DomElement)7 PsiFile (com.intellij.psi.PsiFile)6 XmlTag (com.intellij.psi.xml.XmlTag)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 XmlFile (com.intellij.psi.xml.XmlFile)4 Nullable (org.jetbrains.annotations.Nullable)4 StructureViewModel (com.intellij.ide.structureView.StructureViewModel)3 DomManager (com.intellij.util.xml.DomManager)3 LayoutStructureViewBuilder (org.jetbrains.android.dom.structure.layout.LayoutStructureViewBuilder)3 Project (com.intellij.openapi.project.Project)2 Pair (com.intellij.openapi.util.Pair)2 PsiElement (com.intellij.psi.PsiElement)2 UsageInfo (com.intellij.usageView.UsageInfo)2 HashSet (com.intellij.util.containers.HashSet)2 ResourceValue (org.jetbrains.android.dom.resources.ResourceValue)2 Style (org.jetbrains.android.dom.resources.Style)2 XmlResourceElement (org.jetbrains.android.dom.xml.XmlResourceElement)2 Result (com.intellij.openapi.application.Result)1