Skip to content

Commit

Permalink
8087555: [ChoiceBox] uncontained value not shown
Browse files Browse the repository at this point in the history
Reviewed-by: arapte, aghaisas
  • Loading branch information
Jeanette Winzenburg authored and arapte committed Apr 28, 2020
1 parent 818ac00 commit ceb3fce
Show file tree
Hide file tree
Showing 2 changed files with 456 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,14 @@ public ChoiceBoxSkin(ChoiceBox<T> control) {
updatePopupItems();
updateSelectionModel();
updateSelection();
if(selectionModel != null && selectionModel.getSelectedIndex() == -1) {
label.setText(""); // clear label text when selectedIndex is -1
}
});
registerChangeListener(control.converterProperty(), e -> {
updateChoiceBoxItems();
updatePopupItems();
updateLabelText();
});
registerChangeListener(control.valueProperty(), e -> {
updateLabelText();
});
}

Expand Down Expand Up @@ -323,9 +324,19 @@ private void initialize() {

updateSelectionModel();
updateSelection();
if(selectionModel != null && selectionModel.getSelectedIndex() == -1) {
label.setText(""); // clear label text when selectedIndex is -1
updateLabelText();
}

private void updateLabelText() {
T value = getSkinnable().getValue();
label.setText(getDisplayText(value));
}

private String getDisplayText(T value) {
if (getSkinnable().getConverter() != null) {
return getSkinnable().getConverter().toString(value);
}
return value == null ? "" : value.toString();
}

private void updateChoiceBoxItems() {
Expand Down Expand Up @@ -356,9 +367,7 @@ private void addPopupItem(final T o, int i) {
} else if (o instanceof SeparatorMenuItem) {
popupItem = (SeparatorMenuItem) o;
} else {
StringConverter<T> c = getSkinnable().getConverter();
String displayString = (c == null) ? ((o == null) ? "" : o.toString()) : c.toString(o);
final RadioMenuItem item = new RadioMenuItem(displayString);
final RadioMenuItem item = new RadioMenuItem(getDisplayText(o));
item.setId("choice-box-menu-item");
item.setToggleGroup(toggleGroup);
item.setOnAction(e -> {
Expand Down Expand Up @@ -401,11 +410,11 @@ private void updateSelectionModel() {
private void updateSelection() {
if (selectionModel == null || selectionModel.isEmpty()) {
toggleGroup.selectToggle(null);
label.setText("");
} else {
} else {
int selectedIndex = selectionModel.getSelectedIndex();
if (selectedIndex == -1 || selectedIndex > popup.getItems().size()) {
label.setText(""); // clear label text
// FIXME: when do we get here?
// and if, shouldn't we unselect the toggles?
return;
}
if (selectedIndex < popup.getItems().size()) {
Expand All @@ -417,8 +426,6 @@ private void updateSelection() {
// to be selected
toggleGroup.selectToggle(null);
}
// update the label
label.setText(popup.getItems().get(selectedIndex).getText());
}
}
}
Expand Down
Loading

0 comments on commit ceb3fce

Please sign in to comment.