void (@NotNull CollectionListModel<T> listModel, @NotNull JList<? extends T> list, @NotNull List<? extends T> newItems) { setMergeListItemsRunning(true); try { if (listModel.getSize() == 0) { listModel.add(newItems); } else if (newItems.isEmpty()) { listModel.removeAll(); } else { int newSelectedIndex = -1; T selection = list.getSelectedValue(); if (selection != null) { newSelectedIndex = newItems.indexOf(selection); } listModel.removeAll(); listModel.add(newItems); list.setSelectedIndex(newSelectedIndex); } } finally { setMergeListItemsRunning(false); } }