@Override
  public void decode(FacesContext context, UIComponent component, ClientBehavior behavior) {
    if (null == context || null == component || null == behavior) {
      throw new NullPointerException();
    }

    if (!(behavior instanceof AjaxBehavior)) {
      // TODO: use MessageUtils for this error message?
      throw new IllegalArgumentException(
          "Instance of javax.faces.component.behavior.AjaxBehavior required: " + behavior);
    }

    AjaxBehavior ajaxBehavior = (AjaxBehavior) behavior;

    // First things first - if AjaxBehavior is disabled, we are done.
    if (ajaxBehavior.isDisabled()) {
      return;
    }

    component.queueEvent(createEvent(context, component, ajaxBehavior));

    if (logger.isLoggable(Level.FINE)) {
      logger.fine("This command resulted in form submission " + " AjaxBehaviorEvent queued.");
      logger.log(Level.FINE, "End decoding component {0}", component.getId());
    }
  }
  // Tests whether we should perform immediate processing.  Note
  // that we "inherit" immediate from the parent if not specified
  // on the behavior.
  private static boolean isImmediate(UIComponent component, AjaxBehavior ajaxBehavior) {

    boolean immediate = false;

    if (ajaxBehavior.isImmediateSet()) {
      immediate = ajaxBehavior.isImmediate();
    } else if (component instanceof EditableValueHolder) {
      immediate = ((EditableValueHolder) component).isImmediate();
    } else if (component instanceof ActionSource) {
      immediate = ((ActionSource) component).isImmediate();
    }

    return immediate;
  }
Beispiel #3
0
  @Override
  public void addClientBehavior(String eventName, ClientBehavior clientBehavior) {

    // If the specified client behavior is an Ajax behavior, then the alloy:accordion component tag
    // has an f:ajax
    // child tag. Register a listener that can respond to the Ajax behavior by invoking the
    // tabCollapseListener or
    // tabExpandListener that may have been specified.
    if (clientBehavior instanceof AjaxBehavior) {
      AjaxBehavior ajaxBehavior = (AjaxBehavior) clientBehavior;
      ajaxBehavior.addAjaxBehaviorListener(new AccordionBehaviorListener());
    }

    super.addClientBehavior(eventName, clientBehavior);
  }
  public AjaxParameters(InputFile inputFile, String clientId, String formClientId) {

    // Default value of execute is "@this" which maps to name and id of the rendered input element.
    this.execute = clientId.concat(StringPool.SPACE).concat(clientId);

    // Default value of render is "@none" which maps to an empty string.
    this.render = StringPool.BLANK;

    // For each Ajax behavior associated with the specified component:
    Map<String, List<ClientBehavior>> clientBehaviorMap = inputFile.getClientBehaviors();
    List<ClientBehavior> clientBehaviors = clientBehaviorMap.get(inputFile.getDefaultEventName());

    for (ClientBehavior clientBehavior : clientBehaviors) {

      if (clientBehavior instanceof AjaxBehavior) {

        // Interpret the value of the f:ajax "execute" attribute.
        AjaxBehavior ajaxBehavior = (AjaxBehavior) clientBehavior;
        Collection<String> executeIds = ajaxBehavior.getExecute();

        if ((executeIds != null) && (executeIds.size() > 0)) {

          boolean foundAllKeyword = false;
          boolean foundNoneKeyword = false;
          StringBuilder buf = new StringBuilder(clientId);

          for (String executeId : executeIds) {

            if ("@all".equals(executeId)) {
              foundAllKeyword = true;

              break;
            } else if ("@none".equals(executeId)) {
              foundNoneKeyword = true;
              this.execute = StringPool.BLANK;

              break;
            } else if (executeId.length() > 0) {

              buf.append(StringPool.SPACE);
              buf.append(executeId);
            }
          }

          if (!foundNoneKeyword) {

            if (foundAllKeyword) {
              this.execute = "@all";
            } else {
              this.execute = buf.toString();
              this.execute = this.execute.replace("@form", formClientId);
              this.execute = this.execute.replace("@this", clientId);

              boolean foundClientId = false;
              String[] executeIdArray = this.execute.split(StringPool.SPACE);

              for (String executeId : executeIdArray) {

                if (clientId.equals(executeId)) {
                  foundClientId = true;

                  break;
                }
              }

              if (!foundClientId) {
                this.execute = clientId.concat(StringPool.SPACE).concat(this.execute);
              }
            }
          }
        }

        // Interpret the value of the f:ajax "render" attribute.
        Collection<String> renderIds = ajaxBehavior.getRender();

        if ((renderIds != null) && (renderIds.size() > 0)) {

          boolean first = true;
          boolean foundAllKeyword = false;
          boolean foundNoneKeyword = false;
          StringBuilder buf = new StringBuilder();

          for (String renderId : renderIds) {

            if ("@all".equals(renderId)) {
              foundAllKeyword = true;

              break;
            } else if ("@none".equals(renderId)) {
              foundNoneKeyword = true;
              this.render = StringPool.BLANK;

              break;
            } else {

              if (renderId.length() > 0) {

                if (first) {
                  first = false;
                } else {
                  buf.append(StringPool.SPACE);
                }

                buf.append(renderId);
              }
            }
          }

          if (!foundNoneKeyword) {

            if (foundAllKeyword) {
              this.render = "@all";
            } else {
              this.render = buf.toString();
              this.render = this.render.replace("@form", formClientId);
              this.render = this.render.replace("@this", clientId);
            }
          }
        }
      }
    }
  }
  private static String buildAjaxCommand(
      ClientBehaviorContext behaviorContext,
      AjaxBehavior ajaxBehavior,
      boolean namespaceParameters) {

    // First things first - if AjaxBehavior is disabled, we are done.
    if (ajaxBehavior.isDisabled()) {
      return null;
    }

    UIComponent component = behaviorContext.getComponent();
    String eventName = behaviorContext.getEventName();

    StringBuilder ajaxCommand = new StringBuilder(256);
    Collection<String> execute = ajaxBehavior.getExecute();
    Collection<String> render = ajaxBehavior.getRender();
    String onevent = ajaxBehavior.getOnevent();
    String onerror = ajaxBehavior.getOnerror();
    String sourceId = behaviorContext.getSourceId();
    String delay = ajaxBehavior.getDelay();
    Boolean resetValues = null;
    if (ajaxBehavior.isResetValuesSet()) {
      resetValues = ajaxBehavior.isResetValues();
    }
    Collection<ClientBehaviorContext.Parameter> params = behaviorContext.getParameters();

    // Needed workaround for SelectManyCheckbox - if execute doesn't have sourceId,
    // we need to add it - otherwise, we use the default, which is sourceId:child, which
    // won't work.
    ClientBehaviorContext.Parameter foundparam = null;
    for (ClientBehaviorContext.Parameter param : params) {
      if (param.getName().equals("incExec") && (Boolean) param.getValue()) {
        foundparam = param;
      }
    }
    if (foundparam != null && !execute.contains(sourceId)) {
      execute = new LinkedList<>(execute);
      execute.add(component.getClientId());
    }
    if (foundparam != null) {
      try {
        // And since this is a hack, we now try to remove the param
        params.remove(foundparam);
      } catch (UnsupportedOperationException uoe) {
        if (logger.isLoggable(Level.FINEST)) {
          logger.log(Level.FINEST, "Unsupported operation", uoe);
        }
      }
    }

    ajaxCommand.append("mojarra.ab(");

    if (sourceId == null) {
      ajaxCommand.append("this");
    } else {
      ajaxCommand.append("'");
      ajaxCommand.append(sourceId);
      ajaxCommand.append("'");
    }

    ajaxCommand.append(",event,'");
    ajaxCommand.append(eventName);
    ajaxCommand.append("',");

    appendIds(component, ajaxCommand, execute);
    ajaxCommand.append(",");
    appendIds(component, ajaxCommand, render);

    String namingContainerId = null;
    if (namespaceParameters) {
      FacesContext context = behaviorContext.getFacesContext();
      UIViewRoot viewRoot = context.getViewRoot();
      if (viewRoot instanceof NamingContainer) {
        namingContainerId = viewRoot.getContainerClientId(context);
      }
    }

    if ((namingContainerId != null)
        || (onevent != null)
        || (onerror != null)
        || (delay != null)
        || (resetValues != null)
        || !params.isEmpty()) {

      ajaxCommand.append(",{");

      if (namingContainerId != null) {
        // the literal string must exactly match the corresponding value
        // in jsf.js.
        RenderKitUtils.appendProperty(
            ajaxCommand, "com.sun.faces.namingContainerId", namingContainerId, true);
      }

      if (onevent != null) {
        RenderKitUtils.appendProperty(ajaxCommand, "onevent", onevent, false);
      }

      if (onerror != null) {
        RenderKitUtils.appendProperty(ajaxCommand, "onerror", onerror, false);
      }

      if (delay != null) {
        RenderKitUtils.appendProperty(ajaxCommand, "delay", delay, true);
      }

      if (resetValues != null) {
        RenderKitUtils.appendProperty(ajaxCommand, "resetValues", resetValues, false);
      }

      if (!params.isEmpty()) {
        for (ClientBehaviorContext.Parameter param : params) {
          RenderKitUtils.appendProperty(ajaxCommand, param.getName(), param.getValue());
        }
      }

      ajaxCommand.append("}");
    }

    ajaxCommand.append(")");

    return ajaxCommand.toString();
  }