diff --git a/src/main/java/com/ldtteam/blockui/mod/ScrollingListsGui.java b/src/main/java/com/ldtteam/blockui/mod/ScrollingListsGui.java index 760572f9..71ea0432 100644 --- a/src/main/java/com/ldtteam/blockui/mod/ScrollingListsGui.java +++ b/src/main/java/com/ldtteam/blockui/mod/ScrollingListsGui.java @@ -13,6 +13,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; /** @@ -127,5 +129,32 @@ public void updateElement(final int index, final Pane rowPane) window.findPaneOfTypeByID("list4add", Button.class).setHandler(button -> renderAmount.getAndAdd(2)); window.findPaneOfTypeByID("list4remove", Button.class).setHandler(button -> renderAmount.getAndAdd(-2)); + + // Case 5: A list that will not update + final AtomicBoolean shouldRenderFlag = new AtomicBoolean(); + final ScrollingList list5 = window.findPaneOfTypeByID("list5", ScrollingList.class); + list5.setDataProvider(new DataProvider() + { + @Override + public int getElementCount() + { + return 10; + } + + @Override + public boolean shouldUpdate() + { + return shouldRenderFlag.get(); + } + + @Override + public void updateElement(final int index, final Pane rowPane) + { + shouldRenderFlag.set(false); + rowPane.findPaneByType(Text.class).setText(Component.literal("Hi " + index + " " + UUID.randomUUID())); + } + }); + + window.findPaneOfTypeByID("list5update", Button.class).setHandler(button -> shouldRenderFlag.set(true)); } } diff --git a/src/main/java/com/ldtteam/blockui/views/ScrollingList.java b/src/main/java/com/ldtteam/blockui/views/ScrollingList.java index 7727dd9a..2989ca2b 100644 --- a/src/main/java/com/ldtteam/blockui/views/ScrollingList.java +++ b/src/main/java/com/ldtteam/blockui/views/ScrollingList.java @@ -121,7 +121,7 @@ public void updateElement(final int index, final Pane rowPane) public void setDataProvider(final DataProvider p) { dataProvider = p; - refreshElementPanes(); + refreshElementPanes(true); } /** @@ -129,14 +129,24 @@ public void setDataProvider(final DataProvider p) */ public void refreshElementPanes() { - ((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing); + refreshElementPanes(true); + } + + /** + * Use the data provider to update all the element panes. + * + * @param force should the list be forcefully updated. + */ + public void refreshElementPanes(final boolean force) + { + ((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing, force); } @Override public void onUpdate() { super.onUpdate(); - refreshElementPanes(); + refreshElementPanes(false); } @Override @@ -185,13 +195,33 @@ public interface DataProvider */ int getElementCount(); + /** + * Should all the children update again? + * + * @return true if the updates should be made + */ + default boolean shouldUpdate() + { + return true; + } + + /** + * Should the specific child update again? + * + * @return true if the updates should be made + */ + default boolean shouldUpdate(final int index) + { + return true; + } + /** * Override this to pick a custom size for this element. Event contains the logic to modify the old size. * * @param index the index of the row/list element. * @param modifier the object used to modify the size. */ - default void modifyRowSize(int index, final RowSizeModifier modifier) + default void modifyRowSize(final int index, final RowSizeModifier modifier) { // No implementation by default } diff --git a/src/main/java/com/ldtteam/blockui/views/ScrollingListContainer.java b/src/main/java/com/ldtteam/blockui/views/ScrollingListContainer.java index 14d3ccee..494ff8b9 100644 --- a/src/main/java/com/ldtteam/blockui/views/ScrollingListContainer.java +++ b/src/main/java/com/ldtteam/blockui/views/ScrollingListContainer.java @@ -96,9 +96,15 @@ public void setListNodeParams(final @NotNull PaneParams listNodeParams) * @param dataProvider data provider object, shouldn't be null. * @param height the maximum height of the parent. * @param childSpacing the spacing between each row. + * @param force should the list be forcefully updated. */ - public void refreshElementPanes(final DataProvider dataProvider, final int height, final int childSpacing) + public void refreshElementPanes(final DataProvider dataProvider, final int height, final int childSpacing, final boolean force) { + if (dataProvider == null) + { + return; + } + int currentYpos = 0; if (listNodeParams == null) @@ -107,12 +113,17 @@ public void refreshElementPanes(final DataProvider dataProvider, final int heigh return; } + if (!force && !dataProvider.shouldUpdate()) + { + return; + } + if (this.width != emptyTextComponent.getWidth() || this.height != emptyTextComponent.getHeight()) { emptyTextComponent.setSize(this.width, this.height); } - final int numElements = (dataProvider != null) ? dataProvider.getElementCount() : 0; + final int numElements = dataProvider.getElementCount(); if (numElements > 0) { if (emptyTextComponent.getParent() != null) @@ -150,7 +161,10 @@ public void refreshElementPanes(final DataProvider dataProvider, final int heigh child.setSize(modifier.width, modifier.height); } - dataProvider.updateElement(i, child); + if (force || dataProvider.shouldUpdate(i)) + { + dataProvider.updateElement(i, child); + } } currentYpos += elementHeight + childSpacing; diff --git a/src/main/resources/assets/blockui/gui/test4.xml b/src/main/resources/assets/blockui/gui/test4.xml index 1aa147ef..0b52c84d 100644 --- a/src/main/resources/assets/blockui/gui/test4.xml +++ b/src/main/resources/assets/blockui/gui/test4.xml @@ -1,4 +1,4 @@ - + @@ -21,4 +21,8 @@