Merge branch 'subtree'
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderGroup.java
index cd98fac58525e7f92b8af0cf8e3a2c09d74b6d8e..cc3f1e15f59794e4c3117b3f9efe3e9d1be90ecd 100644 (file)
@@ -3,6 +3,8 @@ package be.nikiroo.fanfix.reader.ui;
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Component;
+import java.awt.Graphics;
+import java.awt.Rectangle;
 import java.awt.event.ActionListener;
 import java.awt.event.ComponentAdapter;
 import java.awt.event.ComponentEvent;
@@ -29,10 +31,13 @@ public class GuiReaderGroup extends JPanel {
        private static final long serialVersionUID = 1L;
        private BookActionListener action;
        private Color backgroundColor;
+       private Color backgroundColorDef;
+       private Color backgroundColorDefPane;
        private GuiReader reader;
        private List<GuiReaderBookInfo> infos;
        private List<GuiReaderBook> books;
        private JPanel pane;
+       private JLabel titleLabel;
        private boolean words; // words or authors (secondary info on books)
        private int itemsPerLine;
 
@@ -43,21 +48,20 @@ public class GuiReaderGroup extends JPanel {
         *            the {@link GuiReaderBook} used to probe some information about
         *            the stories
         * @param title
-        *            the title of this group
+        *            the title of this group (can be NULL for "no title", an empty
+        *            {@link String} will trigger a default title for empty groups)
         * @param backgroundColor
         *            the background colour to use (or NULL for default)
         */
        public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) {
                this.reader = reader;
-               this.backgroundColor = backgroundColor;
 
                this.pane = new JPanel();
-
                pane.setLayout(new WrapLayout(WrapLayout.LEADING, 5, 5));
-               if (backgroundColor != null) {
-                       pane.setBackground(backgroundColor);
-                       setBackground(backgroundColor);
-               }
+
+               this.backgroundColorDef = getBackground();
+               this.backgroundColorDefPane = pane.getBackground();
+               setBackground(backgroundColor);
 
                setLayout(new BorderLayout(0, 10));
 
@@ -68,18 +72,10 @@ public class GuiReaderGroup extends JPanel {
 
                add(pane, BorderLayout.CENTER);
 
-               if (title != null) {
-                       if (title.isEmpty()) {
-                               title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN);
-                       }
-
-                       JLabel label = new JLabel();
-                       label.setText(String.format("<html>"
-                                       + "<body style='text-align: center; color: gray;'><br><b>"
-                                       + "%s" + "</b></body>" + "</html>", title));
-                       label.setHorizontalAlignment(JLabel.CENTER);
-                       add(label, BorderLayout.NORTH);
-               }
+               titleLabel = new JLabel();
+               titleLabel.setHorizontalAlignment(JLabel.CENTER);
+               add(titleLabel, BorderLayout.NORTH);
+               setTitle(title);
 
                // Compute the number of items per line at each resize
                addComponentListener(new ComponentAdapter() {
@@ -119,13 +115,79 @@ public class GuiReaderGroup extends JPanel {
                });
        }
 
+       /**
+        * Note: this class supports NULL as a background colour, which will revert
+        * it to its default state.
+        * <p>
+        * Note: this class' implementation will also set the main pane background
+        * colour at the same time.
+        * <p>
+        * Sets the background colour of this component. The background colour is
+        * used only if the component is opaque, and only by subclasses of
+        * <code>JComponent</code> or <code>ComponentUI</code> implementations.
+        * Direct subclasses of <code>JComponent</code> must override
+        * <code>paintComponent</code> to honour this property.
+        * <p>
+        * It is up to the look and feel to honour this property, some may choose to
+        * ignore it.
+        * 
+        * @param backgroundColor
+        *            the desired background <code>Colour</code>
+        * @see java.awt.Component#getBackground
+        * @see #setOpaque
+        * 
+        * @beaninfo preferred: true bound: true attribute: visualUpdate true
+        *           description: The background colour of the component.
+        */
+       @Override
+       public void setBackground(Color backgroundColor) {
+               this.backgroundColor = backgroundColor;
+               
+               Color cme = backgroundColor == null ? backgroundColorDef
+                               : backgroundColor;
+               Color cpane = backgroundColor == null ? backgroundColorDefPane
+                               : backgroundColor;
+
+               if (pane != null) { // can happen at theme setup time
+                       pane.setBackground(cpane);
+               }
+               super.setBackground(cme);
+       }
+
+       /**
+        * The title of this group (can be NULL for "no title", an empty
+        * {@link String} will trigger a default title for empty groups)
+        * 
+        * @param title
+        *            the title or NULL
+        */
+       public void setTitle(String title) {
+               if (title != null) {
+                       if (title.isEmpty()) {
+                               title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN);
+                       }
+
+                       titleLabel.setText(String.format("<html>"
+                                       + "<body style='text-align: center; color: gray;'><br><b>"
+                                       + "%s" + "</b></body>" + "</html>", title));
+                       titleLabel.setVisible(true);
+               } else {
+                       titleLabel.setVisible(false);
+               }
+       }
+
        /**
         * Compute how many items can fit in a line so UP and DOWN can be used to go
         * up/down one line at a time.
         */
        private void computeItemsPerLine() {
-               // TODO
-               itemsPerLine = 5;
+               itemsPerLine = 1;
+
+               if (books != null && books.size() > 0) {
+                       // this.pane holds all the books with a hgap of 5 px
+                       int wbook = books.get(0).getWidth() + 5;
+                       itemsPerLine = pane.getWidth() / wbook;
+               }
        }
 
        /**
@@ -137,6 +199,30 @@ public class GuiReaderGroup extends JPanel {
         */
        public void setActionListener(BookActionListener action) {
                this.action = action;
+               refreshBooks();
+       }
+
+       /**
+        * Clear all the books in this {@link GuiReaderGroup}.
+        */
+       public void clear() {
+               refreshBooks(new ArrayList<GuiReaderBookInfo>());
+       }
+
+       /**
+        * Refresh the list of {@link GuiReaderBook}s displayed in the control.
+        */
+       public void refreshBooks() {
+               refreshBooks(infos, words);
+       }
+
+       /**
+        * Refresh the list of {@link GuiReaderBook}s displayed in the control.
+        * 
+        * @param infos
+        *            the new list of infos
+        */
+       public void refreshBooks(List<GuiReaderBookInfo> infos) {
                refreshBooks(infos, words);
        }
 
@@ -172,7 +258,7 @@ public class GuiReaderGroup extends JPanel {
                if (infos != null) {
                        for (GuiReaderBookInfo info : infos) {
                                boolean isCached = false;
-                               if (info.getMeta() != null) {
+                               if (info.getMeta() != null && info.getMeta().getLuid() != null) {
                                        isCached = reader.isCached(info.getMeta().getLuid());
                                }
 
@@ -215,6 +301,8 @@ public class GuiReaderGroup extends JPanel {
                pane.repaint();
                validate();
                repaint();
+
+               computeItemsPerLine();
        }
 
        /**
@@ -242,12 +330,21 @@ public class GuiReaderGroup extends JPanel {
                repaint();
        }
 
+       /**
+        * The number of books in this group.
+        * 
+        * @return the count
+        */
+       public int getBooksCount() {
+               return books.size();
+       }
+
        /**
         * Return the index of the currently selected book if any, -1 if none.
         * 
         * @return the index or -1
         */
-       private int getSelectedBookIndex() {
+       public int getSelectedBookIndex() {
                int index = -1;
                for (int i = 0; i < books.size(); i++) {
                        if (books.get(i).isSelected()) {
@@ -264,12 +361,12 @@ public class GuiReaderGroup extends JPanel {
         * @param index
         *            the index of the book to select, can be outside the bounds
         *            (either all the items will be unselected or the first or last
-        *            book will then be selected, see <tt>forceRange>/tt>)
+        *            book will then be selected, see <tt>forceRange></tt>)
         * @param forceRange
         *            TRUE to constraint the index to the first/last element, FALSE
         *            to unselect when outside the range
         */
-       private void setSelectedBook(int index, boolean forceRange) {
+       public void setSelectedBook(int index, boolean forceRange) {
                int previousIndex = getSelectedBookIndex();
 
                if (index >= books.size()) {
@@ -361,4 +458,19 @@ public class GuiReaderGroup extends JPanel {
                        e.consume();
                }
        }
+
+       @Override
+       public void paint(Graphics g) {
+               super.paint(g);
+
+               Rectangle clip = g.getClipBounds();
+               if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
+                       return;
+               }
+
+               if (!isEnabled()) {
+                       g.setColor(new Color(128, 128, 128, 128));
+                       g.fillRect(clip.x, clip.y, clip.width, clip.height);
+               }
+       }
 }