much quicker BooksPanel display, set max title size for BookBlocks
authorNiki Roo <niki@nikiroo.be>
Thu, 9 Apr 2020 20:44:27 +0000 (22:44 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 9 Apr 2020 20:44:27 +0000 (22:44 +0200)
src/be/nikiroo/fanfix_swing/gui/BooksPanel.java
src/be/nikiroo/fanfix_swing/gui/MainFrame.java
src/be/nikiroo/fanfix_swing/gui/book/BookBlock.java
src/be/nikiroo/fanfix_swing/gui/book/BookInfo.java
src/be/nikiroo/fanfix_swing/gui/book/BookLine.java

index 3a07afb77c33af5bd53ad829572f5960431ff756..20e28af116f4aba190ad0e643eacc47019ef4812 100644 (file)
@@ -2,6 +2,7 @@ package be.nikiroo.fanfix_swing.gui;
 
 import java.awt.BorderLayout;
 import java.awt.Component;
+import java.awt.Dimension;
 import java.awt.Image;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
@@ -330,8 +331,24 @@ public class BooksPanel extends ListenerPanel {
                list.setLayoutOrientation(
                                listMode ? JList.VERTICAL : JList.HORIZONTAL_WRAP);
 
+               StringBuilder longString = new StringBuilder();
+               for (int i = 0; i < 20; i++) {
+                       longString.append(
+                                       "Some long string, which is 50 chars long itself...");
+               }
                if (listMode) {
                        bookCoverUpdater.clear();
+                       Dimension sz = new BookLine(
+                                       BookInfo.fromSource(null, longString.toString()), true)
+                                                       .getPreferredSize();
+                       list.setFixedCellHeight((int) sz.getHeight());
+                       list.setFixedCellWidth(list.getWidth());
+               } else {
+                       Dimension sz = new BookBlock(
+                                       BookInfo.fromSource(null, longString.toString()), true)
+                                                       .getPreferredSize();
+                       list.setFixedCellHeight((int) sz.getHeight());
+                       list.setFixedCellWidth((int) sz.getWidth());
                }
        }
 }
index 9d25169c3e6276199324ed6423f1cecab101d9c2..d580c61c788d380b69f1af000d1e25f11c893fc9 100644 (file)
@@ -29,6 +29,7 @@ public class MainFrame extends JFrame {
                detailsPanel = true;
 
                browser = new BrowserPanel();
+               books = new BooksPanel(true);
 
                JComponent other = null;
                boolean orientationH = true;
@@ -47,8 +48,6 @@ public class MainFrame extends JFrame {
                        other = split(goBack, details, false, 0.5, 1);
                }
 
-               books = new BooksPanel(true);
-
                browser.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
index 595f42ba7051d65ac80df1d906d20ad2d96bc064..329c714e200c8f3b04aac2b2736a5e9b76867c0c 100644 (file)
@@ -78,8 +78,8 @@ public class BookBlock extends BookLine {
 
        @Override
        protected void updateMeta() {
-               String main = getInfo().getMainInfo();
-               String optSecondary = getInfo().getSecondaryInfo(isSeeWordCount());
+               String main = getMainInfoDisplay();
+               String optSecondary = getSecondaryInfoDisplay(isSeeWordCount());
                String color = String.format("#%X%X%X", AUTHOR_COLOR.getRed(),
                                AUTHOR_COLOR.getGreen(), AUTHOR_COLOR.getBlue());
                title.setText(String.format("<html>"
index bde9abbc34b547f4598f88bf4eb77a090c453cf2..713ee203564f8ab90af62335e178faebb43fde1b 100644 (file)
@@ -261,9 +261,11 @@ public class BookInfo {
                                "source_" + (source == null ? "" : source), source);
 
                int size = 0;
-               try {
-                       size = lib.getList().filter(source, null, null).size();
-               } catch (IOException e) {
+               if (lib != null) {
+                       try {
+                               size = lib.getList().filter(source, null, null).size();
+                       } catch (IOException e) {
+                       }
                }
 
                info.count = StringUtils.formatNumber(size);
@@ -292,9 +294,11 @@ public class BookInfo {
                                "author_" + (author == null ? "" : author), author);
 
                int size = 0;
-               try {
-                       size = lib.getList().filter(null, author, null).size();
-               } catch (IOException e) {
+               if (lib != null) {
+                       try {
+                               size = lib.getList().filter(null, author, null).size();
+                       } catch (IOException e) {
+                       }
                }
 
                info.count = StringUtils.formatNumber(size);
index f7926a0212d3dd36db60bf13c9c61b6e0c6308b3..d601dd6c728cd47e540ced17eaeb4d0cfe53298c 100644 (file)
@@ -21,6 +21,8 @@ import be.nikiroo.fanfix_swing.gui.BooksPanel;
 public class BookLine extends JPanel {
        private static final long serialVersionUID = 1L;
 
+       private static final int MAX_DISPLAY_SIZE = 40;
+
        /** Colour used for the seconday item (author/word count). */
        protected static final Color AUTHOR_COLOR = new Color(128, 128, 128);
 
@@ -65,7 +67,11 @@ public class BookLine extends JPanel {
                secondary = new JLabel();
                secondary.setForeground(AUTHOR_COLOR);
 
-               JLabel id = new JLabel(info.getMeta().getLuid());
+               String luid = null;
+               if (info.getMeta() != null) {
+                       luid = info.getMeta().getLuid();
+               }
+               JLabel id = new JLabel(luid);
                id.setPreferredSize(new JLabel(" 999 ").getPreferredSize());
                id.setForeground(Color.gray);
                id.setHorizontalAlignment(SwingConstants.CENTER);
@@ -166,14 +172,35 @@ public class BookLine extends JPanel {
                super.paint(g);
        }
 
+       /**
+        * Return a display-ready version of {@link BookInfo#getMainInfo()}.
+        * 
+        * @return the main info in a ready-to-display version
+        */
+       protected String getMainInfoDisplay() {
+               return toDisplay(getInfo().getMainInfo());
+       }
+
+       /**
+        * Return a display-ready version of
+        * {@link BookInfo#getSecondaryInfo(boolean)}.
+        * 
+        * @param seeCount
+        *            TRUE for word/image/story count, FALSE for author name
+        * 
+        * @return the main info in a ready-to-display version
+        */
+       protected String getSecondaryInfoDisplay(boolean seeCount) {
+               return toDisplay(getInfo().getSecondaryInfo(seeCount));
+       }
+
        /**
         * Update the title with the currently registered information.
         */
        protected void updateMeta() {
-               String main = info.getMainInfo();
-               String optSecondary = info.getSecondaryInfo(seeWordCount);
+               String main = getMainInfoDisplay();
+               String optSecondary = getSecondaryInfoDisplay(isSeeWordCount());
 
-               // TODO: max size limit?
                title.setText(main);
                secondary.setText(optSecondary + " ");
 
@@ -186,4 +213,24 @@ public class BookLine extends JPanel {
                                BorderLayout.WEST);
                validate();
        }
+
+       /**
+        * Make the given {@link String} display-ready (i.e., shorten it if it is
+        * too long).
+        * 
+        * @param value
+        *            the full value
+        * 
+        * @return the display-ready value
+        */
+       private String toDisplay(String value) {
+               if (value == null)
+                       value = "";
+
+               if (value.length() > MAX_DISPLAY_SIZE) {
+                       value = value.substring(0, MAX_DISPLAY_SIZE - 3) + "...";
+               }
+
+               return value;
+       }
 }