private void genInitFields( Refs r, Map<Ref, JFieldVar> fieldVarMap, JVar viewVar, Collection<Ref> refs, JBlock body) { for (Ref ref : refs) { JFieldVar fieldVar = fieldVarMap.get(ref); JFieldRef idVar = (ref.isAndroidId ? r.androidRClass : r.rClass).staticRef("id").ref(ref.id); if (ref instanceof View) { JClass viewType = r.ref(((View) ref).type); body.assign(fieldVar, cast(viewType, viewVar.invoke("findViewById").arg(idVar))); } else if (ref instanceof Include) { JClass includeType = r.ref(getClassName(((Include) ref).layout)); body.assign(fieldVar, _new(includeType).arg(viewVar)); } } }
private Map<Ref, JFieldVar> genFields(Refs r, JDefinedClass clazz, Collection<Ref> refs) { Map<Ref, JFieldVar> fieldVarMap = new LinkedHashMap<Ref, JFieldVar>(); for (Ref ref : refs) { String idPackage = (ref.isAndroidId ? "android" : r.packageName) + ".R"; JFieldVar var; if (ref instanceof View) { var = clazz.field(PUBLIC, r.ref(((View) ref).type), ref.fieldName); var.javadoc().append("View for {@link " + idPackage + ".id#" + ref.id + "}."); if (ref.isNullable) { var.annotate(r.nullableAnnotation); } } else if (ref instanceof Include) { var = clazz.field(PUBLIC, r.ref(getClassName(((Include) ref).layout)), ref.fieldName); var.javadoc() .append("Holdr for {@link " + idPackage + ".layout#" + ((Include) ref).layout + "}."); } else { throw new IllegalArgumentException("Unknown ref: " + ref); } fieldVarMap.put(ref, var); } return fieldVarMap; }
private JClass genListenerInterface( Refs r, JDefinedClass clazz, Collection<Ref> refs, Map<Listener.Type, ListenerType> listenerTypeMap) throws JClassAlreadyExistsException { JDefinedClass listenerInterface = null; for (Ref ref : refs) { if (ref instanceof View) { View view = (View) ref; if (!view.listeners.isEmpty()) { if (listenerInterface == null) { listenerInterface = clazz._interface(PUBLIC, "Listener"); } for (Listener listener : view.listeners) { ListenerType listenerType = listenerTypeMap.get(listener.type); JMethod method = listenerInterface.method(PUBLIC, listenerType.methodReturn, listener.name); for (Pair<JType, String> param : listenerType.methodParams) { if (param.second.equals("view")) { // Replace view with reference to the field. method.param(r.ref(view.type), ref.fieldName); } else { method.param(param.first, param.second); } } } } } } return listenerInterface; }
public ListenerType(Refs r, Listener.Type type) { switch (type) { case ON_TOUCH: setter = "setOnTouchListener"; classType = r.ref("android.view.View.OnTouchListener"); methodName = "onTouch"; methodParams = Arrays.asList( new Pair<JType, String>(r.viewClass, "view"), new Pair<JType, String>(r.ref("android.view.MotionEvent"), "event")); methodReturn = r.m.BOOLEAN; defaultReturn = FALSE; break; case ON_CLICK: setter = "setOnClickListener"; classType = r.ref("android.view.View.OnClickListener"); methodName = "onClick"; methodParams = Arrays.asList(new Pair<JType, String>(r.viewClass, "view")); methodReturn = r.m.VOID; defaultReturn = null; break; case ON_LONG_CLICK: setter = "setOnLongClickListener"; classType = r.ref("android.view.View.OnLongClickListener"); methodName = "onLongClick"; methodParams = Arrays.asList(new Pair<JType, String>(r.viewClass, "view")); methodReturn = r.m.BOOLEAN; defaultReturn = FALSE; break; case ON_FOCUS_CHANGE: setter = "setOnFocusChangeListener"; classType = r.ref("android.view.View.OnFocusChangeListener"); methodName = "onFocusChange"; methodParams = Arrays.asList( new Pair<JType, String>(r.viewClass, "view"), new Pair<JType, String>(r.m.BOOLEAN, "hasFocus")); methodReturn = r.m.VOID; defaultReturn = null; break; case ON_CHECKED_CHANGE: setter = "setOnCheckedChangeListener"; classType = r.ref("android.widget.CompoundButton.OnCheckedChangeListener"); methodName = "onCheckedChanged"; methodParams = Arrays.asList( new Pair<JType, String>(r.ref("android.widget.CompoundButton"), "view"), new Pair<JType, String>(r.m.BOOLEAN, "isChecked")); methodReturn = r.m.VOID; defaultReturn = null; break; case ON_EDITOR_ACTION: setter = "setOnEditorActionListener"; classType = r.ref("android.widget.TextView.OnEditorActionListener"); methodName = "onEditorAction"; methodParams = Arrays.asList( new Pair<JType, String>(r.ref("android.widget.TextView"), "view"), new Pair<JType, String>(r.m.INT, "actionId"), new Pair<JType, String>(r.ref("android.view.KeyEvent"), "event")); methodReturn = r.m.BOOLEAN; defaultReturn = FALSE; break; case ON_ITEM_CLICK: setter = "setOnItemClickListener"; classType = r.ref("android.widget.AdapterView.OnItemClickListener"); methodName = "onItemClick"; methodParams = Arrays.asList( new Pair<JType, String>(r.ref("android.widget.AdapterView<?>"), "view"), new Pair<JType, String>(r.viewClass, "item"), new Pair<JType, String>(r.m.INT, "position"), new Pair<JType, String>(r.m.LONG, "id")); methodReturn = r.m.VOID; defaultReturn = null; break; case ON_ITEM_LONG_CLICK: setter = "setOnItemLongClickListener"; classType = r.ref("android.widget.AdapterView.OnItemLongClickListener"); methodName = "onItemLongClick"; methodParams = Arrays.asList( new Pair<JType, String>(r.ref("android.widget.AdapterView<?>"), "view"), new Pair<JType, String>(r.viewClass, "item"), new Pair<JType, String>(r.m.INT, "position"), new Pair<JType, String>(r.m.LONG, "id")); methodReturn = r.m.BOOLEAN; defaultReturn = FALSE; break; default: throw new IllegalArgumentException("Unknown listener type: " + type.toString()); } }