Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBook.java
index af2a5a783f370a7b0cd52d0c83f134545a6c5bc5..73ccdaa0c1b34427a6c10f64046de8199062d329 100644 (file)
@@ -1,6 +1,7 @@
 package be.nikiroo.fanfix.reader.ui;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.Graphics;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
@@ -17,6 +18,8 @@ import be.nikiroo.fanfix.reader.Reader;
 
 /**
  * A book item presented in a {@link GuiReaderFrame}.
+ * <p>
+ * Can be a story, or a comic or... a group.
  * 
  * @author niki
  */
@@ -48,10 +51,19 @@ class GuiReaderBook extends JPanel {
                 * 
                 * @param book
                 *            the {@link GuiReaderBook} itself
-                * @param e
-                *            the {@link MouseEvent} that generated this call
+                * @param target
+                *            the target component for the popup
+                * @param x
+                *            the X position of the click/request (in case of popup
+                *            request from the keyboard, the center of the target is
+                *            selected as point of reference)
+                * @param y
+                *            the Y position of the click/request (in case of popup
+                *            request from the keyboard, the center of the target is
+                *            selected as point of reference)
                 */
-               public void popupRequested(GuiReaderBook book, MouseEvent e);
+               public void popupRequested(GuiReaderBook book, Component target, int x,
+                               int y);
        }
 
        private static final long serialVersionUID = 1L;
@@ -68,6 +80,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 +96,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 +124,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 +135,10 @@ class GuiReaderBook extends JPanel {
                if (this.selected != selected) {
                        this.selected = selected;
                        repaint();
+
+                       if (selected) {
+                               select();
+                       }
                }
        }
 
@@ -134,7 +147,7 @@ class GuiReaderBook extends JPanel {
         * 
         * @return TRUE if it is mouse-hovered
         */
-       private boolean isHovered() {
+       public boolean isHovered() {
                return this.hovered;
        }
 
@@ -144,7 +157,7 @@ class GuiReaderBook extends JPanel {
         * @param hovered
         *            TRUE if it is mouse-hovered
         */
-       private void setHovered(boolean hovered) {
+       public void setHovered(boolean hovered) {
                if (this.hovered != hovered) {
                        this.hovered = hovered;
                        repaint();
@@ -160,14 +173,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,24 +207,22 @@ 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();
                                }
                        }
 
                        private void popup(MouseEvent e) {
-                               for (BookActionListener listener : listeners) {
-                                       listener.select((GuiReaderBook.this));
-                                       listener.popupRequested(GuiReaderBook.this, e);
-                               }
+                               GuiReaderBook.this
+                                               .popup(GuiReaderBook.this, e.getX(), e.getY());
+                               e.consume();
                        }
                });
        }
@@ -226,6 +237,47 @@ 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}.
+        * <p>
+        * Have a look at {@link GuiReaderBook#setSelected(boolean)}.
+        */
+       private void select() {
+               for (BookActionListener listener : listeners) {
+                       listener.select(GuiReaderBook.this);
+               }
+       }
+
+       /**
+        * Request a popup.
+        * 
+        * @param target
+        *            the target component for the popup
+        * @param x
+        *            the X position of the click/request (in case of popup request
+        *            from the keyboard, the center of the target should be selected
+        *            as point of reference)
+        * @param y
+        *            the Y position of the click/request (in case of popup request
+        *            from the keyboard, the center of the target should be selected
+        *            as point of reference)
+        */
+       public void popup(Component target, int x, int y) {
+               for (BookActionListener listener : listeners) {
+                       listener.select((GuiReaderBook.this));
+                       listener.popupRequested(GuiReaderBook.this, target, x, y);
+               }
+       }
+
        /**
         * The information about the book represented by this item.
         * 
@@ -258,12 +310,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));
+       }
 }