support preserving `null` selected value when updating combobox drop-down list
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Tue, 16 Feb 2016 16:36:45 +0000 (19:36 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Tue, 16 Feb 2016 16:36:45 +0000 (19:36 +0300)
platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java

index d79e2d659918a5a822b5afedb0276152c6fa085b..fc3676a4b157c22f9322f3f96121152ca11f7e63 100644 (file)
@@ -270,23 +270,27 @@ public class SwingHelper {
     if (!shouldUpdate(comboBox, newItems)) {
       return;
     }
-    Object selectedItem = comboBox.getSelectedItem();
+    Object itemToSelect = comboBox.getSelectedItem();
+    boolean preserveSelection = true;
     //noinspection SuspiciousMethodCalls
-    if (selectedItem != null && !newItems.contains(selectedItem)) {
-      selectedItem = null;
-    }
-    if (selectedItem == null && newItems.contains(newSelectedItemIfSelectionCannotBePreserved)) {
-      selectedItem = newSelectedItemIfSelectionCannotBePreserved;
+    if (!newItems.contains(itemToSelect)) {
+      if (newItems.contains(newSelectedItemIfSelectionCannotBePreserved)) {
+        itemToSelect = newSelectedItemIfSelectionCannotBePreserved;
+      }
+      else {
+        itemToSelect = null;
+        preserveSelection = false;
+      }
     }
     comboBox.removeAllItems();
     for (T newItem : newItems) {
       comboBox.addItem(newItem);
     }
-    if (selectedItem != null) {
+    if (preserveSelection) {
       int count = comboBox.getItemCount();
       for (int i = 0; i < count; i++) {
         Object item = comboBox.getItemAt(i);
-        if (selectedItem.equals(item)) {
+        if (ComparatorUtil.equalsNullable(itemToSelect, item)) {
           comboBox.setSelectedIndex(i);
           break;
         }