step 2
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBook.java
index 4683f71507ccaf20b6cbfd76c454640e38368a8e..728964baa3faa82676f3b7a6fb6c875b6529bebf 100644 (file)
@@ -12,7 +12,6 @@ import java.util.List;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
-import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.reader.Reader;
 
@@ -67,62 +66,33 @@ class GuiReaderBook extends JPanel {
        private Date lastClick;
 
        private List<BookActionListener> listeners;
-       private MetaData meta;
+       private GuiReaderBookInfo info;
        private boolean cached;
+       private boolean seeWordCount;
 
        /**
         * Create a new {@link GuiReaderBook} item for the given {@link Story}.
         * 
         * @param reader
         *            the associated reader
-        * @param meta
-        *            the story {@link MetaData} or source (if no LUID)
+        * @param info
+        *            the information about the story to represent
         * @param cached
         *            TRUE if it is locally cached
         * @param seeWordCount
         *            TRUE to see word counts, FALSE to see authors
         */
-       public GuiReaderBook(Reader reader, MetaData meta, boolean cached,
+       public GuiReaderBook(Reader reader, GuiReaderBookInfo info, boolean cached,
                        boolean seeWordCount) {
+               this.info = info;
                this.cached = cached;
-               this.meta = meta;
-
-               String optSecondary = meta.getAuthor();
-               if (seeWordCount) {
-                       if (meta.getWords() >= 4000) {
-                               optSecondary = "" + (meta.getWords() / 1000) + "k";
-                       } else if (meta.getWords() > 0) {
-                               optSecondary = "" + meta.getWords();
-                       } else {
-                               optSecondary = "";
-                       }
-
-                       if (!optSecondary.isEmpty()) {
-                               if (meta.isImageDocument()) {
-                                       optSecondary += " images";
-                               } else {
-                                       optSecondary += " words";
-                               }
-                       }
-               }
-
-               if (optSecondary != null && !optSecondary.isEmpty()) {
-                       optSecondary = "(" + optSecondary + ")";
-               } else {
-                       optSecondary = "";
-               }
+               this.seeWordCount = seeWordCount;
 
                icon = new JLabel(GuiReaderCoverImager.generateCoverIcon(
-                               reader.getLibrary(), getMeta()));
-               title = new JLabel(
-                               String.format(
-                                               "<html>"
-                                                               + "<body style='width: %d px; height: %d px; text-align: center'>"
-                                                               + "%s" + "<br>" + "<span style='color: %s;'>"
-                                                               + "%s" + "</span>" + "</body>" + "</html>",
-                                               GuiReaderCoverImager.TEXT_WIDTH,
-                                               GuiReaderCoverImager.TEXT_HEIGHT, meta.getTitle(),
-                                               AUTHOR_COLOR, optSecondary));
+                               reader.getLibrary(), info));
+
+               title = new JLabel();
+               updateTitle();
 
                setLayout(new BorderLayout(10, 10));
                add(icon, BorderLayout.CENTER);
@@ -142,6 +112,9 @@ class GuiReaderBook extends JPanel {
 
        /**
         * The book current selection state.
+        * <p>
+        * Setting this value to true can cause a "select" action to occur if the
+        * previous state was "unselected".
         * 
         * @param selected
         *            TRUE if it is selected
@@ -150,6 +123,10 @@ class GuiReaderBook extends JPanel {
                if (this.selected != selected) {
                        this.selected = selected;
                        repaint();
+
+                       if (selected) {
+                               select();
+                       }
                }
        }
 
@@ -184,14 +161,14 @@ class GuiReaderBook extends JPanel {
                addMouseListener(new MouseListener() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
-                               if (e.isPopupTrigger()) {
+                               if (isEnabled() && e.isPopupTrigger()) {
                                        popup(e);
                                }
                        }
 
                        @Override
                        public void mousePressed(MouseEvent e) {
-                               if (e.isPopupTrigger()) {
+                               if (isEnabled() && e.isPopupTrigger()) {
                                        popup(e);
                                }
                        }
@@ -218,16 +195,15 @@ class GuiReaderBook extends JPanel {
                                        }
 
                                        lastClick = now;
+                                       e.consume();
                                }
                        }
 
                        private void click(boolean doubleClick) {
-                               for (BookActionListener listener : listeners) {
-                                       if (doubleClick) {
-                                               listener.action(GuiReaderBook.this);
-                                       } else {
-                                               listener.select(GuiReaderBook.this);
-                                       }
+                               if (doubleClick) {
+                                       action();
+                               } else {
+                                       select();
                                }
                        }
 
@@ -236,6 +212,8 @@ class GuiReaderBook extends JPanel {
                                        listener.select((GuiReaderBook.this));
                                        listener.popupRequested(GuiReaderBook.this, e);
                                }
+
+                               e.consume();
                        }
                });
        }
@@ -251,12 +229,30 @@ class GuiReaderBook extends JPanel {
        }
 
        /**
-        * The Library {@link MetaData} of the book represented by this item.
+        * Cause an action to occur on this {@link GuiReaderBook}.
+        */
+       public void action() {
+               for (BookActionListener listener : listeners) {
+                       listener.action(GuiReaderBook.this);
+               }
+       }
+
+       /**
+        * Cause a select event on this {@link GuiReaderBook}.
+        */
+       private void select() {
+               for (BookActionListener listener : listeners) {
+                       listener.select(GuiReaderBook.this);
+               }
+       }
+
+       /**
+        * The information about the book represented by this item.
         * 
         * @return the meta
         */
-       public MetaData getMeta() {
-               return meta;
+       public GuiReaderBookInfo getInfo() {
+               return info;
        }
 
        /**
@@ -282,12 +278,30 @@ class GuiReaderBook extends JPanel {
        }
 
        /**
-        * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
+        * Update the title, paint the item, then call
+        * {@link GuiReaderCoverImager#paintOverlay(Graphics, boolean, boolean, boolean, boolean)}
+        * .
         */
        @Override
        public void paint(Graphics g) {
+               updateTitle();
                super.paint(g);
                GuiReaderCoverImager.paintOverlay(g, isEnabled(), isSelected(),
                                isHovered(), isCached());
        }
+
+       /**
+        * Update the title with the currently registered information.
+        */
+       private void updateTitle() {
+               String optSecondary = info.getSecondaryInfo(seeWordCount);
+               title.setText(String
+                               .format("<html>"
+                                               + "<body style='width: %d px; height: %d px; text-align: center'>"
+                                               + "%s" + "<br>" + "<span style='color: %s;'>" + "%s"
+                                               + "</span>" + "</body>" + "</html>",
+                                               GuiReaderCoverImager.TEXT_WIDTH,
+                                               GuiReaderCoverImager.TEXT_HEIGHT, info.getMainInfo(),
+                                               AUTHOR_COLOR, optSecondary));
+       }
 }