X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=ui%2FListModel.java;h=52fa816a55a577aa1c8b3d27e7c9132443083029;hb=f2573c3765c5e2d436c4d2871f3a450ee3ada1ed;hp=06e7914c1a80a5dbcc6287d8d636f96c8de000e2;hpb=4d4527e78b434a5962364ae7f7e4c619712ca5fd;p=fanfix.git diff --git a/ui/ListModel.java b/ui/ListModel.java index 06e7914..52fa816 100644 --- a/ui/ListModel.java +++ b/ui/ListModel.java @@ -81,6 +81,7 @@ public class ListModel extends DefaultListModel6 { private int hoveredIndex; private List items = new ArrayList(); private JList6 list; + private boolean keepSelection = true; /** * Create a new {@link ListModel}. @@ -185,6 +186,31 @@ public class ListModel extends DefaultListModel6 { }); } + /** + * (Try and) keep the elements that were selected when filtering. + *

+ * 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. + *

+ * 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. * @@ -289,6 +315,11 @@ public class ListModel extends DefaultListModel6 { */ @SuppressWarnings("unchecked") // ListModel and JList are not java 1.6 public void filter(Predicate filter) { + ListSnapshot snapshot = null; + + if (keepSelection) + snapshot = new ListSnapshot(list); + clear(); for (T item : items) { if (filter == null || filter.test(item)) { @@ -296,6 +327,9 @@ public class ListModel extends DefaultListModel6 { } } + if (keepSelection) + snapshot.apply(); + list.repaint(); }