|
33 | 33 | import com.googlecode.androidannotations.rclass.IRClass; |
34 | 34 | import com.googlecode.androidannotations.rclass.IRInnerClass; |
35 | 35 | import com.googlecode.androidannotations.rclass.RClass.Res; |
| 36 | +import com.sun.codemodel.JBlock; |
| 37 | +import com.sun.codemodel.JClass; |
36 | 38 | import com.sun.codemodel.JCodeModel; |
| 39 | +import com.sun.codemodel.JDefinedClass; |
| 40 | +import com.sun.codemodel.JExpr; |
| 41 | +import com.sun.codemodel.JFieldRef; |
| 42 | +import com.sun.codemodel.JInvocation; |
| 43 | +import com.sun.codemodel.JMethod; |
| 44 | +import com.sun.codemodel.JMod; |
| 45 | +import com.sun.codemodel.JVar; |
37 | 46 |
|
38 | 47 | /** |
39 | 48 | * @author Pierre-Yves Ricau |
40 | 49 | */ |
41 | 50 | public class TouchProcessor implements ElementProcessor { |
42 | 51 |
|
43 | | - private final IRClass rClass; |
| 52 | + private final IRClass rClass; |
44 | 53 |
|
45 | | - public TouchProcessor(IRClass rClass) { |
46 | | - this.rClass = rClass; |
47 | | - } |
| 54 | + public TouchProcessor(IRClass rClass) { |
| 55 | + this.rClass = rClass; |
| 56 | + } |
48 | 57 |
|
49 | | - @Override |
50 | | - public Class<? extends Annotation> getTarget() { |
51 | | - return Touch.class; |
52 | | - } |
| 58 | + @Override |
| 59 | + public Class<? extends Annotation> getTarget() { |
| 60 | + return Touch.class; |
| 61 | + } |
53 | 62 |
|
54 | | - @Override |
55 | | - public void process(Element element, MetaModel metaModel) { |
| 63 | + @Override |
| 64 | + public void process(Element element, MetaModel metaModel) { |
56 | 65 |
|
57 | | - String methodName = element.getSimpleName().toString(); |
| 66 | + String methodName = element.getSimpleName().toString(); |
58 | 67 |
|
59 | | - Touch annotation = element.getAnnotation(Touch.class); |
60 | | - int idValue = annotation.value(); |
| 68 | + Touch annotation = element.getAnnotation(Touch.class); |
| 69 | + int idValue = annotation.value(); |
61 | 70 |
|
62 | | - IRInnerClass rInnerClass = rClass.get(Res.ID); |
63 | | - String longClickQualifiedId; |
| 71 | + IRInnerClass rInnerClass = rClass.get(Res.ID); |
| 72 | + String longClickQualifiedId; |
64 | 73 |
|
65 | | - if (idValue == Id.DEFAULT_VALUE) { |
66 | | - String fieldName = element.getSimpleName().toString(); |
67 | | - int lastIndex = fieldName.lastIndexOf("Touched"); |
68 | | - if (lastIndex != -1) { |
69 | | - fieldName = fieldName.substring(0, lastIndex); |
70 | | - } |
71 | | - longClickQualifiedId = rInnerClass.getIdQualifiedName(fieldName); |
72 | | - } else { |
73 | | - longClickQualifiedId = rInnerClass.getIdQualifiedName(idValue); |
74 | | - } |
| 74 | + if (idValue == Id.DEFAULT_VALUE) { |
| 75 | + String fieldName = element.getSimpleName().toString(); |
| 76 | + int lastIndex = fieldName.lastIndexOf("Touched"); |
| 77 | + if (lastIndex != -1) { |
| 78 | + fieldName = fieldName.substring(0, lastIndex); |
| 79 | + } |
| 80 | + longClickQualifiedId = rInnerClass.getIdQualifiedName(fieldName); |
| 81 | + } else { |
| 82 | + longClickQualifiedId = rInnerClass.getIdQualifiedName(idValue); |
| 83 | + } |
75 | 84 |
|
76 | | - Element enclosingElement = element.getEnclosingElement(); |
77 | | - MetaActivity metaActivity = metaModel.getMetaActivities().get(enclosingElement); |
78 | | - List<Instruction> onCreateInstructions = metaActivity.getOnCreateInstructions(); |
| 85 | + Element enclosingElement = element.getEnclosingElement(); |
| 86 | + MetaActivity metaActivity = metaModel.getMetaActivities().get(enclosingElement); |
| 87 | + List<Instruction> onCreateInstructions = metaActivity.getOnCreateInstructions(); |
79 | 88 |
|
80 | | - ExecutableElement executableElement = (ExecutableElement) element; |
81 | | - List<? extends VariableElement> parameters = executableElement.getParameters(); |
| 89 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 90 | + List<? extends VariableElement> parameters = executableElement.getParameters(); |
82 | 91 |
|
83 | | - boolean viewParameter = parameters.size() == 2; |
| 92 | + boolean viewParameter = parameters.size() == 2; |
84 | 93 |
|
85 | | - TypeMirror returnType = executableElement.getReturnType(); |
| 94 | + TypeMirror returnType = executableElement.getReturnType(); |
86 | 95 |
|
87 | | - boolean returnMethodResult = returnType.getKind() != TypeKind.VOID; |
| 96 | + boolean returnMethodResult = returnType.getKind() != TypeKind.VOID; |
88 | 97 |
|
89 | | - Instruction instruction = new TouchInstruction(methodName, longClickQualifiedId, viewParameter, returnMethodResult); |
90 | | - onCreateInstructions.add(instruction); |
91 | | - } |
| 98 | + Instruction instruction = new TouchInstruction(methodName, longClickQualifiedId, viewParameter, returnMethodResult); |
| 99 | + onCreateInstructions.add(instruction); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) { |
| 104 | + ActivityHolder holder = activitiesHolder.getActivityHolder(element); |
| 105 | + |
| 106 | + String methodName = element.getSimpleName().toString(); |
| 107 | + |
| 108 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 109 | + List<? extends VariableElement> parameters = executableElement.getParameters(); |
| 110 | + TypeMirror returnType = executableElement.getReturnType(); |
| 111 | + boolean returnMethodResult = returnType.getKind() != TypeKind.VOID; |
| 112 | + |
| 113 | + boolean hasItemParameter = parameters.size() == 2; |
| 114 | + |
| 115 | + JFieldRef idRef = extractClickQualifiedId(element, codeModel); |
| 116 | + |
| 117 | + JDefinedClass listenerClass = codeModel.anonymousClass(codeModel.ref("android.view.View.OnTouchListener")); |
| 118 | + JMethod listenerMethod = listenerClass.method(JMod.PUBLIC, codeModel.BOOLEAN, "onTouch"); |
| 119 | + JClass viewClass = codeModel.ref("android.view.View"); |
| 120 | + JClass motionEventClass = codeModel.ref("android.view.MotionEvent"); |
| 121 | + |
| 122 | + JVar viewParam = listenerMethod.param(viewClass, "view"); |
| 123 | + JVar eventParam = listenerMethod.param(motionEventClass, "event"); |
| 124 | + |
| 125 | + JBlock listenerMethodBody = listenerMethod.body(); |
| 126 | + |
| 127 | + JInvocation call = JExpr.invoke(methodName); |
| 128 | + |
| 129 | + if (returnMethodResult) { |
| 130 | + listenerMethodBody._return(call); |
| 131 | + } else { |
| 132 | + listenerMethodBody.add(call); |
| 133 | + listenerMethodBody._return(JExpr.TRUE); |
| 134 | + } |
| 135 | + |
| 136 | + call.arg(eventParam); |
| 137 | + |
| 138 | + if (hasItemParameter) { |
| 139 | + call.arg(viewParam); |
| 140 | + } |
| 141 | + |
| 142 | + JBlock body = holder.afterSetContentView.body(); |
| 143 | + |
| 144 | + body.add(JExpr.invoke(JExpr.invoke("findViewById").arg(idRef), "setOnTouchListener").arg(JExpr._new(listenerClass))); |
| 145 | + } |
| 146 | + |
| 147 | + private JFieldRef extractClickQualifiedId(Element element, JCodeModel codeModel) { |
| 148 | + Touch annotation = element.getAnnotation(Touch.class); |
| 149 | + int idValue = annotation.value(); |
| 150 | + IRInnerClass rInnerClass = rClass.get(Res.ID); |
| 151 | + if (idValue == Id.DEFAULT_VALUE) { |
| 152 | + String fieldName = element.getSimpleName().toString(); |
| 153 | + int lastIndex = fieldName.lastIndexOf("Touch"); |
| 154 | + if (lastIndex != -1) { |
| 155 | + fieldName = fieldName.substring(0, lastIndex); |
| 156 | + } |
| 157 | + return rInnerClass.getIdStaticRef(fieldName, codeModel); |
| 158 | + } else { |
| 159 | + return rInnerClass.getIdStaticRef(idValue, codeModel); |
| 160 | + } |
| 161 | + } |
92 | 162 |
|
93 | | - @Override |
94 | | - public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) { |
95 | | - // TODO Auto-generated method stub |
96 | | - |
97 | | - } |
98 | 163 | } |
0 commit comments