GUI search: waiting
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderGroup.java
index cd98fac58525e7f92b8af0cf8e3a2c09d74b6d8e..df3b74a0bdaaab9d9da355dd4943f7ba0e94f02f 100644 (file)
@@ -29,10 +29,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 +46,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 +70,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,6 +113,65 @@ public class GuiReaderGroup extends JPanel {
                });
        }
 
+       /**
+        * Note: this class supports NULL as a background color, which will revert
+        * it to its default state.
+        * <p>
+        * Note: this class' implementation will also set the main pane background
+        * color at the same time.
+        * <p>
+        * Sets the background color of this component. The background color 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 honor this property.
+        * <p>
+        * It is up to the look and feel to honor this property, some may choose to
+        * ignore it.
+        * 
+        * @param bg
+        *            the desired background <code>Color</code>
+        * @see java.awt.Component#getBackground
+        * @see #setOpaque
+        * 
+        * @beaninfo preferred: true bound: true attribute: visualUpdate true
+        *           description: The background color of the component.
+        */
+       @Override
+       public void setBackground(Color 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.
@@ -172,7 +225,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());
                                }
 
@@ -242,12 +295,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()) {
@@ -269,7 +331,7 @@ public class GuiReaderGroup extends JPanel {
         *            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()) {