From: Niki Roo Date: Thu, 9 Apr 2020 20:44:27 +0000 (+0200) Subject: much quicker BooksPanel display, set max title size for BookBlocks X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=89c5b3e28f9f73777541157adf4e4b4fe25ad50d;p=fanfix-jexer.git much quicker BooksPanel display, set max title size for BookBlocks --- diff --git a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java index 3a07afb..20e28af 100644 --- a/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java +++ b/src/be/nikiroo/fanfix_swing/gui/BooksPanel.java @@ -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()); } } } diff --git a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java index 9d25169..d580c61 100644 --- a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java @@ -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) { diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookBlock.java b/src/be/nikiroo/fanfix_swing/gui/book/BookBlock.java index 595f42b..329c714 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookBlock.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookBlock.java @@ -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("" diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookInfo.java b/src/be/nikiroo/fanfix_swing/gui/book/BookInfo.java index bde9abb..713ee20 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookInfo.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookInfo.java @@ -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); diff --git a/src/be/nikiroo/fanfix_swing/gui/book/BookLine.java b/src/be/nikiroo/fanfix_swing/gui/book/BookLine.java index f7926a0..d601dd6 100644 --- a/src/be/nikiroo/fanfix_swing/gui/book/BookLine.java +++ b/src/be/nikiroo/fanfix_swing/gui/book/BookLine.java @@ -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; + } }