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

ChoiceBox: must update value on setting SelectionModel, part 2

XMLWordPrintable

      Left-over from JDK-8090015

      fixed for
      nothing selected in box -> replace model with selection must update value to selection in model

      still virulent:
      selection in box -> replace model without selection must update value to selection in model

      Culprit is choiceBox.selectionModel property, which explicitly excludes a null selectedItem in the new model:

              @Override protected void invalidated() {
                  if (oldSM != null) {
                      oldSM.selectedItemProperty().removeListener(selectedItemListener);
                  }
                  SelectionModel<T> sm = get();
                  oldSM = sm;
                  if (sm != null) {
                      sm.selectedItemProperty().addListener(selectedItemListener);
                      // unfixed part of JDK-8090015
                      if (sm.getSelectedItem() != null && ! valueProperty().isBound()) {
                          ChoiceBox.this.setValue(sm.getSelectedItem());
                      }
                  }
              }

      Can't think of any reason to do this. There's even a passing test that covers the exclusion - which is wrong, IMO.

      failing test:

          @Test
          public void testSetSelectionModelUpdatesValueStandalone() {
              ObservableList<String> items = FXCollections.observableArrayList(
                      "9-item", "8-item", "7-item", "6-item",
                      "5-item", "4-item", "3-item", "2-item", "1-item");

              ChoiceBox box = new ChoiceBox(items);
              int index = 0;
              box.setValue(box.getItems().get(index));
              assertEquals("sanity: model is selecting index and item", items.get(index),
                      box.getSelectionModel().getSelectedItem());
              SingleSelectionModel model = new SingleSelectionModel() {
                  @Override protected Object getModelItem(int index) {
                      if (index < 0 || index >= getItemCount()) return null;
                      return box.getItems().get(index);
                  }

                  @Override protected int getItemCount() {
                      return box.getItems() != null ? box.getItems().size() : 0;
                  }
              };
              assertEquals(null, model.getSelectedItem());
              box.setSelectionModel(model);
              assertEquals("box value must be same as selected item", null, box.getValue());
          }

            fastegal Jeanette Winzenburg
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: