Search in sources :

Example 1 with OnClickConverter

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

use of org.jetbrains.android.dom.converters.OnClickConverter 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)2 GenericAttributeValue (com.intellij.util.xml.GenericAttributeValue)2 OnClickConverter (org.jetbrains.android.dom.converters.OnClickConverter)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 ClassFilter (com.intellij.ide.util.ClassFilter)1 TreeClassChooser (com.intellij.ide.util.TreeClassChooser)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlFile (com.intellij.psi.xml.XmlFile)1