Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8290348: TreeTableView jumping to top
Reviewed-by: aghaisas, kcr
  • Loading branch information
Johan Vos committed Jul 21, 2022
1 parent 25e8605 commit 6da05c2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 35 deletions.
Expand Up @@ -3069,45 +3069,51 @@ private void recalculateEstimatedSize() {
recalculateAndImproveEstimatedSize(DEFAULT_IMPROVEMENT);
}

private boolean recalculating = false;

private void recalculateAndImproveEstimatedSize(int improve) {
int itemCount = getCellCount();
int cacheCount = itemSizeCache.size();
boolean keepRatio = ((cacheCount > 0) && !Double.isInfinite(this.absoluteOffset));
double estSize = estimatedSize / itemCount;

int oldIndex = computeCurrentIndex();
double oldOffset = computeViewportOffset(getPosition());
int added = 0;
while ((itemCount > itemSizeCache.size()) && (added < improve)) {
getOrCreateCellSize(itemSizeCache.size());
added++;
}
cacheCount = itemSizeCache.size();
int cnt = 0;
double tot = 0d;
for (int i = 0; (i < itemCount && i < cacheCount); i++) {
Double il = itemSizeCache.get(i);
if (il != null) {
tot = tot + il;
cnt++;
}
}
this.estimatedSize = cnt == 0 ? 1d : tot * itemCount / cnt;
estSize = estimatedSize / itemCount;

if (keepRatio) {
double newOffset = 0;
for (int i = 0; i < oldIndex; i++) {
double h = getCellSize(i);
if (h < 0) {
h = estSize;
if (recalculating) return;
recalculating = true;
try {
int itemCount = getCellCount();
int cacheCount = itemSizeCache.size();
boolean keepRatio = ((cacheCount > 0) && !Double.isInfinite(this.absoluteOffset));

int oldIndex = computeCurrentIndex();
double oldOffset = computeViewportOffset(getPosition());
int added = 0;
while ((itemCount > itemSizeCache.size()) && (added < improve)) {
getOrCreateCellSize(itemSizeCache.size());
added++;
}
cacheCount = itemSizeCache.size();
int cnt = 0;
double tot = 0d;
for (int i = 0; (i < itemCount && i < cacheCount); i++) {
Double il = itemSizeCache.get(i);
if (il != null) {
tot = tot + il;
cnt++;
}
newOffset += h;
}
this.absoluteOffset = newOffset + oldOffset;
adjustPosition();
}
this.estimatedSize = cnt == 0 ? 1d : tot * itemCount / cnt;
double estSize = estimatedSize / itemCount;

if (keepRatio) {
double newOffset = 0;
for (int i = 0; i < oldIndex; i++) {
double h = getCellSize(i);
if (h < 0) {
h = estSize;
}
newOffset += h;
}
this.absoluteOffset = newOffset + oldOffset;
adjustPosition();
}
} finally {
recalculating = false;
}
}

private void resetSizeEstimates() {
Expand Down
Expand Up @@ -3907,6 +3907,35 @@ public void testRemoveTreeItemFromCollapsedAncestorKeepsSelectedItem() {
assertEquals("Node 0", table.getFocusModel().getFocusedItem().getValue());
}

private List<TreeItem<String>> generateChildren(int lvl) {
List<TreeItem<String>> children = new ArrayList<>();
for (int idx = 0; idx < 10; idx++) {
TreeItem<String> child = new TreeItem<>("Child lvl. " + lvl + " idx. " + idx);
child.setExpanded(true);
if (lvl <= 2) {
child.getChildren().addAll(generateChildren(lvl + 1));
}
children.add(child);
}
return children;
}

// JDK-8290348
@Test
public void testCheckPositionAfterCollapsed() {
TreeItem<String> rootNode = new TreeItem<>("Root");
rootNode.setExpanded(true);
rootNode.getChildren().addAll(generateChildren(1));
TreeView<String> treeView = new TreeView<>(rootNode);
treeView.scrollTo(100);
IndexedCell expandedCell = VirtualFlowTestUtils.getCell(treeView, 100);
Toolkit.getToolkit().firePulse();
rootNode.getChildren().get(1).setExpanded(false);
Toolkit.getToolkit().firePulse();
IndexedCell scrolledCell = VirtualFlowTestUtils.getCell(treeView, 100);
assertTrue(scrolledCell.isVisible());
}

public static class MisbehavingOnCancelTreeCell<S> extends TreeCell<S> {

@Override
Expand Down

1 comment on commit 6da05c2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.