Exemple #1
0
  /**
   * @param context
   * @param view
   * @param request
   * @return null if it fails validating provided by request
   */
  private HashMap<String, String> getParams(Context context, View view, Object request) {
    HashMap<String, String> params = new HashMap<String, String>();
    for (Field field : request.getClass().getDeclaredFields()) {
      Annotation annotation = field.getAnnotation(ViewInj.class);
      // not from view
      if (annotation == null) {
        /*
         * maybe it's an mutable, which wraps entities
         */
        try {
          Object value = field.get(request);
          if (value instanceof MutableEntity) {
            Object entity = ((MutableEntity) value).getEntity();
            //						if(entity instanceof Getable){
            HashMap<String, String> map = getParams(context, view, entity);
            params.putAll(map);
            //						}
          } else {
            // not from view
            params.put(field.getName(), field.get(request).toString());
          }
        } catch (Exception e1) {
          e1.printStackTrace();
        }

        continue;
      }
      ViewInj inj = (ViewInj) annotation;
      int resId = inj.value();
      View childView = findViewById(view, resId);
      if (childView == null) {
        continue;
      }
      try {
        for (ViewInjector injector : injectors) {
          String value = injector.addParams(childView, params, request, field);
          if (value != null) {
            if (request instanceof Validatable) {
              String validate = ((Validatable) request).validate(field.getName(), value);
              if (validate != null) {
                // error!
                if (request instanceof Getable) {
                  ((Getable) request).onError(context, validate);
                }
                return null;
              }
            }
            break;
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return params;
  }
Exemple #2
0
 /**
  * @param context
  * @param view
  * @param bean
  * @return true if a ViewFoundException is supposed to throw
  */
 public void setContent(Context context, View view, Object bean) {
   for (Field field : bean.getClass().getDeclaredFields()) {
     Annotation annotation = field.getAnnotation(ViewInj.class);
     if (annotation == null) {
       continue;
     }
     ViewInj inj = (ViewInj) annotation;
     int resId = inj.value();
     View v = findViewById(view, resId);
     if (v == null) {
       // TODO
       Log.w(
           TAG, "View not find, in class " + bean.getClass().getName() + ", resource id " + resId);
       continue;
     }
     for (ViewInjector injector : injectors) {
       if (injector.setContent(context, v, bean, field)) {
         break;
       }
     }
   }
 }