Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2011 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):
Benjamin Fellous
Pierre-Yves Ricau
Mathieu Boniface
 
 
 	public ItemLongClickProcessor(IRClass rClass) {
 		super(rClass);
 	}
 
 	public Class<? extends AnnotationgetTarget() {
 		return ItemLongClick.class;
 	}
 
 	public void process(Element elementJCodeModel codeModelEBeansHolder activitiesHolder) {
 		EBeanHolder holder = activitiesHolder.getEnclosingActivityHolder(element);
 
 		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() == 1;
 
 		ItemLongClick annotation = element.getAnnotation(ItemLongClick.class);
 		List<JFieldRefidsRefs = extractQualifiedIds(elementannotation.value(), "ItemLongClicked"holder);
 
 		JDefinedClass onItemClickListenerClass = codeModel.anonymousClass(holder.refClass("android.widget.AdapterView.OnItemLongClickListener"));
 		JMethod onItemLongClickMethod = onItemClickListenerClass.method(.codeModel.BOOLEAN"onItemLongClick");
 		JClass adapterViewClass = holder.refClass("android.widget.AdapterView");
 		JClass viewClass = holder.refClass("android.view.View");
 
 		JClass narrowAdapterViewClass = adapterViewClass.narrow(codeModel.wildcard());
 		JVar onItemClickParentParam = onItemLongClickMethod.param(narrowAdapterViewClass"parent");
 		onItemLongClickMethod.param(viewClass"view");
 		JVar onItemClickPositionParam = onItemLongClickMethod.param(codeModel.INT"position");
 		onItemLongClickMethod.param(codeModel.LONG"id");
 
 		JBlock onItemLongClickBody = onItemLongClickMethod.body();
 
 		JInvocation itemClickCall = JExpr.invoke(methodName);
 
 		if (returnMethodResult) {
 			onItemLongClickBody._return(itemClickCall);
 		} else {
 			onItemLongClickBody.add(itemClickCall);
 			onItemLongClickBody._return(.);
 		}
 
 		if (hasItemParameter) {
 			VariableElement parameter = parameters.get(0);
 			String parameterQualifiedName = parameter.asType().toString();
 			itemClickCall.arg(JExpr.cast(holder.refClass(parameterQualifiedName), JExpr.invoke(onItemClickParentParam"getAdapter").invoke("getItem").arg(onItemClickPositionParam)));
 		}
 
		for (JFieldRef idRef : idsRefs) {
			JBlock block = holder.afterSetContentView.body().block();
			JVar view = block.decl(narrowAdapterViewClass"view", JExpr.cast(narrowAdapterViewClass, JExpr.invoke("findViewById").arg(idRef)));
			block._if(view.ne(JExpr._null()))._then().invoke(view"setOnItemLongClickListener").arg(JExpr._new(onItemClickListenerClass));
		}
	}
New to GrepCode? Check out our FAQ X