protected void encodeAll()

in trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SelectRangeChoiceBarRenderer.java [303:655]


  protected void encodeAll(
    FacesContext     context,
    RenderingContext rc,
    UIComponent      component,
    FacesBean        bean
    ) throws IOException
  {
    int rowIndex = getRowIndex(component);
    try
    {
      int blockSize = getRows(component, bean);
      if (blockSize < 0)
       blockSize = toInt(_rowsKey.getDefault());

      // =-=AEW The old rendering code was written with "value" as one-indexed;
      // "first" is zero-indexed.  Rewrite the rendering code to deal.
      long currentValue = getFirst(component, bean) + 1;
      if (currentValue < 1)
        currentValue = 1;

      long minValue = 1;

      // @todo: =-=jmw ... get maxValue from the model. If no model, then use the
      // maximum attribute. Not sure we want to implement this feature.
      long maxValue = getRowCount(component);
      if (maxValue <= 0)
        maxValue = XhtmlConstants.MAX_VALUE_UNKNOWN;

      // get name
      String id = getClientId(context, component);
      // For the source, just pass the ID as long as this is being
      // used on its own - but give a hook for subclasses to their thing.
      String source = getSource();
      if (source == null)
        source = id;

      if (rc.getFormData() == null)
        return;

      String formName = rc.getFormData().getName();
      if (formName == null)
        return;

      int  nextRecords = 0;
      int  prevRecords = 0;
      long backValue = 0;
      long nextValue = 0;

      if (blockSize > 0)
      {
        // determine how many records user can go forward
        long lNextRecords = blockSize;

        if (maxValue != XhtmlConstants.MAX_VALUE_UNKNOWN)
        {
          // if we know the total records, align the current value to the
          // start of its block. This makes the choice-rendering style
          // not show extra back navigation records on the min block,
          // which it would if not aligned.

          // =-=AEW Revisit bug 3052637 for JSF
          /** no don't do this. see bug: 3052637
              currentValue -= minValue;
              currentValue /= blockSize;
              currentValue *= blockSize;
              currentValue += minValue;
          */

          lNextRecords = maxValue - (currentValue + blockSize - 1);
        }

        // determine how many records user can go back
        long lPrevRecords = currentValue - minValue;

        // trim
        nextRecords = (lNextRecords > blockSize)
          ? blockSize
          : (int) lNextRecords;

        prevRecords = (lPrevRecords > blockSize)
          ? blockSize
          : (int) lPrevRecords;

        backValue = currentValue - prevRecords;
        nextValue = currentValue + blockSize;
      }


      boolean validate = !getImmediate(component, bean);

      boolean showDisabledNavigation = disabledNavigationShown();
      boolean hasBackRecords = (prevRecords > 0);
      boolean hasNextRecords = (nextRecords > 0);

      if (hasNextRecords && (maxValue == XhtmlConstants.MAX_VALUE_UNKNOWN))
      {
        // make sure the next range exists in the data model.
        hasNextRecords = isRowAvailable(component, (int)nextValue-1);
      }
      
      if (!hasNextRecords)
      {
        nextRecords = 0;
      }

      boolean showBackButton = hasBackRecords || showDisabledNavigation;
      boolean showNextButton = hasNextRecords || showDisabledNavigation;
      if (!supportsNavigation(rc))
      {
        showBackButton = false;
        showNextButton = false;
      }

      boolean showAllActive = getShowAll(component, bean);

      if (showAllActive)
      {
        prevRecords = 0;
        nextRecords = 0;
      }

      String prevOnClick = null;
      String nextOnClick = null;


      if (hasBackRecords || hasNextRecords)
      {
        addHiddenFields(rc);
        // Render script submission code.
        ProcessUtils.renderNavSubmitScript(context, rc);
        ProcessUtils.renderNavChoiceSubmitScript(context, rc);
      }

      // use form submit
      if (supportsScripting(rc))
      {
        if (hasBackRecords && !showAllActive)
        {
          prevOnClick = ProcessUtils.getSubmitScriptCall(formName,
                                                         source,
                                                         backValue,
                                                         validate);
        }

        if (hasNextRecords && !showAllActive)
        {
          nextOnClick =  ProcessUtils.getSubmitScriptCall(formName,
                                                          source,
                                                          nextValue,
                                                          validate);
        }
      }

      // ready to render
      ResponseWriter writer = context.getResponseWriter();
      boolean renderAsTable = __renderAsTable(component);


      // The following strange code is part of the work around for
      // bug 2275703.  IE has problems re-laying out a TableBean
      // after a partial page replacement.  In particular, pieces
      // of the table's top navigation bar, such as the previous link
      // or icon, or sometimes the entire navigation bar,  may shift to
      // the left.  In some cases, pieces of the navigation bar (the previous
      // icon) may disappear during re-layout!  There doesn't seem to be
      // any clean way to avoid this apparent IE bug.  However, we explicitly
      // perform a partial replacement of the navigation bar's previous icon
      // *after* the entire table has been replaced, everything seems to lay
      // out just fine.
      //
      // So, if we are rendering a TableBean's navigation bar with
      // PPR enabled on IE, then we generate an ID for the nav bar's
      // previous icon, and we add this to the list of rendered partial
      // targets during the partial page render.  This forces the icon
      // to be replaced as part of the partial page update, and fixes
      // our layout problems.
      String iconID = null;
      if (PartialPageUtils.isPPRActive(context) &&
          isIE(rc))
      {
        iconID = id + "-i";
      }

      // we only want to render the baseID, if needed, once. Then we
      // render the subIDs. So we need to keep track of this.
      boolean renderedId = false;

      // If the request is from a desktop browser we don't need to wrap up
      // with a div tag
      boolean isDesktop = false;

      // if we need to render standalone, create a table and table row...
      if (renderAsTable)
      {
        isDesktop = (rc.getAgent().getType().equals(Agent.TYPE_DESKTOP));
        // Few mobile browsers doesn't support PPR for Table element
        // so lets wrap it up with a div tag
        if(!isDesktop )
        {
          writer.startElement("div", component);
          writer.writeAttribute("id", id, "id");
        }
        writer.startElement("table", component);
        OutputUtils.renderLayoutTableAttributes(context, rc, "0", null);

        // =-=AEW Where do these attrs get written out when
        // we're inside a PanelPageButton?
        renderAllAttributes(context, rc, component, bean);

        // We should always render the ID, but we particularly need
        // to make sure that the ID is rendered if the NavBar is being
        // used to navigate a TableBean, since we explicitly target
        // TableBean NavBars when using PPR to re-render TableBeans...
        if(isDesktop )
        {
          writer.writeAttribute("id", id, "id");
        }
        renderedId = true;

        writer.startElement("tr", null);
      }

      boolean narrowScreen = supportsNarrowScreen(rc);

      // skip rendering back button for narrow-screen PDAs to reduce the
      // overall width of selectRangeChoiceBar.
      if (showBackButton && !narrowScreen)
      {
        Icon prevIcon = getIcon(rc, false, (prevOnClick != null));

        if (!prevIcon.isNull())
        {
          // We assign an id to the left arrow so that we can target it as
          // a partial target to work around bug 2275703 - see note above.
          if (iconID != null)
          {
            writer.startElement("td", component);
            writer.writeAttribute("id", iconID, null);

            // If the navigation bar that we are currently rendering
            // is included in a partial page response, add the icon
            // id to the list of partial targets.
            // =-=AEW Not sure this is still necessary
            PartialPageContext pprContext = rc.getPartialPageContext();
            if ((pprContext != null) &&
                pprContext.isInsidePartialTarget())
            {
              pprContext.addRenderedPartialTarget(iconID);
            }
          }
          else
          {
            // not in PPR mode, so just render the td (and id if not in a table)
            _renderStartTableCell(writer, id, renderedId);
            renderedId = true;
          }


          writer.writeAttribute("valign", "middle", null);
          _renderArrow(context, rc, prevIcon, false, prevOnClick);
          writer.endElement("td");

          _renderSpacerCell(context, rc);
        }

        _renderStartTableCell(writer, id, renderedId);
        renderedId = true;
        writer.writeAttribute("valign", "middle", null);
        writer.writeAttribute("nowrap", Boolean.TRUE, null);

        _renderLink(context,
                    rc,
                    false,
                    prevOnClick,
                    prevRecords,
                    id,
                    source,
                    backValue);

        writer.endElement("td");
        _renderSpacerCell(context, rc);
      }

      _renderStartTableCell(writer, id, renderedId);
      renderedId = true;
      writer.writeAttribute("valign", "middle", null);
      writer.writeAttribute("nowrap", Boolean.TRUE, null);

      _renderChoice(context,
                    rc,
                    component,
                    id,
                    source,
                    formName,
                    minValue,
                    currentValue,
                    blockSize,
                    maxValue,
                    validate);

      writer.endElement("td");

      // skip rendering back button for narrow-screen PDAs to reduce the
      // overall width of selectRangeChoiceBar.
      if (showNextButton && !narrowScreen)
      {
        _renderSpacerCell(context, rc);

        _renderStartTableCell(writer, id, true);
        writer.writeAttribute("valign", "middle", null);
        writer.writeAttribute("nowrap", Boolean.TRUE, null);

        _renderLink(context,
                    rc,
                    true,
                    nextOnClick,
                    nextRecords,
                    id,
                    source,
                    nextValue);

        writer.endElement("td");

        Icon nextIcon = getIcon(rc, true, (nextOnClick != null));
        if (!nextIcon.isNull())
        {
          _renderSpacerCell(context, rc);

          _renderStartTableCell(writer, id, true);
          writer.writeAttribute("valign", "middle", null);
          _renderArrow(context, rc, nextIcon, true, nextOnClick);
          writer.endElement("td");
        }
      }

      if (renderAsTable)
      {
        writer.endElement("tr");
        writer.endElement("table");
      }

      if (renderAsTable && !isDesktop )
      {
        writer.endElement("div");
      }

    }
    // Make sure we always restore the row index correctly
    finally
    {
      setRowIndex(component, rowIndex);
    }
  }