Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2012 eBusiness Information, Excilys Group Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed To in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.googlecode.androidannotations.processing;
 
 import java.util.List;
 
 

Author(s):
Mathieu Boniface
 
 public class TextChangeProcessor implements ElementProcessor {
 
 	private final TextWatcherHelper helper;
 
 
 	public TextChangeProcessor(ProcessingEnvironment processingEnvIRClass rClass) {
 		 = new TextWatcherHelper(processingEnvgetTarget(), rClass);
 	}
 
 	public Class<? extends AnnotationgetTarget() {
 		return TextChange.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolder) {
 
 		EBeanHolder holder = activitiesHolder.getEnclosingEBeanHolder(element);
 
 		String methodName = element.getSimpleName().toString();
 
 		ExecutableElement executableElement = (ExecutableElementelement;
 		List<? extends VariableElementparameters = executableElement.getParameters();
 
 		int startParameterPosition = -1;
 		int countParameterPosition = -1;
 		int beforeParameterPosition = -1;
 		int charSequenceParameterPosition = -1;
 		int viewParameterPosition = -1;
 		TypeMirror viewParameterType = null;
 
 		for (int i = 0; i < parameters.size(); i++) {
 			VariableElement parameter = parameters.get(i);
 			String parameterName = parameter.toString();
 			TypeMirror parameterType = parameter.asType();
 
 			if ("java.lang.CharSequence".equals(parameterType.toString())) {
 				charSequenceParameterPosition = i;
 			} else if (parameterType.getKind() == . || "java.lang.integer".equals(parameterType.toString())) {
 				if ("start".equals(parameterName)) {
 					startParameterPosition = i;
 				} else if ("count".equals(parameterName)) {
 					countParameterPosition = i;
 				} else if ("before".equals(parameterName)) {
 					beforeParameterPosition = i;
 				}
 			} else {
 				TypeMirror textViewType = .typeElementFromQualifiedName("android.widget.TextView").asType();
 				if (.isSubtype(parameterTypetextViewType)) {
 					viewParameterPosition = i;
 					viewParameterType = parameterType;
 				}
 			}
 		}
 
		TextChange annotation = element.getAnnotation(TextChange.class);
		List<JFieldRefidsRefs = .extractFieldRefsFromAnnotationValues(elementannotation.value(), "TextChanged"holder);
		for (JFieldRef idRef : idsRefs) {
			TextWatcherHolder textWatcherHolder = .getOrCreateListener(codeModelholderidRefviewParameterType);
			JInvocation textChangeCall;
			JMethod methodToCall = textWatcherHolder.onTextChangedMethod;
			JBlock previousBody = .removeBody(methodToCall);
			JBlock methodBody = methodToCall.body();
			methodBody.add(previousBody);
			JExpression activityRef = holder.eBean.staticRef("this");
			textChangeCall = methodBody.invoke(activityRefmethodName);
			for (int i = 0; i < parameters.size(); i++) {
				if (i == startParameterPosition) {
					JVar startParameter = .findParameterByName(methodToCall"start");
					textChangeCall.arg(startParameter);
else if (i == countParameterPosition) {
					JVar countParameter = .findParameterByName(methodToCall"count");
					textChangeCall.arg(countParameter);
else if (i == beforeParameterPosition) {
					JVar beforeParameter = .findParameterByName(methodToCall"before");
					textChangeCall.arg(beforeParameter);
else if (i == charSequenceParameterPosition) {
					JVar charSequenceParam = .findParameterByName(methodToCall"s");
					textChangeCall.arg(charSequenceParam);
else if (i == viewParameterPosition) {
					JVar viewParameter = textWatcherHolder.viewVariable;
					textChangeCall.arg(viewParameter);
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X