Perf improvement: covers
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderBook.java
index 7dd41b6ca304a9ef586360d5f452c8062b4fd211..0145559cd04d15ed189548cb6ef909e712e25a81 100644 (file)
@@ -9,17 +9,25 @@ import java.awt.Rectangle;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.EventListener;
 import java.util.List;
 
+import javax.imageio.ImageIO;
 import javax.swing.ImageIcon;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
+import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.ui.UIUtils;
 
 /**
@@ -85,44 +93,39 @@ class LocalReaderBook extends JPanel {
        private Date lastClick;
 
        private List<BookActionListener> listeners;
-       private String luid;
+       private MetaData meta;
        private boolean cached;
 
        /**
-        * Create a new {@link LocalReaderBook} item for the givn {@link Story}.
+        * Create a new {@link LocalReaderBook} item for the given {@link Story}.
         * 
         * @param meta
         *            the story {@code}link MetaData}
         * @param cached
         *            TRUE if it is locally cached
+        * @param seeWordcount
+        *            TRUE to see word counts, FALSE to see authors
         */
-       public LocalReaderBook(MetaData meta, boolean cached) {
-               this.luid = meta.getLuid();
+       public LocalReaderBook(MetaData meta, boolean cached, boolean seeWordCount) {
                this.cached = cached;
-
-               BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH
-                               + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
-                               BufferedImage.TYPE_4BYTE_ABGR);
-               Graphics2D g = resizedImage.createGraphics();
-               g.setColor(Color.white);
-               g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
-               if (meta.getCover() != null) {
-                       g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
-                                       null);
-               } else {
-                       g.setColor(Color.black);
-                       g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
-                       g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
+               this.meta = meta;
+
+               String optSecondary = meta.getAuthor();
+               if (seeWordCount) {
+                       if (meta.getWords() >= 4000) {
+                               optSecondary = (meta.getWords() / 1000) + "k words";
+                       } else if (meta.getWords() > 0) {
+                               optSecondary = meta.getWords() + " words";
+                       } else {
+                               optSecondary = "";
+                       }
                }
-               g.dispose();
-
-               icon = new JLabel(new ImageIcon(resizedImage));
 
-               String optAuthor = meta.getAuthor();
-               if (optAuthor != null && !optAuthor.isEmpty()) {
-                       optAuthor = "(" + optAuthor + ")";
+               if (optSecondary != null && !optSecondary.isEmpty()) {
+                       optSecondary = "(" + optSecondary + ")";
                }
 
+               icon = new JLabel(generateCoverIcon(meta));
                title = new JLabel(
                                String.format(
                                                "<html>"
@@ -130,14 +133,13 @@ class LocalReaderBook extends JPanel {
                                                                + "%s" + "<br>" + "<span style='color: %s;'>"
                                                                + "%s" + "</span>" + "</body>" + "</html>",
                                                TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
-                                               optAuthor));
+                                               optSecondary));
 
-               this.setLayout(new BorderLayout(10, 10));
-               this.add(icon, BorderLayout.CENTER);
-               this.add(title, BorderLayout.SOUTH);
+               setLayout(new BorderLayout(10, 10));
+               add(icon, BorderLayout.CENTER);
+               add(title, BorderLayout.SOUTH);
 
                setupListeners();
-               setSelected(false);
        }
 
        /**
@@ -156,8 +158,10 @@ class LocalReaderBook extends JPanel {
         *            TRUE if it is selected
         */
        public void setSelected(boolean selected) {
-               this.selected = selected;
-               repaint();
+               if (this.selected != selected) {
+                       this.selected = selected;
+                       repaint();
+               }
        }
 
        /**
@@ -167,8 +171,10 @@ class LocalReaderBook extends JPanel {
         *            TRUE if it is mouse-hovered
         */
        private void setHovered(boolean hovered) {
-               this.hovered = hovered;
-               repaint();
+               if (this.hovered != hovered) {
+                       this.hovered = hovered;
+                       repaint();
+               }
        }
 
        /**
@@ -242,12 +248,12 @@ class LocalReaderBook extends JPanel {
        }
 
        /**
-        * The Library UID of the book represented by this item.
+        * The Library {@code}link MetaData} of the book represented by this item.
         * 
-        * @return the LUID
+        * @return the meta
         */
-       public String getLuid() {
-               return luid;
+       public MetaData getMeta() {
+               return meta;
        }
 
        /**
@@ -266,18 +272,27 @@ class LocalReaderBook extends JPanel {
         *            TRUE if it is present in the {@link LocalReader} cache
         */
        public void setCached(boolean cached) {
-               this.cached = cached;
+               if (this.cached != cached) {
+                       this.cached = cached;
+                       repaint();
+               }
        }
 
        /**
-        * Draw a "cached" icon and a partially transparent overlay if needed
-        * depending upon the selection and mouse-hover states on top of the normal
-        * component.
+        * Paint the item, then call {@link LocalReaderBook#paintOverlay(Graphics)}.
         */
        @Override
        public void paint(Graphics g) {
                super.paint(g);
+               paintOverlay(g);
+       }
 
+       /**
+        * Draw a partially transparent overlay if needed depending upon the
+        * selection and mouse-hover states on top of the normal component, as well
+        * as a possible "cached" icon if the item is cached.
+        */
+       public void paintOverlay(Graphics g) {
                Rectangle clip = g.getClipBounds();
                if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
                        return;
@@ -329,4 +344,67 @@ class LocalReaderBook extends JPanel {
                                        + HOFFSET + 30, 10, 20, 20);
                }
        }
+
+       /**
+        * Generate a cover icon based upon the given {@link MetaData}.
+        * 
+        * @param meta
+        *            the {@link MetaData} about the target {@link Story}
+        * 
+        * @return the icon
+        */
+       private ImageIcon generateCoverIcon(MetaData meta) {
+               String id = meta.getUuid() + ".thumb_" + SPINE_WIDTH + "x"
+                               + COVER_WIDTH + "+" + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@"
+                               + HOFFSET;
+               BufferedImage resizedImage = null;
+
+               InputStream in = Instance.getCache().getFromCache(id);
+               if (in != null) {
+                       try {
+                               resizedImage = IOUtils.toImage(in);
+                               in.close();
+                               in = null;
+                       } catch (IOException e) {
+                               Instance.syserr(e);
+                       }
+               }
+
+               if (resizedImage == null) {
+                       try {
+                               BufferedImage cover = Instance.getLibrary().getCover(
+                                               meta.getLuid());
+
+                               resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH,
+                                               SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
+                                               BufferedImage.TYPE_4BYTE_ABGR);
+                               Graphics2D g = resizedImage.createGraphics();
+                               g.setColor(Color.white);
+                               g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
+                               if (cover != null) {
+                                       g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
+                                                       null);
+                               } else {
+                                       g.setColor(Color.black);
+                                       g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
+                                       g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
+                               }
+                               g.dispose();
+
+                               ByteArrayOutputStream out = new ByteArrayOutputStream();
+                               ImageIO.write(resizedImage, "png", out);
+                               byte[] imageBytes = out.toByteArray();
+                               in = new ByteArrayInputStream(imageBytes);
+                               Instance.getCache().addToCache(in, id);
+                               in.close();
+                               in = null;
+                       } catch (MalformedURLException e) {
+                               Instance.syserr(e);
+                       } catch (IOException e) {
+                               Instance.syserr(e);
+                       }
+               }
+
+               return new ImageIcon(resizedImage);
+       }
 }