|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2013 eBusiness Information, Excilys Group |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed To in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.androidannotations.handler; |
| 17 | + |
| 18 | +import com.sun.codemodel.*; |
| 19 | +import org.androidannotations.annotations.EditorAction; |
| 20 | +import org.androidannotations.helper.CanonicalNameConstants; |
| 21 | +import org.androidannotations.model.AnnotationElements; |
| 22 | +import org.androidannotations.process.IsValid; |
| 23 | + |
| 24 | +import javax.annotation.processing.ProcessingEnvironment; |
| 25 | +import javax.lang.model.element.Element; |
| 26 | +import javax.lang.model.element.ExecutableElement; |
| 27 | +import javax.lang.model.element.VariableElement; |
| 28 | +import javax.lang.model.type.TypeKind; |
| 29 | +import javax.lang.model.type.TypeMirror; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class EditorActionHandler extends AbstractListenerHandler { |
| 33 | + |
| 34 | + public EditorActionHandler(ProcessingEnvironment processingEnvironment) { |
| 35 | + super(EditorAction.class, processingEnvironment); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void validate(Element element, AnnotationElements validatedElements, IsValid valid) { |
| 40 | + super.validate(element, validatedElements, valid); |
| 41 | + |
| 42 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 43 | + |
| 44 | + validatorHelper.returnTypeIsVoidOrBoolean(executableElement, valid); |
| 45 | + |
| 46 | + validatorHelper.param.hasAtMostOneTextViewParameter(executableElement, valid); |
| 47 | + |
| 48 | + validatorHelper.param.hasAtMostOneIntegerParameter(executableElement, valid); |
| 49 | + |
| 50 | + validatorHelper.param.hasAtMostOneKeyEventParameter(executableElement, valid); |
| 51 | + |
| 52 | + validatorHelper.param.hasNoOtherParameterFromATextViewAnIntegerAndAKeyEvent(executableElement, valid); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void makeCall(JBlock listenerMethodBody, JInvocation call, TypeMirror returnType) { |
| 57 | + boolean returnMethodResult = returnType.getKind() != TypeKind.VOID; |
| 58 | + |
| 59 | + if (returnMethodResult) { |
| 60 | + listenerMethodBody._return(call); |
| 61 | + } else { |
| 62 | + listenerMethodBody.add(call); |
| 63 | + listenerMethodBody._return(JExpr.TRUE); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void processParameters(JMethod listenerMethod, JInvocation call, List<? extends VariableElement> userParameters) { |
| 69 | + JVar textView = listenerMethod.param(classes().TEXT_VIEW, "textView"); |
| 70 | + JVar actionId = listenerMethod.param(codeModel().INT, "actionId"); |
| 71 | + JVar event = listenerMethod.param(classes().KEY_EVENT, "event"); |
| 72 | + |
| 73 | + for (VariableElement param : userParameters) { |
| 74 | + String paramClassQualifiedName = param.asType().toString(); |
| 75 | + if (paramClassQualifiedName.equals(CanonicalNameConstants.TEXT_VIEW)) { |
| 76 | + call.arg(textView); |
| 77 | + } else if (paramClassQualifiedName.equals(CanonicalNameConstants.INTEGER) || paramClassQualifiedName.equals(codeModel().INT.fullName())) { |
| 78 | + call.arg(actionId); |
| 79 | + } else if(paramClassQualifiedName.equals(CanonicalNameConstants.KEY_EVENT)) { |
| 80 | + call.arg(event); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected JMethod createListenerMethod(JDefinedClass listenerAnonymousClass) { |
| 87 | + return listenerAnonymousClass.method(JMod.PUBLIC, codeModel().BOOLEAN, "onEditorAction"); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected String getSetterName() { |
| 92 | + return "setOnEditorActionListener"; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected JClass getListenerClass() { |
| 97 | + return classes().TEXT_VIEW_ON_EDITOR_ACTION_LISTENER; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected JClass getViewClass() { |
| 102 | + return classes().TEXT_VIEW; |
| 103 | + } |
| 104 | +} |
0 commit comments