step 2
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBook.java
index af2a5a783f370a7b0cd52d0c83f134545a6c5bc5..728964baa3faa82676f3b7a6fb6c875b6529bebf 100644 (file)
@@ -68,6 +68,7 @@ class GuiReaderBook extends JPanel {
        private List<BookActionListener> listeners;
        private GuiReaderBookInfo info;
        private boolean cached;
+       private boolean seeWordCount;
 
        /**
         * Create a new {@link GuiReaderBook} item for the given {@link Story}.
@@ -83,22 +84,15 @@ class GuiReaderBook extends JPanel {
         */
        public GuiReaderBook(Reader reader, GuiReaderBookInfo info, boolean cached,
                        boolean seeWordCount) {
-               this.cached = cached;
                this.info = info;
-
-               String optSecondary = info.getSecondaryInfo(seeWordCount);
+               this.cached = cached;
+               this.seeWordCount = seeWordCount;
 
                icon = new JLabel(GuiReaderCoverImager.generateCoverIcon(
                                reader.getLibrary(), info));
-               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, info.getMainInfo(),
-                                               AUTHOR_COLOR, optSecondary));
+
+               title = new JLabel();
+               updateTitle();
 
                setLayout(new BorderLayout(10, 10));
                add(icon, BorderLayout.CENTER);
@@ -118,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
@@ -126,6 +123,10 @@ class GuiReaderBook extends JPanel {
                if (this.selected != selected) {
                        this.selected = selected;
                        repaint();
+
+                       if (selected) {
+                               select();
+                       }
                }
        }
 
@@ -160,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);
                                }
                        }
@@ -194,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();
                                }
                        }
 
@@ -212,6 +212,8 @@ class GuiReaderBook extends JPanel {
                                        listener.select((GuiReaderBook.this));
                                        listener.popupRequested(GuiReaderBook.this, e);
                                }
+
+                               e.consume();
                        }
                });
        }
@@ -226,6 +228,24 @@ class GuiReaderBook extends JPanel {
                listeners.add(listener);
        }
 
+       /**
+        * 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.
         * 
@@ -258,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));
+       }
 }