Version 1.2.4: fixes, new "Re-download" UI option
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderBook.java
index 7dd41b6ca304a9ef586360d5f452c8062b4fd211..9a4290de201912ca0e12f6b0357e1ced2cbc1176 100644 (file)
@@ -85,11 +85,11 @@ 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}
@@ -97,32 +97,16 @@ class LocalReaderBook extends JPanel {
         *            TRUE if it is locally cached
         */
        public LocalReaderBook(MetaData meta, boolean cached) {
-               this.luid = meta.getLuid();
                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);
-               }
-               g.dispose();
-
-               icon = new JLabel(new ImageIcon(resizedImage));
+               this.meta = meta;
 
                String optAuthor = meta.getAuthor();
                if (optAuthor != null && !optAuthor.isEmpty()) {
                        optAuthor = "(" + optAuthor + ")";
                }
 
+               icon = new JLabel(generateCoverIcon(meta.getCover()));
+
                title = new JLabel(
                                String.format(
                                                "<html>"
@@ -132,12 +116,11 @@ class LocalReaderBook extends JPanel {
                                                TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
                                                optAuthor));
 
-               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 +139,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 +152,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 +229,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 +253,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 +325,32 @@ class LocalReaderBook extends JPanel {
                                        + HOFFSET + 30, 10, 20, 20);
                }
        }
+
+       /**
+        * Generate a cover icon based upon the given cover image (which may be
+        * NULL).
+        * 
+        * @param image
+        *            the cover image, or NULL for none
+        * 
+        * @return the icon
+        */
+       private ImageIcon generateCoverIcon(BufferedImage image) {
+               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 (image != null) {
+                       g.drawImage(image, 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();
+
+               return new ImageIcon(resizedImage);
+       }
 }