Search in sources :

Example 1 with AndroidResourceReferenceBase

use of org.jetbrains.android.dom.converters.AndroidResourceReferenceBase in project android by JetBrains.

the class AndroidInlineStyleReferenceAction method doRefactorForTags.

@Override
protected void doRefactorForTags(@NotNull Project project, @NotNull final XmlTag[] tags) {
    assert tags.length == 1;
    final XmlTag tag = tags[0];
    final PsiFile file = tag.getContainingFile();
    if (file == null) {
        return;
    }
    final StyleUsageData usageData = AndroidInlineUtil.getStyleUsageData(tag);
    if (usageData == null) {
        return;
    }
    final AndroidResourceReferenceBase reference = usageData.getReference();
    final String title = AndroidBundle.message("android.inline.style.title");
    final PsiElement[] styleElements = reference.computeTargetElements();
    if (styleElements.length == 0) {
        AndroidUtils.reportError(project, "Cannot find style by reference '" + reference.getValue() + "'", title);
        return;
    }
    if (styleElements.length > 1) {
        AndroidUtils.reportError(project, RefactoringBundle.getCannotRefactorMessage("Unambiguous style reference."), title);
        return;
    }
    final PsiElement styleElement = styleElements[0];
    final XmlTag styleTag = PsiTreeUtil.getParentOfType(styleElement, XmlTag.class);
    final DomElement domElement = styleTag != null ? DomManager.getDomManager(project).getDomElement(styleTag) : null;
    if (!(domElement instanceof Style)) {
        AndroidUtils.reportError(project, "Cannot find style by reference '" + reference.getValue() + "'", title);
        return;
    }
    final Style style = (Style) domElement;
    String styleName = style.getName().getStringValue();
    if (styleName == null) {
        AndroidUtils.reportError(project, RefactoringBundle.getCannotRefactorMessage("Style name is not specified."), title);
        return;
    }
    AndroidInlineUtil.doInlineStyleDeclaration(project, new AndroidInlineUtil.MyStyleData(styleName, style, styleElement), usageData, new ProjectBasedErrorReporter(project), myTestConfig);
}
Also used : DomElement(com.intellij.util.xml.DomElement) ProjectBasedErrorReporter(org.jetbrains.android.util.ProjectBasedErrorReporter) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) Style(org.jetbrains.android.dom.resources.Style) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with AndroidResourceReferenceBase

use of org.jetbrains.android.dom.converters.AndroidResourceReferenceBase 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 3 with AndroidResourceReferenceBase

use of org.jetbrains.android.dom.converters.AndroidResourceReferenceBase in project android by JetBrains.

the class AndroidInlineLayoutHandler method inlineElement.

@Override
public void inlineElement(Project project, Editor editor, PsiElement element) {
    if (element instanceof ResourceElementWrapper) {
        element = ((ResourceElementWrapper) element).getWrappee();
    }
    if (element instanceof XmlFile) {
        PsiElement usageElement = null;
        if (editor != null) {
            final PsiReference reference = TargetElementUtil.findReference(editor);
            if (reference != null) {
                usageElement = reference.getElement();
            }
        }
        AndroidInlineUtil.doInlineLayoutFile(project, (XmlFile) element, usageElement, ourTestConfig);
        return;
    }
    final LayoutUsageData usageData = getLayoutUsageDataFromContext(editor);
    assert usageData != null;
    final AndroidResourceReferenceBase ref = usageData.getReference();
    final PsiElement[] elements = ref.computeTargetElements();
    final ErrorReporter errorReporter = editor != null ? new HintBasedErrorReporter(editor) : new ProjectBasedErrorReporter(project);
    final String title = AndroidBundle.message("android.inline.layout.title");
    if (elements.length == 0) {
        final String resName = ref.getResourceValue().getResourceName();
        final String message = resName != null ? "Cannot find layout '" + resName + "'" : "Error: cannot find the layout";
        errorReporter.report(message, title);
        return;
    }
    if (elements.length > 1) {
        errorReporter.report("Error: unambiguous reference", title);
        return;
    }
    final PsiElement resolvedElement = elements[0];
    if (!(resolvedElement instanceof XmlFile)) {
        errorReporter.report("Cannot inline reference '" + ref.getValue() + "'", title);
        return;
    }
    AndroidInlineUtil.doInlineLayoutFile(project, (XmlFile) resolvedElement, ref.getElement(), ourTestConfig);
}
Also used : HintBasedErrorReporter(org.jetbrains.android.util.HintBasedErrorReporter) ProjectBasedErrorReporter(org.jetbrains.android.util.ProjectBasedErrorReporter) ErrorReporter(org.jetbrains.android.util.ErrorReporter) ProjectBasedErrorReporter(org.jetbrains.android.util.ProjectBasedErrorReporter) XmlFile(com.intellij.psi.xml.XmlFile) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) ResourceElementWrapper(org.jetbrains.android.dom.wrappers.ResourceElementWrapper) PsiReference(com.intellij.psi.PsiReference) HintBasedErrorReporter(org.jetbrains.android.util.HintBasedErrorReporter) PsiElement(com.intellij.psi.PsiElement)

