예제 #1
0
  public void executeActionsOnView(View view, Object mappedObject) {
    if (view.getTag() != null) {

      List<CarpaccioAction> actions;

      if (view.getTag() instanceof List) { // if already splitted
        actions = (List<CarpaccioAction>) view.getTag();
      } else { // only do this work the first time
        actions = new ArrayList<>();

        String tag = view.getTag().toString().trim();
        String[] tagCalls = CarpaccioHelper.trim(tag.split(";"));
        for (String call : tagCalls) {
          if (!call.startsWith("//")) { // skip the "//function(args)"
            CarpaccioAction carpaccioAction = new CarpaccioAction(call);

            actions.add(carpaccioAction);
          }
        }

        view.setTag(actions); // save into view tags, replace the string with the list
      }

      CarpaccioLogger.d(TAG, view.getClass().getName() + " has actions " + actions);

      if (actions != null)
        for (CarpaccioAction action : actions) {

          // if it's a mapped function ex: setText($user)
          if (mappingManager != null && action.isCallMapping()) {
            mappingManager.callMappingOnView(action, view, mappedObject);

            if (Carpaccio.IN_EDIT_MODE) {
              try {
                callActionOnView(action, view);
              } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
              }
            }
          } else // an usual function setText(florent)
          callActionOnView(action, view);
        }
    }

    if (view instanceof TextView && ((TextView) view).getText() != null) {
      String text = ((TextView) view).getText().toString().trim();

      if (text.startsWith("$")) {
        if (mappingManager != null) {
          String textAction = "setText(" + text + ")";
          CarpaccioAction carpaccioAction = new CarpaccioAction(textAction);
          if (view.getTag() != null) {
            if (view.getTag() instanceof String) view.setTag(view.getTag() + ";" + textAction);
            else if (view.getTag() instanceof List) {
              ((List) view.getTag()).add(carpaccioAction);
            }
          } else {
            List<CarpaccioAction> actions = new ArrayList<>();
            actions.add(carpaccioAction);
            view.setTag(actions);
          }
          mappingManager.callMappingOnView(carpaccioAction, view, null);
        }
      }
    }
  }
예제 #2
0
  public void callActionOnView(CarpaccioAction action, View view) {
    // find the controller for this call
    ObjectAndMethod objectAndMethod = action.getObjectAndMethod();
    if (objectAndMethod == null) {
      // check if cached it
      String key = action.getFunction() + (action.getArgs().length + 1);

      objectAndMethod = savedControllers.get(key);

      if (objectAndMethod == null) { // if not cached,
        objectAndMethod =
            CarpaccioHelper.findObjectWithThisMethod(
                this.registerControllers,
                action.getFunction(),
                action.getArgs().length + 1); // +1 for the view

        if (objectAndMethod != null) {
          CarpaccioLogger.d(
              TAG, objectAndMethod.getObject() + " used for " + action.getCompleteCall());
        } else {
          CarpaccioLogger.e(TAG, "cannot find any controller for " + action.getCompleteCall());
          throw new CarpaccioException(
              "cannot find any controller for " + action.getCompleteCall());
        }

        savedControllers.put(key, objectAndMethod);
      }
      action.setObjectAndMethod(objectAndMethod);
    }

    if (objectAndMethod != null) {
      // call !
      CarpaccioHelper.callMethod(
          action.getObjectAndMethod().getObject(),
          action.getObjectAndMethod().getMethod(),
          action.getFunction(),
          view,
          action.getValues());
    }
  }