Skip to content
Permalink
Browse files
8189354: Change.getRemoved() list contains incorrect selected items w…
…hen a TreeItem is collapsed

Reviewed-by: arapte, fastegal
  • Loading branch information
mstr2 authored and arapte committed May 10, 2021
1 parent ab7c151 commit eb913cb78bbc7a4973d1dfab3b02335edeff72aa
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -32,8 +32,6 @@
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@@ -201,28 +199,4 @@ private void checkState() {

sm.selectedIndices._endChange();
}

// Given a listen of removed elements, we create the minimal number of changes by coalescing elements that are
// adjacent
static void reducingChange(MultipleSelectionModelBase<?>.SelectedIndicesList selectedIndices, List<Integer> removed) {
if (removed.isEmpty()) return;

int startPos = 0;
int endPos = 1;
boolean firedOnce = false;
while (endPos < removed.size()) {
if (removed.get(startPos) == removed.get(endPos) - 1) {
endPos++;
continue;
}
selectedIndices._nextRemove(selectedIndices.indexOf(removed.get(startPos)), removed.subList(startPos, endPos));
startPos = endPos;
endPos = startPos + 1;
firedOnce = true;
}

if (!firedOnce) {
selectedIndices._nextRemove(selectedIndices.indexOf(removed.get(0)), removed);
}
}
}
@@ -2551,7 +2551,9 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
}
}

ControlUtils.reducingChange(selectedIndices, removed);
if (!removed.isEmpty()) {
selectedIndices._nextRemove(selectedIndices.indexOf(removed.get(0)), removed);
}

for (int index : removed) {
startAtomic();
@@ -1375,7 +1375,9 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
}
}

ControlUtils.reducingChange(selectedIndices, removed);
if (!removed.isEmpty()) {
selectedIndices._nextRemove(selectedIndices.indexOf(removed.get(0)), removed);
}

for (int index : removed) {
startAtomic();
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -46,6 +46,7 @@
import javafx.collections.transformation.FilteredList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.javafx.collections.MockListObserver;
import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
import test.com.sun.javafx.scene.control.infrastructure.KeyModifier;
import test.com.sun.javafx.scene.control.infrastructure.MouseEventFirer;
@@ -6641,4 +6642,24 @@ private void test_jdk_8169642(Consumer<TreeTableView.TreeTableViewSelectionModel
}
);
}

@Test public void testRemovedSelectedItemsWhenBranchIsCollapsed() {
TreeItem<String> c1, c2, c3;
TreeItem<String> root = new TreeItem<>("foo");
root.getChildren().add(c1 = new TreeItem<>("bar"));
root.getChildren().add(c2 = new TreeItem<>("baz"));
root.getChildren().add(c3 = new TreeItem<>("qux"));
root.setExpanded(true);

TreeTableView<String> treeTableView = new TreeTableView<>(root);
treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
treeTableView.getSelectionModel().selectAll();

MockListObserver<TreeItem<String>> observer = new MockListObserver<>();
treeTableView.getSelectionModel().getSelectedItems().addListener(observer);
root.setExpanded(false);

observer.check1();
observer.checkAddRemove(0, treeTableView.getSelectionModel().getSelectedItems(), List.of(c1, c2, c3), 1, 1);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
import com.sun.javafx.scene.control.behavior.TreeCellBehavior;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.control.*;
import test.javafx.collections.MockListObserver;
import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
import test.com.sun.javafx.scene.control.infrastructure.KeyModifier;
import test.com.sun.javafx.scene.control.infrastructure.StageLoader;
@@ -3681,4 +3682,24 @@ private void test_jdk8131924(boolean showRoot) {
// in the selectedIndices and selectedItems list
childNode1.setExpanded(false);
}

@Test public void testRemovedSelectedItemsWhenBranchIsCollapsed() {
TreeItem<String> c1, c2, c3;
TreeItem<String> root = new TreeItem<>("foo");
root.getChildren().add(c1 = new TreeItem<>("bar"));
root.getChildren().add(c2 = new TreeItem<>("baz"));
root.getChildren().add(c3 = new TreeItem<>("qux"));
root.setExpanded(true);

TreeView<String> treeView = new TreeView<>(root);
treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
treeView.getSelectionModel().selectAll();

MockListObserver<TreeItem<String>> observer = new MockListObserver<>();
treeView.getSelectionModel().getSelectedItems().addListener(observer);
root.setExpanded(false);

observer.check1();
observer.checkAddRemove(0, treeView.getSelectionModel().getSelectedItems(), List.of(c1, c2, c3), 1, 1);
}
}

1 comment on commit eb913cb

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on eb913cb May 10, 2021

Please sign in to comment.