Example 4 with AndroidResourceReferenceBase

use of org.jetbrains.android.dom.converters.AndroidResourceReferenceBase in project android by JetBrains.

the class AndroidInlineStyleHandler method inlineElement.

@Override
public void inlineElement(Project project, final Editor editor, PsiElement element) {
    final AndroidInlineUtil.MyStyleData data = AndroidInlineUtil.getInlinableStyleDataFromContext(element);
    if (data != null) {
        final ErrorReporter reporter = editor != null ? new HintBasedErrorReporter(editor) : new ProjectBasedErrorReporter(project);
        StyleUsageData usageData = null;
        if (editor != null) {
            final PsiReference reference = TargetElementUtil.findReference(editor);
            if (reference instanceof AndroidResourceReferenceBase) {
                usageData = getUsageDataFromEditor(reference);
            }
        }
        AndroidInlineUtil.doInlineStyleDeclaration(project, data, usageData, reporter, ourTestConfig);
    }
}
Also used : HintBasedErrorReporter(org.jetbrains.android.util.HintBasedErrorReporter) ProjectBasedErrorReporter(org.jetbrains.android.util.ProjectBasedErrorReporter) ErrorReporter(org.jetbrains.android.util.ErrorReporter) ProjectBasedErrorReporter(org.jetbrains.android.util.ProjectBasedErrorReporter) AndroidResourceReferenceBase(org.jetbrains.android.dom.converters.AndroidResourceReferenceBase) PsiReference(com.intellij.psi.PsiReference) HintBasedErrorReporter(org.jetbrains.android.util.HintBasedErrorReporter)

Example 5 with AndroidResourceReferenceBase

use of org.jetbrains.android.dom.converters.AndroidResourceReferenceBase 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)

Aggregations

AndroidResourceReferenceBase (org.jetbrains.android.dom.converters.AndroidResourceReferenceBase)6 PsiElement (com.intellij.psi.PsiElement)3 DomElement (com.intellij.util.xml.DomElement)3 ProjectBasedErrorReporter (org.jetbrains.android.util.ProjectBasedErrorReporter)3 PsiFile (com.intellij.psi.PsiFile)2 PsiReference (com.intellij.psi.PsiReference)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ResourceValue (org.jetbrains.android.dom.resources.ResourceValue)2 Style (org.jetbrains.android.dom.resources.Style)2 ErrorReporter (org.jetbrains.android.util.ErrorReporter)2 HintBasedErrorReporter (org.jetbrains.android.util.HintBasedErrorReporter)2 Nullable (org.jetbrains.annotations.Nullable)2 Project (com.intellij.openapi.project.Project)1 Include (org.jetbrains.android.dom.layout.Include)1 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)1 ResourceElementWrapper (org.jetbrains.android.dom.wrappers.ResourceElementWrapper)1