some update/refresh fixes
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / BooksPanel.java
index 579f394702aa9a7d4a45f6fcd13cca3d743342d5..04ee86fce036602c9e15962f463b4eabc1270049 100644 (file)
@@ -18,7 +18,6 @@ import java.util.concurrent.ExecutionException;
 
 import javax.swing.DefaultListModel;
 import javax.swing.JList;
-import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
 import javax.swing.ListCellRenderer;
 import javax.swing.ListSelectionModel;
@@ -33,10 +32,11 @@ import be.nikiroo.fanfix_swing.gui.book.BookBlock;
 import be.nikiroo.fanfix_swing.gui.book.BookInfo;
 import be.nikiroo.fanfix_swing.gui.book.BookLine;
 import be.nikiroo.fanfix_swing.gui.book.BookPopup;
+import be.nikiroo.fanfix_swing.gui.utils.ListenerPanel;
 import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
 
-public class BooksPanel extends JPanel {
-       class ListModel extends DefaultListModel<BookInfo> {
+public class BooksPanel extends ListenerPanel {
+       private class ListModel extends DefaultListModel<BookInfo> {
                public void fireElementChanged(BookInfo element) {
                        int index = indexOf(element);
                        if (index >= 0) {
@@ -45,6 +45,8 @@ public class BooksPanel extends JPanel {
                }
        }
 
+       static public final String INVALIDATE_CACHE = "invalidate_cache";
+
        private List<BookInfo> bookInfos = new ArrayList<BookInfo>();
        private Map<BookInfo, BookLine> books = new HashMap<BookInfo, BookLine>();
        private boolean seeWordCount;
@@ -68,7 +70,7 @@ public class BooksPanel extends JPanel {
                searchBar.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               reload(searchBar.getText());
+                               filter(searchBar.getText());
                        }
                });
 
@@ -127,7 +129,7 @@ public class BooksPanel extends JPanel {
                        protected List<BookInfo> doInBackground() throws Exception {
                                List<BookInfo> bookInfos = new ArrayList<BookInfo>();
                                BasicLibrary lib = Instance.getInstance().getLibrary();
-                               for (MetaData meta : lib.getList(null).filter(sources, authors, tags)) {
+                               for (MetaData meta : lib.getList().filter(sources, authors, tags)) {
                                        bookInfos.add(BookInfo.fromMeta(lib, meta));
                                }
 
@@ -155,14 +157,15 @@ public class BooksPanel extends JPanel {
                        updateBookQueue.clear();
                }
 
-               reload(searchBar.getText());
+               filter(searchBar.getText());
        }
 
        // cannot be NULL
-       private void reload(String filter) {
+       private void filter(String filter) {
                data.clear();
                for (BookInfo bookInfo : bookInfos) {
-                       if (filter.isEmpty() || bookInfo.getMainInfo().toLowerCase().contains(filter.toLowerCase())) {
+                       if (bookInfo.getMainInfo() == null || filter.isEmpty()
+                                       || bookInfo.getMainInfo().toLowerCase().contains(filter.toLowerCase())) {
                                data.addElement(bookInfo);
                        }
                }
@@ -227,6 +230,12 @@ public class BooksPanel extends JPanel {
                                }
                                return null;
                        }
+
+                       @Override
+                       public void invalidateCache() {
+                               // TODO: also reset the popup menu for sources/author
+                               fireActionPerformed(INVALIDATE_CACHE);
+                       }
                });
 
                list.addMouseMotionListener(new MouseAdapter() {