much quicker BooksPanel display, set max title size for BookBlocks
[nikiroo-utils.git] / src / be / nikiroo / fanfix_swing / gui / BooksPanel.java
index 810323f6b76b3494bcefb7136ade645bed48c835..20e28af116f4aba190ad0e643eacc47019ef4812 100644 (file)
@@ -2,6 +2,7 @@ package be.nikiroo.fanfix_swing.gui;
 
 import java.awt.BorderLayout;
 import java.awt.Component;
+import java.awt.Dimension;
 import java.awt.Image;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
@@ -10,10 +11,8 @@ import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Queue;
 import java.util.concurrent.ExecutionException;
 
 import javax.swing.DefaultListModel;
@@ -21,7 +20,6 @@ import javax.swing.JList;
 import javax.swing.JPopupMenu;
 import javax.swing.ListCellRenderer;
 import javax.swing.ListSelectionModel;
-import javax.swing.SwingUtilities;
 import javax.swing.SwingWorker;
 
 import be.nikiroo.fanfix.Instance;
@@ -32,6 +30,7 @@ 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.DelayWorker;
 import be.nikiroo.fanfix_swing.gui.utils.ListenerPanel;
 import be.nikiroo.fanfix_swing.gui.utils.UiHelper;
 
@@ -55,12 +54,10 @@ public class BooksPanel extends ListenerPanel {
        private JList<BookInfo> list;
        private int hoveredIndex = -1;
        private ListModel data = new ListModel();
+       private DelayWorker bookCoverUpdater;
 
        private SearchBar searchBar;
 
-       private Queue<BookBlock> updateBookQueue = new LinkedList<BookBlock>();
-       private Object updateBookQueueLock = new Object();
-
        public BooksPanel(boolean listMode) {
                setLayout(new BorderLayout());
 
@@ -74,51 +71,9 @@ public class BooksPanel extends ListenerPanel {
                        }
                });
 
+               bookCoverUpdater = new DelayWorker(20);
+               bookCoverUpdater.start();
                add(UiHelper.scroll(initList(listMode)), BorderLayout.CENTER);
-
-               Thread bookBlocksUpdater = new Thread(new Runnable() {
-                       @Override
-                       public void run() {
-                               while (true) {
-                                       BasicLibrary lib = Instance.getInstance().getLibrary();
-                                       while (true) {
-                                               final BookBlock book;
-                                               synchronized (updateBookQueueLock) {
-                                                       if (!updateBookQueue.isEmpty()) {
-                                                               book = updateBookQueue.remove();
-                                                       } else {
-                                                               book = null;
-                                                               break;
-                                                       }
-                                               }
-
-                                               try {
-                                                       final Image coverImage = BookBlock
-                                                                       .generateCoverImage(lib, book.getInfo());
-                                                       SwingUtilities.invokeLater(new Runnable() {
-                                                               @Override
-                                                               public void run() {
-                                                                       try {
-                                                                               book.setCoverImage(coverImage);
-                                                                               data.fireElementChanged(book.getInfo());
-                                                                       } catch (Exception e) {
-                                                                       }
-                                                               }
-                                                       });
-                                               } catch (Exception e) {
-                                               }
-                                       }
-
-                                       try {
-                                               Thread.sleep(10);
-                                       } catch (InterruptedException e) {
-                                       }
-                               }
-                       }
-               });
-               bookBlocksUpdater.setName("BookBlocks visual updater");
-               bookBlocksUpdater.setDaemon(true);
-               bookBlocksUpdater.start();
        }
 
        // null or empty -> all sources
@@ -156,9 +111,7 @@ public class BooksPanel extends ListenerPanel {
        public void load(List<BookInfo> bookInfos) {
                this.bookInfos.clear();
                this.bookInfos.addAll(bookInfos);
-               synchronized (updateBookQueueLock) {
-                       updateBookQueue.clear();
-               }
+               bookCoverUpdater.clear();
 
                filter(searchBar.getText());
        }
@@ -335,9 +288,7 @@ public class BooksPanel extends ListenerPanel {
                                                book = new BookLine(value, seeWordCount);
                                        } else {
                                                book = new BookBlock(value, seeWordCount);
-                                               synchronized (updateBookQueueLock) {
-                                                       updateBookQueue.add((BookBlock) book);
-                                               }
+                                               startUpdateBookCover((BookBlock) book);
                                        }
                                        books.put(value, book);
                                }
@@ -349,6 +300,27 @@ public class BooksPanel extends ListenerPanel {
                };
        }
 
+       private void startUpdateBookCover(final BookBlock book) {
+               bookCoverUpdater.delay(book.getInfo().getId(),
+                               new SwingWorker<Image, Void>() {
+                                       @Override
+                                       protected Image doInBackground() throws Exception {
+                                               BasicLibrary lib = Instance.getInstance().getLibrary();
+                                               return BookBlock.generateCoverImage(lib,
+                                                               book.getInfo());
+                                       }
+
+                                       protected void done() {
+                                               try {
+                                                       book.setCoverImage(get());
+                                                       data.fireElementChanged(book.getInfo());
+                                               } catch (Exception e) {
+                                                       // TODO ? probably just log
+                                               }
+                                       }
+                               });
+       }
+
        public boolean isListMode() {
                return listMode;
        }
@@ -359,10 +331,24 @@ public class BooksPanel extends ListenerPanel {
                list.setLayoutOrientation(
                                listMode ? JList.VERTICAL : JList.HORIZONTAL_WRAP);
 
+               StringBuilder longString = new StringBuilder();
+               for (int i = 0; i < 20; i++) {
+                       longString.append(
+                                       "Some long string, which is 50 chars long itself...");
+               }
                if (listMode) {
-                       synchronized (updateBookQueueLock) {
-                               updateBookQueue.clear();
-                       }
+                       bookCoverUpdater.clear();
+                       Dimension sz = new BookLine(
+                                       BookInfo.fromSource(null, longString.toString()), true)
+                                                       .getPreferredSize();
+                       list.setFixedCellHeight((int) sz.getHeight());
+                       list.setFixedCellWidth(list.getWidth());
+               } else {
+                       Dimension sz = new BookBlock(
+                                       BookInfo.fromSource(null, longString.toString()), true)
+                                                       .getPreferredSize();
+                       list.setFixedCellHeight((int) sz.getHeight());
+                       list.setFixedCellWidth((int) sz.getWidth());
                }
        }
 }