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 static com.sun.codemodel.JExpr._new;
 import static com.sun.codemodel.JExpr._null;
 
 import java.util.List;
 
 

Author(s):
Pierre-Yves Ricau
Mathieu Boniface
 
 public class TouchProcessor implements ElementProcessor {
 
 
 	public TouchProcessor(ProcessingEnvironment processingEnvIRClass rClass) {
 		 = new IdAnnotationHelper(processingEnvgetTarget(), rClass);
 	}
 
 	public Class<? extends AnnotationgetTarget() {
 		return Touch.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolder) {
 		EBeanHolder holder = activitiesHolder.getEnclosingEBeanHolder(element);
 		Classes classes = holder.classes();
 
 		String methodName = element.getSimpleName().toString();
 
 		ExecutableElement executableElement = (ExecutableElementelement;
 		List<? extends VariableElementparameters = executableElement.getParameters();
 		TypeMirror returnType = executableElement.getReturnType();
 		boolean returnMethodResult = returnType.getKind() != .;
 
 		boolean hasItemParameter = parameters.size() == 2;
 
 		Touch annotation = element.getAnnotation(Touch.class);
 		List<JFieldRefidsRefs = .extractFieldRefsFromAnnotationValues(elementannotation.value(), "Touched"holder);
 
 		JDefinedClass listenerClass = codeModel.anonymousClass(classes.ON_TOUCH_LISTENER);
 		JMethod listenerMethod = listenerClass.method(.codeModel.BOOLEAN"onTouch");
 		listenerMethod.annotate(Override.class);
 
 		JVar viewParam = listenerMethod.param(classes.VIEW"view");
 		JVar eventParam = listenerMethod.param(classes.MOTION_EVENT"event");
 
 		JBlock listenerMethodBody = listenerMethod.body();
 
 		JInvocation call = JExpr.invoke(methodName);
 
 		if (returnMethodResult) {
 			listenerMethodBody._return(call);
 		} else {
 			listenerMethodBody.add(call);
 			listenerMethodBody._return(.);
 		}
 
 		call.arg(eventParam);
 
 		if (hasItemParameter) {
			call.arg(viewParam);
		}
		for (JFieldRef idRef : idsRefs) {
			JBlock block = holder.afterSetContentView.body().block();
			JInvocation findViewById = JExpr.invoke("findViewById");
			JVar view = block.decl(classes.VIEW"view"findViewById.arg(idRef));
			block._if(view.ne(_null()))._then().invoke(view"setOnTouchListener").arg(_new(listenerClass));
		}
	}
New to GrepCode? Check out our FAQ X