ListSnapshot (used on ListModel)
authorNiki Roo <niki@nikiroo.be>
Sat, 25 Apr 2020 18:01:12 +0000 (20:01 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 25 Apr 2020 18:01:12 +0000 (20:01 +0200)
ui/ListModel.java
ui/ListSnapshot.java [new file with mode: 0644]

index 06e7914c1a80a5dbcc6287d8d636f96c8de000e2..52fa816a55a577aa1c8b3d27e7c9132443083029 100644 (file)
@@ -81,6 +81,7 @@ public class ListModel<T> extends DefaultListModel6<T> {
        private int hoveredIndex;
        private List<T> items = new ArrayList<T>();
        private JList6<T> list;
+       private boolean keepSelection = true;
 
        /**
         * Create a new {@link ListModel}.
@@ -185,6 +186,31 @@ public class ListModel<T> extends DefaultListModel6<T> {
                });
        }
 
+       /**
+        * (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.
         * 
@@ -289,6 +315,11 @@ public class ListModel<T> extends DefaultListModel6<T> {
         */
        @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)) {
@@ -296,6 +327,9 @@ public class ListModel<T> extends DefaultListModel6<T> {
                        }
                }
 
+               if (keepSelection)
+                       snapshot.apply();
+
                list.repaint();
        }
 
diff --git a/ui/ListSnapshot.java b/ui/ListSnapshot.java
new file mode 100644 (file)
index 0000000..d2e89c8
--- /dev/null
@@ -0,0 +1,62 @@
+package be.nikiroo.utils.ui;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.JList;
+
+public class ListSnapshot {
+       private JList list;
+       private List<Object> elements = new ArrayList<Object>();
+
+       public ListSnapshot(JList list) {
+               this.list = list;
+
+               for (int index : list.getSelectedIndices()) {
+                       elements.add(list.getModel().getElementAt(index));
+               }
+       }
+
+       public void apply() {
+               applyTo(list);
+       }
+
+       public void applyTo(JList list) {
+               List<Integer> indices = new ArrayList<Integer>();
+               for (int i = 0; i < list.getModel().getSize(); i++) {
+                       Object newObject = list.getModel().getElementAt(i);
+                       for (Object oldObject : elements) {
+                               if (isSameElement(oldObject, newObject)) {
+                                       indices.add(i);
+                                       break;
+                               }
+                       }
+               }
+
+               int a[] = new int[indices.size()];
+               for (int i = 0; i < indices.size(); i++) {
+                       a[i] = indices.get(i);
+               }
+               list.setSelectedIndices(a);
+       }
+
+       // You can override this
+       protected boolean isSameElement(Object oldElement, Object newElement) {
+               if (oldElement == null || newElement == null)
+                       return oldElement == null && newElement == null;
+
+               return oldElement.toString().equals(newElement.toString());
+       }
+
+       @Override
+       public String toString() {
+               StringBuilder builder = new StringBuilder();
+               builder.append("List Snapshot of: ").append(list).append("\n");
+               builder.append("Selected elements:\n");
+               for (Object element : elements) {
+                       builder.append("\t").append(element).append("\n");
+               }
+
+               return builder.toString();
+       }
+}