Class VFXGridManager<T, C extends VFXCell<T>>
VFXGrid. Although, to be precise, and as the name also suggests,
this can be considered more like a 'manager' than a behavior. Behaviors typically respond to user input, and then
update the component's state. This behavior contains core methods to respond to various properties change
in VFXGrid. All computations here will generate a new VFXGridState, if possible, and update the grid
and the layout (indirect, call to VFXGrid.requestViewportLayout()).
By default, manages the following changes:
- geometry changes (width/height changes), onGeometryChanged()
- position changes, onPositionChanged(Orientation)
- number of columns changes, onColumnsNumChanged()
- cell factory changes, onCellFactoryChanged()
- cell size changes, onCellSizeChanged()
- spacing changes, onSpacingChanged()
- items changes, onItemsChanged()
VFXContainerHelper.invalidatePos() is called when necessary.
However, invalidating the positions, also means that the onPositionChanged(Orientation) method could be potentially
triggered, thus generating an unwanted 'middle' state. For this reason a special fla invalidatingPos is set
to true before the invalidation, so that the other method will exit immediately. It's reset back to false
after the computation or if any of the checks before the actual computation fails.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanAvoids code duplication.protected booleanAvoids code duplication.protected voidmoveReuseCreateAlgorithm(io.github.palexdev.mfxcore.base.beans.range.IntegerRange rowsRange, io.github.palexdev.mfxcore.base.beans.range.IntegerRange columnsRange, VFXGridState<T, C> newState) Avoids code duplication.protected voidThis method is responsible for updating the grid's state when theVFXGrid.getCellFactory()changes.protected voidThis method is responsible for computing a new state when theVFXGrid.cellSizeProperty()changes.protected voidThis core method is responsible for updating the grid's state when the number of columns specified byVFXGrid.columnsNumProperty()changes.protected voidThis core method is responsible for ensuring that the viewport always has the right number of cells.protected voidBefore describing the operations performed by this method, it's important for the reader to understand the difference between the two changes caught by this method.protected voidonPositionChanged(Orientation axis) This core method is responsible for updating the grid's state when the vertical and horizontal positions change.protected voidThis method is responsible for updating the grid's state when either theVFXGrid.hSpacingProperty()orVFXGrid.vSpacingProperty()change.protected booleanrangeCheck(io.github.palexdev.mfxcore.base.beans.range.IntegerRange rowsRange, io.github.palexdev.mfxcore.base.beans.range.IntegerRange columnsRange, boolean update, boolean dispose) Avoids code duplication.protected voidremainingAlgorithm(Set<Integer> remaining, VFXGridState<T, C> newState) Avoids code duplication.Methods inherited from class io.github.palexdev.mfxcore.behavior.BehaviorBase
dispose, getActions, getNode, init, keyPressed, keyPressed, keyReleased, keyReleased, keyTyped, keyTyped, mouseClicked, mouseClicked, mouseDragged, mouseDragged, mouseEntered, mouseEntered, mouseExited, mouseExited, mouseMoved, mouseMoved, mousePressed, mousePressed, mouseReleased, mouseReleased, register, scroll, scroll, touchMoved, touchMoved, touchPressed, touchPressed, touchReleased, touchReleased, touchStationary, touchStationary
-
Field Details
-
invalidatingPos
protected boolean invalidatingPos
-
-
Constructor Details
-
VFXGridManager
-
-
Method Details
-
onGeometryChanged
protected void onGeometryChanged()This core method is responsible for ensuring that the viewport always has the right number of cells. This is called every time the grid's geometry changes (width/height changes), which means that this is also responsible for the initialization (when width/height becomes > 0.0).After preliminary checks done by
Note that to compute a valid new state, it is important to also validate the grid's positions by invokinggridFactorySizeCheck()andrangeCheck(IntegerRange, IntegerRange, boolean, boolean), the computation of the new state is delegated to themoveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState).VFXContainerHelper.invalidatePos(). -
onPositionChanged
This core method is responsible for updating the grid's state when the vertical and horizontal positions change. Since the grid doesn't use any throttling technique to limit the number of events/changes, and since scrolling can happen very fast, performance here is crucial.Immediately exits if: the special flag
Before further discussing the internal mechanisms of this method, notice that this accepts a parameter of typeinvalidatingPosis true or the current state isVFXGridState.INVALID. Many other computations here need to validate the positions by callingVFXContainerHelper.invalidatePos(), to ensure that the resulting state is valid. However, invalidating the positions may trigger this method, causing two or more state computations to run at the 'same time'; this behavior must be avoided, and that flag exists specifically for this reason.Orientation. The reason is simple. The grid is virtualized on both the x-axis and y-axis, but changes are 'atomic', meaning that only one can be processed at a time. For the scroll it's the same. If you imagine scroll on a timeline, each event occurs after the other. Even if you scroll in both directions at the same time, there's a difference between what you perceive and what happens under the hood. For this reason, to also avoid code duplication, that parameter tells this method on which axis the scroll happened, so that we can get the right ranges and values for the computation: vertical -> rowsRange, horizontal -> columnsRange.Also, note that for the same reason, the algorithm differs from the one of
VFXListManager. When scrolling in the grid, it is indeed possible to have less or more cells than the current state. That's because rows/columns can be incomplete. So, themoveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState)will do the trick here.Before delegating the computation to the aforementioned algorithm though, there are a few prerequisites and checks:
- the last range (retrieved from the current state)
- the new range (give by
VFXGridHelper)- exits if these two ranges are equal or the new range in invalid ([-1, -1])
At the end, the current state is disposed by callingdisposeCurrent(), and if it was not empty we also set the new state'sVFXGridState.haveCellsChanged()flag totrue. Finally, we can callVFXGrid.update(VFXGridState)with the new state and also callVFXGrid.requestViewportLayout(). Note that this last call will be done only if theVFXGridState.haveCellsChanged()was not set totrue. In other words, when the position changes, we always want to update the layout to ensure that cells are at the correct x and y coordinates. -
onColumnsNumChanged
protected void onColumnsNumChanged()This core method is responsible for updating the grid's state when the number of columns specified byVFXGrid.columnsNumProperty()changes.After preliminary checks done by
At the end, the current state is disposed by callinggridFactorySizeCheck()andrangeCheck(IntegerRange, IntegerRange, boolean, boolean), the computation for the new state is delegated to themoveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState).disposeCurrent(), and if it was not empty we also set the new state'sVFXGridState.haveCellsChanged()flag totrue. Finally, we can callVFXGrid.update(VFXGridState)with the new state and also callVFXGrid.requestViewportLayout(). Note that this last call will be done only if theVFXGridState.haveCellsChanged()was not set totrue. In other words, when the position changes, we always want to update the layout to ensure that cells are at the correct x and y coordinates. Note that to compute a valid new state, it is important to also validate the grid's positions by invokingVFXContainerHelper.invalidatePos(). -
onCellFactoryChanged
protected void onCellFactoryChanged()This method is responsible for updating the grid's state when theVFXGrid.getCellFactory()changes. Unfortunately, this is always a costly operation because all cells need to be re-created, and theVFXCellsCachecleaned. In fact, the very first operation done by this method is exactly this, the disposal of the current state and the cleaning of the cache. Luckily, this kind of change is likely to not happen very often.After preliminary checks done by
gridFactorySizeCheck()andrangeCheck(IntegerRange, IntegerRange, boolean, boolean), the computation for the new state is delegated to themoveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState).The new state's
VFXGridState.haveCellsChanged()flag will always betrueof course. The great thing about the factory change is that there is no need to invalidate the position. -
onCellSizeChanged
protected void onCellSizeChanged()This method is responsible for computing a new state when theVFXGrid.cellSizeProperty()changes.After preliminary checks done by
Note that to compute a valid new state, it is important to also validate the grid's positions by invokinggridFactorySizeCheck(), the computation for the new state is delegated to themoveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState).VFXContainerHelper.invalidatePos(). Note that this will request the layout computation,VFXGrid.requestViewportLayout(), even if the cells didn't change for obvious reasons. -
onSpacingChanged
protected void onSpacingChanged()This method is responsible for updating the grid's state when either theVFXGrid.hSpacingProperty()orVFXGrid.vSpacingProperty()change.After preliminary checks done by
Note that to compute a valid new state, it is important to also validate the grid's positions by invokinggridFactorySizeCheck()andrangeCheck(IntegerRange, IntegerRange, boolean, boolean), the computation for the new state is delegated to themoveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState).VFXContainerHelper.invalidatePos(). Also, this will request the layout computation,VFXGrid.requestViewportLayout(), even if the cells didn't change for obvious reasons. -
onItemsChanged
protected void onItemsChanged()Before describing the operations performed by this method, it's important for the reader to understand the difference between the two changes caught by this method.VFXGridmakes use of aListPropertyto store the items to display. The property is essentially the equivalent of thisObjectProperty<ObservableList>. Now, if you are familiar with JavaFX, you probably know that there are two possible changes to listen to: one is changes toObjectProperty(if theObservableListinstance changes), and the other are changes in theObservableListinstance itself. As you may guess, managing both these changes with a simpleObjectPropertyis quite cumbersome, because you need two listeners: one that that catches changes in the list, and another to catch changes to the property. In particular, the latter has the task to add the first listener to the newObservableListinstance.And here is where
ListPropertycomes in handy. By adding anInvalidationListenerto this special property we are able to intercept both the type of changes always, even if theObservableListinstance changes, everything is handled automatically.Needless to say, we use a
This core method is responsible for updating the grid's state when any of the two aforementioned changes happen.Propertyto store the items to allow the usage of bindings!These kind of updates are the most tricky and expensive. In particular, additions and removals can occur at any position in the list, which means that calculating the new state solely on the indexes is a no-go. It is indeed possible by, in theory, isolating the indexes at which the changes occurred, separating the cells that need only an index update from the ones that actually need a full update. However, such an approach requires a lot of code, is error-prone, and a bit heavy on performance. The new approach implemented here requires changes to the state class as well,
VFXGridState.The computation for the new state is similar to the
For this reason, cells from the old state are not removed by index, but by item,moveReuseCreateAlgorithm(IntegerRange, IntegerRange, VFXGridState), but the first step, which tries to identify the common cells, is quite different. You see, as I said before, additions and removals can occur at any place in the list. Making an example for a 2D structure such as the grid would take a lot of space and effort, so to better understand what can happen, you can check the example I wrote hereVFXListManager(go to the same method).VFXGridState.removeCell(Object). First, we retrieve the item from the list from the row and column coordinates (make sure to read theVFXGridStatedocs to understand how indexes are managed!), then we try to remove tge cell for that item from the current state. If the cell is found, we update it by index and add it to the new state. Here's a difference between this method and the one used byVFXListManager: the latter expands the range of items to display to aSetand removes every index for which a cell is found in the current state. This, however, is not possible here, because we have two ranges, so instead we do the opposite. While iterating on the ranges, if the cell was not found, then we add the index to aSet, this set represents the remaining items, those for which any of these three things may happen: a new cell is created, a cell is polled from the cache and updated, a cell from the current state is updated and moved to the new state.The processing of those remaining elements is delegated to the
Last notes:remainingAlgorithm(Set, VFXGridState).1) This is one of those methods that to produce a valid new state needs to validate the grid's positions, so it calls
VFXContainerHelper.invalidatePos()2) Before invalidating the position, this must also request the re-computation of the container's virtual sizes by calling
VFXContainerHelper.invalidateVirtualSizes()3) To make sure the layout is always correct, at the end we always invoke
VFXGrid.requestViewportLayout(). After changes like this, items may be still present in the viewport, but at different indexes, which translates to different layout positions. There is no easy way to detect this, so better safe than sorry, always update the layout. -
moveReuseCreateAlgorithm
protected void moveReuseCreateAlgorithm(io.github.palexdev.mfxcore.base.beans.range.IntegerRange rowsRange, io.github.palexdev.mfxcore.base.beans.range.IntegerRange columnsRange, VFXGridState<T, C> newState) Avoids code duplication. Typically used when, while iterating on the rows and columns ranges, it's enough to move the cells from the current state to the new state. For indexes which are not found in the current state, a new cell is either taken from the old state, taken from cache or created by the cell factory.(The last operations are delegated to the
remainingAlgorithm(Set, VFXGridState)).- See Also:
-
remainingAlgorithm
Avoids code duplication. Typically used to process indexes not found in the current state.For any index in the given collection, a cell is needed. Also, it needs to be updated by index and item both. This cell can come from three sources:
1) from the current state if it's not empty yet. Since the cells are stored in a
SequencedMap, one is removed by callingIndexBiMap.StateMapBase.pollFirst().2) from the
VFXCellsCacheif not empty3) created by the cell factory
- See
VFXGridHelper.indexToCell(int): this handles the second and third cases. If a cell can be taken from the cache, automatically updates its item then returns it. Otherwise, invokes theVFXGrid.getCellFactory()to create a new one -
gridFactorySizeCheck
protected boolean gridFactorySizeCheck()Avoids code duplication. This method checks for three things:1) If the list is empty
2) If the cell factory is
null3) If the cell size is lesser or equal to 0
If any of those checks is true: the grid's state is set to
VFXGridState.INVALID, the current state is disposed, the 'invalidatingPos' flag is reset, finally returns false.Otherwise, does nothing and returns true.
- See
CellFactory.canCreate()- See
VFXGrid.cellSizeProperty()- See
disposeCurrent(): for the current state disposal- Returns:
- whether all the aforementioned checks have passed
-
rangeCheck
protected boolean rangeCheck(io.github.palexdev.mfxcore.base.beans.range.IntegerRange rowsRange, io.github.palexdev.mfxcore.base.beans.range.IntegerRange columnsRange, boolean update, boolean dispose) Avoids code duplication. Used to check whether the given ranges are valid, not equal toUtils.INVALID_RANGE.When invalid, returns false, but first runs the following operations: disposes the current state (only if the 'dispose' parameter is true), sets the grid's state to
VFXGridState.INVALID(only if the 'update' parameter is true), resets the 'invalidatingPos' flag. Otherwise, does nothing and returns true.Last but not least, this is a note for the future on why the method is structured like this. It's crucial for the disposal operation to happen before the list's state is set to
VFXGridState.INVALID, otherwise the disposal method will fail, since it will then retrieve the empty state instead of the correct one.- See
disposeCurrent(): for the current state disposal- Parameters:
rowsRange- the rows range to checkcolumnsRange- the columns range to checkupdate- whether to set the grid's state to 'empty' if the range is not validdispose- whether to dispose the current/old state if the range is not valid- Returns:
- whether the range is valid or not
-
disposeCurrent
protected boolean disposeCurrent()Avoids code duplication. Responsible for disposing the current state if it is not empty.- See
VFXGridState.dispose()- Returns:
- whether the disposal was done or not
-