Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8088752

ListView: incorrect selection state after insert of uncontained selectedItem

XMLWordPrintable

      Context:
      - muliple selectionMode with multiple indices selected
      - select item that's not in the list
      - add selected item to list
      - expected after insert: selectedItem unchanged, selectedIndex = indexOf(selectedItem)
      - actual: selectedIndex is expected +1, selectedItem changed to one after expected item

      (TableView misbehaves similarly, only the offset seems to be 2 vs. 1 for ListView)

      Below is a failing test. Debugging points to updating selectedIndex from two parts of the code.


          /**
           * Rinse with multiple indices selected.<p>
           *
           * fails due to competing handling of Change:
           * <li> in itemsContentListener falls into block where the index is updated
           * to the index position of the item
           * <li> in updateSelection the wasAdded-block shifts the selectedIndex, thus
           * shifting away from the already correct index
           * <p>
           * best would be to not separate out both - just let selectionChanged handle all
           * then analyse each block whether or not it requires further action
           *
           */
          @Test
          public void testSelectedOnInsertUncontainedMultiple() {
              if (!multipleMode)
                  return;
              Object uncontained = "uncontained";
              // prepare state, select a range
              int start = 3;
              int end = 5;
              getSelectionModel().selectRange(start, end);
              getSelectionModel().select(uncontained);
              assertEquals("sanity: having uncontained selectedItem", uncontained,
                      getSelectionModel().getSelectedItem());
              assertEquals("sanity: selected index removed ", -1, getSelectionModel()
                      .getSelectedIndex());
              // insert uncontained to items
              int insertIndex = 3;
              items.add(insertIndex, uncontained);
              assertEquals("selectedItem unchanged", uncontained,
                      getSelectionModel().getSelectedItem());
              assertEquals("selectedIndex updated", insertIndex, getSelectionModel()
                      .getSelectedIndex());
          }

            jgiles Jonathan Giles
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: