Search in sources :

Example 61 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class SetAttributeQuickFix method apply.

@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
    final XmlTag tag = PsiTreeUtil.getParentOfType(startElement, XmlTag.class, false);
    if (tag == null) {
        return;
    }
    String value = myValue;
    if (value == null && context instanceof AndroidQuickfixContexts.DesignerContext) {
        value = askForAttributeValue(tag);
        if (value == null) {
            return;
        }
    }
    if (myNamespace != null) {
        XmlFile file = PsiTreeUtil.getParentOfType(tag, XmlFile.class);
        if (file != null) {
            AndroidResourceUtil.ensureNamespaceImported(file, myNamespace, null);
        }
    }
    final XmlAttribute attribute = myNamespace != null ? tag.setAttribute(myAttributeName, myNamespace, "") : tag.setAttribute(myAttributeName, "");
    if (attribute != null) {
        if (value != null) {
            attribute.setValue(value);
        }
        if (context instanceof AndroidQuickfixContexts.EditorContext) {
            final Editor editor = ((AndroidQuickfixContexts.EditorContext) context).getEditor();
            final XmlAttributeValue valueElement = attribute.getValueElement();
            final TextRange valueTextRange = attribute.getValueTextRange();
            if (valueElement != null) {
                final int valueElementStart = valueElement.getTextRange().getStartOffset();
                editor.getCaretModel().moveToOffset(valueElementStart + valueTextRange.getStartOffset());
                if (valueTextRange.getStartOffset() < valueTextRange.getEndOffset()) {
                    editor.getSelectionModel().setSelection(valueElementStart + valueTextRange.getStartOffset(), valueElementStart + valueTextRange.getEndOffset());
                }
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 62 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidDomUtil method getAndroidResourceReference.

@Nullable
public static AndroidResourceReferenceBase getAndroidResourceReference(@Nullable GenericAttributeValue<ResourceValue> attribute, boolean localOnly) {
    if (attribute == null) {
        return null;
    }
    final ResourceValue resValue = attribute.getValue();
    if (resValue == null || (localOnly && resValue.getNamespace() != null)) {
        return null;
    }
    final XmlAttributeValue value = attribute.getXmlAttributeValue();
    if (value == null) {
        return null;
    }
    for (PsiReference reference : value.getReferences()) {
        if (reference instanceof AndroidResourceReferenceBase) {
            return (AndroidResourceReferenceBase) reference;
        }
    }
    return null;
}
Also used : ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidXmlDocumentationProvider method getDocumentationElementForLookupItem.

@Override
public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element) {
    if (object instanceof ResourceReferenceConverter.DocumentationHolder) {
        final ResourceReferenceConverter.DocumentationHolder holder = (ResourceReferenceConverter.DocumentationHolder) object;
        return new ProvidedDocumentationPsiElement(psiManager, Language.ANY, holder.getValue(), holder.getDocumentation());
    }
    if (!(element instanceof XmlAttributeValue) || !(object instanceof String)) {
        return null;
    }
    final String value = (String) object;
    final PsiElement parent = element.getParent();
    if (!(parent instanceof XmlAttribute)) {
        return null;
    }
    final GenericAttributeValue domValue = DomManager.getDomManager(parent.getProject()).getDomElement((XmlAttribute) parent);
    if (domValue == null) {
        return null;
    }
    final Converter converter = domValue.getConverter();
    if (converter instanceof AttributeValueDocumentationProvider) {
        final String doc = ((AttributeValueDocumentationProvider) converter).getDocumentation(value);
        if (doc != null) {
            return new MyDocElement(element, doc);
        }
    }
    if ((value.startsWith(PREFIX_RESOURCE_REF) || value.startsWith(PREFIX_THEME_REF)) && !DataBindingUtil.isBindingExpression(value)) {
        return new MyResourceElement(element, value);
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ResourceReferenceConverter(org.jetbrains.android.dom.converters.ResourceReferenceConverter) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AttributeValueDocumentationProvider(org.jetbrains.android.dom.converters.AttributeValueDocumentationProvider) ResourceReferenceConverter(org.jetbrains.android.dom.converters.ResourceReferenceConverter) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement)

Example 64 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidCreateOnClickHandlerAction method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final AndroidFacet facet = AndroidFacet.getInstance(file);
    assert facet != null;
    final XmlAttributeValue attrValue = getXmlAttributeValue(file, editor);
    assert attrValue != null;
    final String methodName = attrValue.getValue();
    assert methodName != null;
    final GenericAttributeValue domValue = DomManager.getDomManager(project).getDomElement((XmlAttribute) attrValue.getParent());
    assert domValue != null;
    final OnClickConverter converter = (OnClickConverter) domValue.getConverter();
    final PsiClass activityBaseClass = JavaPsiFacade.getInstance(project).findClass(AndroidUtils.ACTIVITY_BASE_CLASS_NAME, facet.getModule().getModuleWithDependenciesAndLibrariesScope(false));
    if (activityBaseClass == null) {
        return;
    }
    final GlobalSearchScope scope = facet.getModule().getModuleScope(false);
    final PsiClass selectedClass;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        final Ref<PsiClass> selClassRef = Ref.create();
        ClassInheritorsSearch.search(activityBaseClass, scope, true, true, false).forEach(new Processor<PsiClass>() {

            @Override
            public boolean process(PsiClass psiClass) {
                if (!psiClass.isInterface() && !psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
                    selClassRef.set(psiClass);
                    return false;
                }
                return true;
            }
        });
        selectedClass = selClassRef.get();
    } else {
        final TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project).createInheritanceClassChooser("Choose Activity to Create the Method", scope, activityBaseClass, null, new ClassFilter() {

            @Override
            public boolean isAccepted(PsiClass aClass) {
                return !converter.findHandlerMethod(aClass, methodName);
            }
        });
        chooser.showDialog();
        selectedClass = chooser.getSelected();
    }
    if (selectedClass != null) {
        addHandlerMethodAndNavigate(project, selectedClass, methodName, converter.getDefaultMethodParameterType(selectedClass));
    }
}
Also used : OnClickConverter(org.jetbrains.android.dom.converters.OnClickConverter) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassFilter(com.intellij.ide.util.ClassFilter) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Example 65 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidCreateOnClickHandlerAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (editor == null || !(file instanceof XmlFile)) {
        return false;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(file);
    if (facet == null) {
        return false;
    }
    final XmlAttributeValue attrValue = getXmlAttributeValue(file, editor);
    if (attrValue == null) {
        return false;
    }
    final PsiElement parent = attrValue.getParent();
    if (!(parent instanceof XmlAttribute)) {
        return false;
    }
    final GenericAttributeValue domValue = DomManager.getDomManager(project).getDomElement((XmlAttribute) parent);
    if (domValue == null || !(domValue.getConverter() instanceof OnClickConverter)) {
        return false;
    }
    final String methodName = attrValue.getValue();
    return methodName != null && StringUtil.isJavaIdentifier(methodName);
}
Also used : OnClickConverter(org.jetbrains.android.dom.converters.OnClickConverter) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5