X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FGuiReaderBook.java;h=2ae04b461d13bdd43cac911e0702190439d12cca;hb=e604986c4208da0091d26bc0e1c4feb4ff3c588f;hp=e6fe91b4d84ed95def7fe13ba48b52850ceb9686;hpb=5dd985cf7d5e2bb88b07fd43e7b4a4eda4647181;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderBook.java b/src/be/nikiroo/fanfix/reader/GuiReaderBook.java index e6fe91b..2ae04b4 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReaderBook.java +++ b/src/be/nikiroo/fanfix/reader/GuiReaderBook.java @@ -27,7 +27,7 @@ import javax.swing.JPanel; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.utils.IOUtils; +import be.nikiroo.utils.ImageUtils; import be.nikiroo.utils.ui.UIUtils; /** @@ -93,36 +93,51 @@ class GuiReaderBook extends JPanel { private Date lastClick; private List listeners; + private Reader reader; private MetaData meta; private boolean cached; /** * Create a new {@link GuiReaderBook} item for the given {@link Story}. * + * @param reader + * the associated reader * @param meta - * the story {@link MetaData} + * the story {@link MetaData} or source (if no LUID) * @param cached * TRUE if it is locally cached * @param seeWordCount * TRUE to see word counts, FALSE to see authors */ - public GuiReaderBook(MetaData meta, boolean cached, boolean seeWordCount) { + public GuiReaderBook(Reader reader, MetaData meta, boolean cached, + boolean seeWordCount) { + this.reader = reader; this.cached = cached; this.meta = meta; String optSecondary = meta.getAuthor(); if (seeWordCount) { if (meta.getWords() >= 4000) { - optSecondary = (meta.getWords() / 1000) + "k words"; + optSecondary = "" + (meta.getWords() / 1000) + "k"; } else if (meta.getWords() > 0) { - optSecondary = meta.getWords() + " words"; + optSecondary = "" + meta.getWords(); } else { optSecondary = ""; } + + if (!optSecondary.isEmpty()) { + if (meta.isImageDocument()) { + optSecondary += " images"; + } else { + optSecondary += " words"; + } + } } if (optSecondary != null && !optSecondary.isEmpty()) { optSecondary = "(" + optSecondary + ")"; + } else { + optSecondary = ""; } icon = new JLabel(generateCoverIcon(meta)); @@ -184,26 +199,31 @@ class GuiReaderBook extends JPanel { private void setupListeners() { listeners = new ArrayList(); addMouseListener(new MouseListener() { + @Override public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { popup(e); } } + @Override public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) { popup(e); } } + @Override public void mouseExited(MouseEvent e) { setHovered(false); } + @Override public void mouseEntered(MouseEvent e) { setHovered(true); } + @Override public void mouseClicked(MouseEvent e) { if (isEnabled()) { Date now = new Date(); @@ -352,20 +372,27 @@ class GuiReaderBook extends JPanel { * Generate a cover icon based upon the given {@link MetaData}. * * @param meta - * the {@link MetaData} about the target {@link Story} + * the {@link MetaData} about the target {@link Story} or source + * (if no LUID) * * @return the icon */ private ImageIcon generateCoverIcon(MetaData meta) { - String id = meta.getUuid() + ".thumb_" + SPINE_WIDTH + "x" - + COVER_WIDTH + "+" + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" - + HOFFSET; BufferedImage resizedImage = null; + String id = null; + + String key = meta.getUuid(); + if (key == null) { + // a fake meta (a source) + key = "source_" + meta.getSource(); + } + id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+" + + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET; InputStream in = Instance.getCache().getFromCache(id); if (in != null) { try { - resizedImage = IOUtils.toImage(in); + resizedImage = ImageUtils.fromStream(in); in.close(); in = null; } catch (IOException e) { @@ -375,8 +402,13 @@ class GuiReaderBook extends JPanel { if (resizedImage == null) { try { - BufferedImage cover = Instance.getLibrary().getCover( - meta.getLuid()); + BufferedImage cover = null; + if (meta.getLuid() == null) { + cover = reader.getLibrary() + .getSourceCover(meta.getSource()); + } else { + cover = reader.getLibrary().getCover(meta.getLuid()); + } resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, @@ -394,13 +426,15 @@ class GuiReaderBook extends JPanel { } g.dispose(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ImageIO.write(resizedImage, "png", out); - byte[] imageBytes = out.toByteArray(); - in = new ByteArrayInputStream(imageBytes); - Instance.getCache().addToCache(in, id); - in.close(); - in = null; + if (id != null) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ImageIO.write(resizedImage, "png", out); + byte[] imageBytes = out.toByteArray(); + in = new ByteArrayInputStream(imageBytes); + Instance.getCache().addToCache(in, id); + in.close(); + in = null; + } } catch (MalformedURLException e) { Instance.syserr(e); } catch (IOException e) {