Class FlowManager<T,C extends Cell<T>>
- Type Parameters:
T
- the type of itemsC
- the type of cell used
FlowManager
is responsible for managing the flow's viewport, the cells.
It stores the current state of the viewport at any time with two properties:
- The state property which holds an object of type FlowState
- The last range property which holds the last range of displayed items, as a IntegerRange
-
Property Summary
PropertiesTypePropertyDescriptionReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>>
Specifies the current range of displayed items as aIntegerRange
object.Specifies the current state of the viewport as aFlowState
object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the viewport.io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>
Gets the value of the property lastRange.getState()
Gets the value of the property state.protected VirtualFlow<T,
C> protected T
indexToItem
(int index) Converts the given index to an item from the list.void
init()
This method is responsible for filling the viewport with the necessary number of cells.final boolean
final int
itemsNum()
protected C
itemToCell
(T item) Converts the given item to a cell usingVirtualFlow.cellFactoryProperty()
.ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>>
Specifies the current range of displayed items as aIntegerRange
object.void
onListChange
(ListChangeListener.Change<? extends T> c) This is responsible for updating the viewport state whenever a change occurs in the items list.void
onScroll()
This is responsible for updating the viewport state on scroll.void
reset()
protected void
setLastRange
(io.github.palexdev.mfxcore.base.beans.range.IntegerRange lastRange) Sets the value of the property lastRange.protected void
Sets the value of the property state.Specifies the current state of the viewport as aFlowState
object.
-
Property Details
-
state
Specifies the current state of the viewport as aFlowState
object.- See Also:
-
lastRange
public ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>> lastRangePropertySpecifies the current range of displayed items as aIntegerRange
object.- See Also:
-
-
Method Details
-
init
public void init()This method is responsible for filling the viewport with the necessary number of cells. So, it is a bit more than just an initializing method, since this is also called for example when the viewport size changes and cells need to be added or removed. For this reason this method works in a general way:First we compute the range of items we want to display by using the
OrientationHelper
. Specifically we get:the number of needed cells with
OrientationHelper.maxCells()
the end index with
Math.max(start + num - 1, itemsNum - 1)
the start index with
The next step is to instantiate a newOrientationHelper.firstVisible()
. It may happen that the difference between the end and start indexes doesn't equal to the number of needed cells, for this reason we prevent any error from occurring by "correcting" the start index as follows:Math.max(end - num + 1, 0)
FlowState
which will be the new state of the viewport, and the current "old" state.If the old state is
FlowState.EMPTY
then from start to end index new cells are created and added to the new state. Then both the state property and last range property are updated and finallyVirtualFlow.requestViewportLayout()
is called.In any other case it means we are in a situation where cells need to either be supplied or removed.
From start to end index we check if the old state contains the i-th cell, if that's the case the cell is removed from the old state and added to the new one. Otherwise, a new cell is created and added to the new state.
So, how the supply/remove works:
The new state carries all the needed cells, a listener in the
VirtualFlowSkin
will then update the viewport children if needed. All the cells which remain in the old state are the one to be removed, but since only the new state determines the viewport's content, the only operation left is to dispose these unneeded ones withCell.dispose()
.Finally, both the state property and last range property are updated and
VirtualFlow.requestViewportLayout()
is called. -
onScroll
public void onScroll()This is responsible for updating the viewport state on scroll. If the current state isFlowState.EMPTY
or the items list is empty, exits immediately.The first step is to compute the new range of items to display. Then the new state is updated only if the current range and the computed one are different, the new state is built with
FlowState.transition(IntegerRange)
.The second step is to call
VirtualFlow.requestViewportLayout()
, but this happens only if the last range and the new range are not equal. -
onListChange
This is responsible for updating the viewport state whenever a change occurs in the items list. There are three separate situations:1) The items list is empty, calls
clear()
and exits2) The current state is
FlowState.EMPTY
, callsinit()
and exits3) The given change is processed by using the
ListChangeHelper
utility class, then the new state is computed by usingFlowState.transition(List)
, finallyVirtualFlow.requestViewportLayout()
is called and the last range property is updated. -
clear
public void clear()Clears the viewport. Sets the state toFlowState.EMPTY
. -
reset
public void reset() -
indexToItem
Converts the given index to an item from the list. -
itemToCell
Converts the given item to a cell usingVirtualFlow.cellFactoryProperty()
. -
itemsNum
public final int itemsNum()- Returns:
- the number of items in the list
-
itemsEmpty
public final boolean itemsEmpty()- Returns:
- whether the list is empty
-
getVirtualFlow
- Returns:
- the virtual flow instance associated to this manager
-
getState
Gets the value of the property state.- Property description:
- Specifies the current state of the viewport as a
FlowState
object.
-
stateProperty
Specifies the current state of the viewport as aFlowState
object.- See Also:
-
setState
Sets the value of the property state.- Property description:
- Specifies the current state of the viewport as a
FlowState
object.
-
getLastRange
Gets the value of the property lastRange.- Property description:
- Specifies the current range of displayed items as a
IntegerRange
object.
-
lastRangeProperty
public ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>> lastRangeProperty()Specifies the current range of displayed items as aIntegerRange
object.- See Also:
-
setLastRange
protected void setLastRange(io.github.palexdev.mfxcore.base.beans.range.IntegerRange lastRange) Sets the value of the property lastRange.- Property description:
- Specifies the current range of displayed items as a
IntegerRange
object.
-