private int _addNavigationOptions()

in trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/ui/laf/base/xhtml/NavigationBarRenderer.java [1448:1590]


  private int _addNavigationOptions(UIXRenderingContext context,
                                    UINode navBar,
                                    MarlinBean choice,
                                    boolean isForm,
                                    long minValue, long maxValue, long value,
                                    int blockSize,
                                    String sizeKey)
  {
    int selectedIndex = -1;

    boolean maxUnknown = (maxValue == MAX_VALUE_UNKNOWN);

    // Zero-indexed block index.
    long blockIndex = (value - minValue + blockSize - 1L) /
                         blockSize;

    // sometimes a record set won't start on a multiple of blockSize. So
    // remember to add any offset:
    // this can safely be an int because it is an index into the blockSize,
    // which is itself an int:
    int offset = (int) (value - (minValue + (blockIndex * blockSize)));
    if (offset < 0)
      offset = offset + blockSize;

    // Total number of blocks (again, zero-indexed)
    long maxBlockIndex;
    if (maxUnknown)
      maxBlockIndex = blockIndex + 1;
    else
    {
      maxBlockIndex = (maxValue - minValue - offset) / blockSize;
      if (offset > 0)
        maxBlockIndex++;
    }

    // Calculate the first block that should be shown.  The order goes:
    // Group 0:            0-28 + More
    // Group 1:Previous + 29-56 + More
    // Group 2:Previous + 57-84 + More
    // etc..
    long firstBlockIndex;

    // If everything is visible, or we're in the first group, start at zero.
    if ((maxBlockIndex <= (_MAX_VISIBLE_OPTIONS - 1L)) ||
        (blockIndex <= (_MAX_VISIBLE_OPTIONS - 2L)))
      firstBlockIndex = 0;
    else
      firstBlockIndex = ((blockIndex - 1L) / (_MAX_VISIBLE_OPTIONS - 2L)) *
                        (_MAX_VISIBLE_OPTIONS - 2L);

    // And we always show a total of 30 groups (or straight to the end)
    long lastBlockIndex = firstBlockIndex + (_MAX_VISIBLE_OPTIONS - 1L);
    if (lastBlockIndex > maxBlockIndex)
      lastBlockIndex = maxBlockIndex;

    Object showAll = navBar.getAttributeValue(context, SHOW_ALL_ATTR);
    boolean atShowAll = SHOW_ALL_ACTIVE.equals(showAll);

    // If they want "Show All" add an option for it - but ONLY allow
    // this option when there are less than 30 groups (maxBlockIndex
    // start as zero, hence "29"), and only allow it when there's
    // more than 1 visible item!
    if (!maxUnknown &&
        (lastBlockIndex > firstBlockIndex) &&
        (maxBlockIndex <= (_MAX_VISIBLE_OPTIONS - 1L)) &&
        (atShowAll || SHOW_ALL_YES.equals(showAll)))
    {
      choice.addIndexedChild(0,
                             _createShowAllOption(context,
                                                  maxValue,
                                                  atShowAll));
    }
    else
    {
      atShowAll = false;
    }

    DataObject indexNames = (DataObject) navBar.getAttributeValue(context,
                                                                  _INDEX_NAMES_ATTR);
    for (blockIndex = firstBlockIndex;
         blockIndex <= lastBlockIndex;
         blockIndex++)
    {
      long blockStart = minValue + (blockIndex * blockSize);

      // if there is an offset, then adjust accordingly. for example, if the
      // offset is 7 (and the blockSize is 10), then the new blockStarts are:
      // 1-7, 8-17, 18-27, etc ...
      if (offset > 0)
        blockStart += (offset - blockSize);

      final int currentRecordSize;
      // check to see if this is the very first record set in a table using an
      // offset:
      if (blockStart < minValue)
      {
        // treat this specially. this is the 1-7 case from the example above:
        blockStart = minValue;
        currentRecordSize = offset;
      }
      else
      {
        currentRecordSize = blockSize;
      }

      Object text;
      // Need "Previous..."
      if ((blockIndex == firstBlockIndex) &&
          (blockIndex != 0))
      {
        text = getTranslatedValue(context, _PREVIOUS_TEXT_KEY);
      }
      // Need "More..." (on the last block, either 'cause
      // the total number of blocks is unknown or we've shown enough blocks
      else if ((blockIndex == lastBlockIndex) &&
               (maxUnknown || (lastBlockIndex < maxBlockIndex)))
      {
        text = getTranslatedValue(context, _MORE_TEXT_KEY);
      }
      else
      {
        text = null;
      }

      MarlinBean currOption = _createNavigationOption(context,
                                                  navBar,
                                                  isForm,
                                                  blockStart,
                                                  currentRecordSize,
                                                  maxValue,
                                                  // Don't select
                                                  atShowAll ?
                                                    minValue - 1 : value,
                                                  sizeKey,
                                                  text,
                                                  indexNames);
      choice.addIndexedChild(currOption);
      if (Boolean.TRUE.equals(currOption.getAttributeValue(SELECTED_ATTR)))
        selectedIndex = choice.getIndexedChildCount(context) - 1;
    }

    return selectedIndex;
  }