+
+ /**
+ * Generate a cover icon based upon the given {@link MetaData}.
+ *
+ * @param meta
+ * the {@link MetaData} about the target {@link Story}
+ *
+ * @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;
+
+ 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);
+ }
+ }
+
+ return new ImageIcon(resizedImage);
+ }