|
| 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.processing; |
| 17 | + |
| 18 | +import static com.sun.codemodel.JExpr.invoke; |
| 19 | +import static com.sun.codemodel.JMod.PUBLIC; |
| 20 | + |
| 21 | +import javax.annotation.processing.ProcessingEnvironment; |
| 22 | +import javax.lang.model.element.Element; |
| 23 | + |
| 24 | +import org.androidannotations.annotations.EFragment; |
| 25 | +import org.androidannotations.helper.IdAnnotationHelper; |
| 26 | +import org.androidannotations.helper.ThirdPartyLibHelper; |
| 27 | +import org.androidannotations.processing.EBeansHolder.Classes; |
| 28 | +import org.androidannotations.rclass.IRClass; |
| 29 | + |
| 30 | +import com.sun.codemodel.JBlock; |
| 31 | +import com.sun.codemodel.JClass; |
| 32 | +import com.sun.codemodel.JCodeModel; |
| 33 | +import com.sun.codemodel.JExpr; |
| 34 | +import com.sun.codemodel.JInvocation; |
| 35 | +import com.sun.codemodel.JMethod; |
| 36 | +import com.sun.codemodel.JType; |
| 37 | +import com.sun.codemodel.JVar; |
| 38 | + |
| 39 | +public abstract class AbstractOptionsMenuProcessor implements DecoratingElementProcessor { |
| 40 | + |
| 41 | + protected final ThirdPartyLibHelper libHelper; |
| 42 | + protected final IdAnnotationHelper annotationHelper; |
| 43 | + |
| 44 | + public AbstractOptionsMenuProcessor(ProcessingEnvironment processingEnv, IRClass rClass) { |
| 45 | + annotationHelper = new IdAnnotationHelper(processingEnv, getTarget(), rClass); |
| 46 | + libHelper = new ThirdPartyLibHelper(annotationHelper); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void process(Element element, JCodeModel codeModel, EBeanHolder holder) { |
| 51 | + if (holder.onCreateOptionMenuMethodBody == null) { |
| 52 | + Classes classes = holder.classes(); |
| 53 | + |
| 54 | + boolean isFragment = holder.eBeanAnnotation == EFragment.class; |
| 55 | + |
| 56 | + JClass menuClass; |
| 57 | + JClass menuInflaterClass; |
| 58 | + String getMenuInflaterMethodName; |
| 59 | + if (libHelper.usesActionBarSherlock(holder)) { |
| 60 | + menuClass = classes.SHERLOCK_MENU; |
| 61 | + menuInflaterClass = classes.SHERLOCK_MENU_INFLATER; |
| 62 | + getMenuInflaterMethodName = "getSupportMenuInflater"; |
| 63 | + } else { |
| 64 | + menuClass = classes.MENU; |
| 65 | + menuInflaterClass = classes.MENU_INFLATER; |
| 66 | + getMenuInflaterMethodName = "getMenuInflater"; |
| 67 | + } |
| 68 | + |
| 69 | + JType returnType; |
| 70 | + if (isFragment) { |
| 71 | + returnType = codeModel.VOID; |
| 72 | + } else { |
| 73 | + returnType = codeModel.BOOLEAN; |
| 74 | + } |
| 75 | + |
| 76 | + JBlock body; |
| 77 | + JVar menuInflater; |
| 78 | + JVar menuParam; |
| 79 | + |
| 80 | + JMethod method = holder.generatedClass.method(PUBLIC, returnType, "onCreateOptionsMenu"); |
| 81 | + method.annotate(Override.class); |
| 82 | + |
| 83 | + menuParam = method.param(menuClass, "menu"); |
| 84 | + |
| 85 | + JBlock methodBody = method.body(); |
| 86 | + |
| 87 | + if (isFragment) { |
| 88 | + menuInflater = method.param(menuInflaterClass, "inflater"); |
| 89 | + } else { |
| 90 | + menuInflater = methodBody.decl(menuInflaterClass, "menuInflater", invoke(getMenuInflaterMethodName)); |
| 91 | + } |
| 92 | + |
| 93 | + body = methodBody.block(); |
| 94 | + |
| 95 | + JInvocation superCall = invoke(JExpr._super(), method); |
| 96 | + superCall.arg(menuParam); |
| 97 | + |
| 98 | + if (isFragment) { |
| 99 | + superCall.arg(menuInflater); |
| 100 | + methodBody.add(superCall); |
| 101 | + } else { |
| 102 | + methodBody._return(superCall); |
| 103 | + } |
| 104 | + |
| 105 | + if (isFragment) { |
| 106 | + holder.initBody.invoke("setHasOptionsMenu").arg(JExpr.TRUE); |
| 107 | + } |
| 108 | + |
| 109 | + holder.onCreateOptionMenuMethodBody = body; |
| 110 | + holder.onCreateOptionMenuMenuInflaterVariable = menuInflater; |
| 111 | + holder.onCreateOptionMenuMenuParam = menuParam; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments