public void encodeClientScript(
      ResponseWriter writer, FacesContext facesContext, UIDataTableBase component)
      throws IOException {
    AbstractCollapsibleSubTable subTable = (AbstractCollapsibleSubTable) component;

    String id = subTable.getClientId(facesContext);

    UIComponent nestingForm = getUtils().getNestingForm(subTable);
    String formId = nestingForm != null ? nestingForm.getClientId(facesContext) : "";

    Map<String, Object> options = new HashMap<String, Object>();
    options.put("stateInput", subTable.getClientId(facesContext) + STATE);
    options.put("optionsInput", subTable.getClientId(facesContext) + OPTIONS);
    options.put("expandMode", subTable.getExpandMode());
    options.put("eventOptions", AjaxRendererUtils.buildEventOptions(facesContext, subTable));

    JSFunction jsFunction = new JSFunction("new RichFaces.ui.CollapsibleSubTable");
    jsFunction.addParameter(id);
    jsFunction.addParameter(formId);
    jsFunction.addParameter(options);

    writer.startElement(HtmlConstants.SCRIPT_ELEM, subTable);
    writer.writeAttribute(HtmlConstants.TYPE_ATTR, HtmlConstants.JAVASCRIPT_TYPE, null);
    writer.writeText(jsFunction.toScript(), null);
    writer.endElement(HtmlConstants.SCRIPT_ELEM);
  }
  public String toScript() {
    String result = null;

    if (!handlers.isEmpty()) {
      if (handlers.size() == 1) {
        result = handlers.get(0);
      } else {
        JSFunction jsFunction =
            new JSFunction("jsf.util.chain", JSReference.THIS, JSReference.EVENT);

        for (String handler : handlers) {
          jsFunction.addParameter(handler);
        }

        result = jsFunction.toScript();
      }
    }

    return result;
  }