public static <T> void updateItems(@NotNull JComboBox<T> comboBox,
                                     @NotNull List<? extends T> newItems,
                                     @Nullable T newSelectedItemIfSelectionCannotBePreserved) {
    if (!shouldUpdate(comboBox, newItems)) {
      return;
    }
    Object itemToSelect = comboBox.getSelectedItem();
    boolean preserveSelection = true;
    //noinspection SuspiciousMethodCalls
    if (itemToSelect == null || !newItems.contains(itemToSelect)) {
      if (newSelectedItemIfSelectionCannotBePreserved != null && newItems.contains(newSelectedItemIfSelectionCannotBePreserved)) {
        itemToSelect = newSelectedItemIfSelectionCannotBePreserved;
      }
      else {
        itemToSelect = null;
        preserveSelection = false;
      }
    }
    comboBox.removeAllItems();
    for (T newItem : newItems) {
      comboBox.addItem(newItem);
    }
    if (preserveSelection) {
      int count = comboBox.getItemCount();
      for (int i = 0; i < count; i++) {
        Object item = comboBox.getItemAt(i);
        if (ComparatorUtil.equalsNullable(itemToSelect, item)) {
          comboBox.setSelectedIndex(i);
          break;
        }
      }
    }
  }
public static <T> void updateItems(@NotNull JComboBox<T> comboBox,
                                     @NotNull List<? extends T> newItems,
                                     @Nullable T newSelectedItemIfSelectionCannotBePreserved)