Skip to content

Commit

Permalink
Faces 1713: Step Backwards
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jan 28, 2025
1 parent 7949f80 commit 618cd30
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ public int getIndex()

public Integer getStep()
{
if (step == -1)
{
return null;
}

return step;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ public void setSize(int size)
}

/**
* Iteration will only process every step items of the collection, starting with the first one.
* If the <code>step</code> attribute is less than <code>1</code>: a <code>FacesException</code> must be thrown.
*
* Iteration will process every <code>step</code>th item of the collection, starting with the first item.
* If the <code>step</code> attribute is equal to <code>0</code>, a <code>FacesException</code> must be thrown.
* If the <code>step</code> value is less than <code>0</code>, the iteration will traverse the collection
* in reverse order.
*
* @return the step
*/

@JSFProperty
public int getStep()
{
Expand Down Expand Up @@ -330,9 +333,9 @@ private DataModel createDataModel()
{
throw new LocationAwareFacesException("'offset' attribute may not be less than 0");
}
if (getStep() < 1)
if (getStep() == 0)
{
throw new LocationAwareFacesException("'step' attribute may not be less than 1");
throw new LocationAwareFacesException("'step' attribute may not be 0");
}
}

Expand Down Expand Up @@ -454,7 +457,7 @@ private RepeatStatus _getRepeatStatus()
begin = getOffset();
}

return new RepeatStatus(_count == 0, _index + getStep() >= getRowCount(),
return new RepeatStatus(_count == 0, _index + Math.abs(getStep()) >= getRowCount(),
_count, _index, begin, _end, getStep());

}
Expand Down Expand Up @@ -863,7 +866,8 @@ public int getRowCount()
{
if (_emptyModel) // empty model
{
return (getEnd() - getBegin())/(getStep() <= 0 ? 1 : getStep());
int step = Math.abs(getStep());
return (getEnd() - getBegin()) / (step == 0 ? 1 : step);
}
else
{
Expand Down Expand Up @@ -1352,7 +1356,7 @@ private Collection<Object[]> saveDescendantInitialComponentStates(FacesContext f
*/
private int _calculateCountForIndex(int index)
{
return (index - getOffset()) / getStep();
return (index - getOffset()) / Math.abs(getStep());
}

private void _validateAttributes() throws FacesException
Expand Down Expand Up @@ -1803,11 +1807,23 @@ public boolean visitTree(VisitContext context, VisitCallback callback)
int i = getOffset();
int end = getSize();
int step = getStep();
end = (end >= 0) ? i + end : Integer.MAX_VALUE - 1;
_count = 0;


// Determine if iterating forwards or backwards
if (step < 0)
{
// Adjust for backward iteration
end = (end >= 0) ? i - end : Integer.MIN_VALUE + 1; // backwards, so decrement end
step = Math.abs(step); // Use the absolute value of step for incrementing
}
else
{
// Forward iteration
end = (end >= 0) ? i + end : Integer.MAX_VALUE - 1;
}

setRowIndex(i);
while (i < end && _isIndexAvailable())
while (((step > 0 && i < end) || (step < 0 && i > end)) && _isIndexAvailable()) // Check direction
{
for (int j = 0, childCount = getChildCount(); j < childCount; j++)
{
Expand All @@ -1817,10 +1833,9 @@ public boolean visitTree(VisitContext context, VisitCallback callback)
return true;
}
}

_count++;
i += step;

i += (step > 0 ? step : -step); // Move in the appropriate direction
setRowIndex(i);
}
}
Expand Down Expand Up @@ -2251,4 +2266,4 @@ enum PropertyKeys
, end
, rowStatePreserved
}
}
}

0 comments on commit 618cd30

Please sign in to comment.