Class FlowManager<T,C extends Cell<T>>

java.lang.Object
io.github.palexdev.virtualizedfx.flow.FlowManager<T,C>
Type Parameters:
T - the type of items
C - the type of cell used

public class FlowManager<T,C extends Cell<T>> extends Object
The 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

This is responsible for initializing, update on scroll, update on any change on the items list, clearing or resetting the viewport when needed.
  • Property Details

  • 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 OrientationHelper.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)

      The next step is to instantiate a new 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 finally VirtualFlow.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 with Cell.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 is FlowState.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

      public void onListChange(ListChangeListener.Change<? extends T> c)
      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 exits

      2) The current state is FlowState.EMPTY, calls init() and exits

      3) The given change is processed by using the ListChangeHelper utility class, then the new state is computed by using FlowState.transition(List), finally VirtualFlow.requestViewportLayout() is called and the last range property is updated.

    • clear

      public void clear()
      Clears the viewport. Sets the state to FlowState.EMPTY.
    • reset

      public void reset()
      Resets the viewport by first calling clear(), then init().
    • indexToItem

      protected T indexToItem(int index)
      Converts the given index to an item from the list.
    • itemToCell

      protected C itemToCell(T item)
      Converts the given item to a cell using VirtualFlow.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

      protected VirtualFlow<T,C> getVirtualFlow()
      Returns:
      the virtual flow instance associated to this manager
    • getState

      public FlowState<T,C> getState()
      Gets the value of the state property.
      Property description:
      Specifies the current state of the viewport as a FlowState object.
      Returns:
      the value of the state property
      See Also:
    • stateProperty

      public ReadOnlyObjectProperty<FlowState<T,C>> stateProperty()
      Specifies the current state of the viewport as a FlowState object.
      Returns:
      the state property
      See Also:
    • setState

      protected void setState(FlowState<T,C> state)
      Sets the value of the state property.
      Property description:
      Specifies the current state of the viewport as a FlowState object.
      Parameters:
      state - the value for the state property
      See Also:
    • getLastRange

      public io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer> getLastRange()
      Gets the value of the lastRange property.
      Property description:
      Specifies the current range of displayed items as a IntegerRange object.
      Returns:
      the value of the lastRange property
      See Also:
    • lastRangeProperty

      public ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>> lastRangeProperty()
      Specifies the current range of displayed items as a IntegerRange object.
      Returns:
      the lastRange property
      See Also:
    • setLastRange

      protected void setLastRange(io.github.palexdev.mfxcore.base.beans.range.IntegerRange lastRange)
      Sets the value of the lastRange property.
      Property description:
      Specifies the current range of displayed items as a IntegerRange object.
      Parameters:
      lastRange - the value for the lastRange property
      See Also: