import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.EventListener;
import java.util.List;
+import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
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.ui.UIUtils;
/**
private Date lastClick;
private List<BookActionListener> listeners;
- private String luid;
+ private MetaData meta;
private boolean cached;
/**
* the story {@code}link MetaData}
* @param cached
* TRUE if it is locally cached
+ * @param seeWordcount
+ * TRUE to see word counts, FALSE to see authors
*/
- public LocalReaderBook(MetaData meta, boolean cached) {
+ public LocalReaderBook(MetaData meta, boolean cached, boolean seeWordCount) {
this.cached = cached;
- luid = meta.getLuid();
-
- String optAuthor = meta.getAuthor();
- if (optAuthor != null && !optAuthor.isEmpty()) {
- optAuthor = "(" + optAuthor + ")";
+ this.meta = meta;
+
+ String optSecondary = meta.getAuthor();
+ if (seeWordCount) {
+ if (meta.getWords() >= 4000) {
+ optSecondary = (meta.getWords() / 1000) + "k words";
+ } else if (meta.getWords() > 0) {
+ optSecondary = meta.getWords() + " words";
+ } else {
+ optSecondary = "";
+ }
}
- icon = new JLabel(generateCoverIcon(meta.getCover()));
+ if (optSecondary != null && !optSecondary.isEmpty()) {
+ optSecondary = "(" + optSecondary + ")";
+ }
+ icon = new JLabel(generateCoverIcon(meta));
title = new JLabel(
String.format(
"<html>"
+ "%s" + "<br>" + "<span style='color: %s;'>"
+ "%s" + "</span>" + "</body>" + "</html>",
TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
- optAuthor));
+ optSecondary));
setLayout(new BorderLayout(10, 10));
add(icon, BorderLayout.CENTER);
}
/**
- * The Library UID of the book represented by this item.
+ * The Library {@code}link MetaData} of the book represented by this item.
*
- * @return the LUID
+ * @return the meta
*/
- public String getLuid() {
- return luid;
+ public MetaData getMeta() {
+ return meta;
}
/**
}
/**
- * Generate a cover icon based upon the given cover image (which may be
- * NULL).
+ * Generate a cover icon based upon the given {@link MetaData}.
*
- * @param image
- * the cover image, or NULL for none
+ * @param meta
+ * the {@link MetaData} about the target {@link Story}
*
* @return the icon
*/
- private ImageIcon generateCoverIcon(BufferedImage image) {
- BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH
- + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
- BufferedImage.TYPE_4BYTE_ABGR);
- Graphics2D g = resizedImage.createGraphics();
- g.setColor(Color.white);
- g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
- if (image != null) {
- g.drawImage(image, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, null);
- } else {
- g.setColor(Color.black);
- g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
- g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
+ private ImageIcon generateCoverIcon(MetaData meta) {
+ String id = meta.getUuid() + ".thumb_" + SPINE_WIDTH + "x"
+ + COVER_WIDTH + "+" + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@"
+ + HOFFSET;
+ BufferedImage resizedImage = null;
+
+ InputStream in = Instance.getCache().getFromCache(id);
+ if (in != null) {
+ try {
+ resizedImage = IOUtils.toImage(in);
+ in.close();
+ in = null;
+ } catch (IOException e) {
+ Instance.syserr(e);
+ }
+ }
+
+ if (resizedImage == null) {
+ try {
+ BufferedImage cover = Instance.getLibrary().getCover(
+ meta.getLuid());
+
+ resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH,
+ SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
+ BufferedImage.TYPE_4BYTE_ABGR);
+ Graphics2D g = resizedImage.createGraphics();
+ g.setColor(Color.white);
+ g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
+ if (cover != null) {
+ g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
+ null);
+ } else {
+ g.setColor(Color.black);
+ g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
+ g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
+ }
+ 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;
+ } catch (MalformedURLException e) {
+ Instance.syserr(e);
+ } catch (IOException e) {
+ Instance.syserr(e);
+ }
}
- g.dispose();
return new ImageIcon(resizedImage);
}