changeset 11013:e6fb2b3967be

8197985: Pressing Shift + DOWN in ListView causes Exception to be thrown Reviewed-by: kcr, arapte Contributed-by: fastegal@swingempire.de
author fastegal
date Thu, 02 Aug 2018 18:54:08 +0530
parents c4058f0ea40b
children 9ad45ae7a5b0
files modules/javafx.controls/src/main/java/javafx/scene/control/MultipleSelectionModelBase.java modules/javafx.controls/src/test/java/test/javafx/scene/control/ListViewTest.java
diffstat 2 files changed, 31 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/MultipleSelectionModelBase.java	Tue Jul 31 21:16:04 2018 -0700
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/MultipleSelectionModelBase.java	Thu Aug 02 18:54:08 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2018, 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
@@ -751,7 +751,8 @@
                 } else if (size == 1) {
                     _beginChange();
                     int _index = sortedNewIndices.get(0);
-                    _nextAdd(_index, _index + 1);
+                    int indicesIndex = indexOf(_index);
+                    _nextAdd(indicesIndex, indicesIndex + 1);
                     _endChange();
                 } else {
                     _beginChange();
--- a/modules/javafx.controls/src/test/java/test/javafx/scene/control/ListViewTest.java	Tue Jul 31 21:16:04 2018 -0700
+++ b/modules/javafx.controls/src/test/java/test/javafx/scene/control/ListViewTest.java	Thu Aug 02 18:54:08 2018 +0530
@@ -1947,4 +1947,32 @@
 
         sl.dispose();
     }
+
+    @Test
+    public void testEventIndicesOnSelectRange() {
+        ObservableList<String> listItems = FXCollections.observableArrayList("zero", "one", "two", "three");
+        final ListView<String> lv = new ListView<>();
+        lv.setItems(listItems);
+        MultipleSelectionModel<String> sm = lv.getSelectionModel();
+
+        int selected = 1;
+        sm.setSelectionMode(SelectionMode.MULTIPLE);
+        sm.select(selected);
+        sm.getSelectedIndices().addListener((ListChangeListener<Integer>) ch -> {
+            if (ch.next()) {
+                assertEquals("Two items should be selected.", 2, ch.getList().size());
+                assertEquals("Selection range should be from index 1 ", 1, ch.getFrom());
+                assertEquals("Selection range should be till index 2 ", 2, ch.getTo());
+            } else {
+                fail("Change event is expected when selection is changed.");
+            }
+        });
+        int focus = lv.getFocusModel().getFocusedIndex();
+        assertEquals("Selected item should be focused.", selected, focus);
+        // Select the next element
+        sm.selectRange(selected, focus + 2);
+        assertEquals("Two items should be selected.", 2, sm.getSelectedIndices().size());
+        assertEquals("List item at index 1 should be selected", 1, (int) sm.getSelectedIndices().get(0));
+        assertEquals("List item at index 2 should be selected", 2, (int) sm.getSelectedIndices().get(1));
+    }
 }