- * Generate a cover icon based upon the given {@link MetaData}.
- *
- * @return the icon
- */
- private ImageIcon generateCoverIcon() {
- BufferedImage resizedImage = null;
- String id = getIconId(meta);
-
- InputStream in = Instance.getCache().getFromCache(id);
- if (in != null) {
- try {
- resizedImage = ImageUtilsAwt.fromImage(new Image(in));
- in.close();
- in = null;
- } catch (IOException e) {
- Instance.getTraceHandler().error(e);
- }
- }
-
- if (resizedImage == null) {
- try {
- Image cover = null;
- if (meta.getLuid() != null) {
- cover = reader.getLibrary().getCover(meta.getLuid());
- }
- if (cover == null) {
- cover = reader.getLibrary()
- .getSourceCover(meta.getSource());
- }
-
- 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) {
- BufferedImage coverb = ImageUtilsAwt.fromImage(cover);
- g.drawImage(coverb, 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();
-
- 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.getTraceHandler().error(e);
- } catch (IOException e) {
- Instance.getTraceHandler().error(e);
- }
- }
-
- if (resizedImage != null) {
- return new ImageIcon(resizedImage);
- }
-
- return null;
- }
-
- /**
- * Manually clear the icon set for this item.
- *
- * @param meta
- * the meta of the story or source (if luid is null)
- */
- public static void clearIcon(MetaData meta) {
- String id = getIconId(meta);
- Instance.getCache().removeFromCache(id);
- }
-
- /**
- * Get a unique ID from this meta (note that if the luid is null, it is
- * considered a source and not a {@link Story}).
- *
- * @param meta
- * the meta
- * @return the unique ID