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());
}
}
}
}
}
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;
}
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;
}
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));
}
}
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);
}
Aggregations