private int hoveredIndex;
private List<T> items = new ArrayList<T>();
private JList6<T> list;
+ private boolean keepSelection = true;
/**
* Create a new {@link ListModel}.
});
}
+ /**
+ * (Try and) keep the elements that were selected when filtering.
+ * <p>
+ * This will use toString on the elements to identify them, and can be a bit
+ * resource intensive.
+ *
+ * @return TRUE if we do
+ */
+ public boolean isKeepSelection() {
+ return keepSelection;
+ }
+
+ /**
+ * (Try and) keep the elements that were selected when filtering.
+ * <p>
+ * This will use toString on the elements to identify them, and can be a bit
+ * resource intensive.
+ *
+ * @param keepSelection
+ * TRUE to try and keep them selected
+ */
+ public void setKeepSelection(boolean keepSelection) {
+ this.keepSelection = keepSelection;
+ }
+
/**
* Check if this element is currently under the mouse.
*
*/
@SuppressWarnings("unchecked") // ListModel<T> and JList<T> are not java 1.6
public void filter(Predicate<T> filter) {
+ ListSnapshot snapshot = null;
+
+ if (keepSelection)
+ snapshot = new ListSnapshot(list);
+
clear();
for (T item : items) {
if (filter == null || filter.test(item)) {
}
}
+ if (keepSelection)
+ snapshot.apply();
+
list.repaint();
}