search: cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderCoverImager.java
CommitLineData
bb0cff4b
NR
1package be.nikiroo.fanfix.reader.ui;
2
3import java.awt.Color;
4import java.awt.Graphics;
5import java.awt.Graphics2D;
6import java.awt.Polygon;
7import java.awt.Rectangle;
8import java.awt.image.BufferedImage;
9import java.io.ByteArrayInputStream;
10import java.io.ByteArrayOutputStream;
11import java.io.IOException;
12import java.io.InputStream;
13import java.net.MalformedURLException;
14
15import javax.imageio.ImageIO;
16import javax.swing.ImageIcon;
17
18import be.nikiroo.fanfix.Instance;
19import be.nikiroo.fanfix.data.MetaData;
bb0cff4b
NR
20import be.nikiroo.fanfix.library.BasicLibrary;
21import be.nikiroo.utils.Image;
22import be.nikiroo.utils.ui.ImageUtilsAwt;
23import be.nikiroo.utils.ui.UIUtils;
24
25/**
26 * This class can create a cover icon ready to use for the graphical
27 * application.
28 *
29 * @author niki
30 */
31class GuiReaderCoverImager {
32
33 // TODO: export some of the configuration options?
34 private static final int COVER_WIDTH = 100;
35 private static final int COVER_HEIGHT = 150;
36 private static final int SPINE_WIDTH = 5;
37 private static final int SPINE_HEIGHT = 5;
38 private static final int HOFFSET = 20;
39 private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180);
40 private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100);
41 private static final Color BORDER = Color.black;
42
43 public static final int TEXT_HEIGHT = 50;
44 public static final int TEXT_WIDTH = COVER_WIDTH + 40;
45
46 //
47
48 /**
49 * Draw a partially transparent overlay if needed depending upon the
50 * selection and mouse-hover states on top of the normal component, as well
51 * as a possible "cached" icon if the item is cached.
52 *
53 * @param g
54 * the {@link Graphics} to paint onto
55 * @param enabled
56 * draw an enabled overlay
57 * @param selected
58 * draw a selected overlay
59 * @param hovered
60 * draw a hovered overlay
61 * @param cached
62 * draw a cached overlay
63 */
64 static public void paintOverlay(Graphics g, boolean enabled,
65 boolean selected, boolean hovered, boolean cached) {
66 Rectangle clip = g.getClipBounds();
67 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
68 return;
69 }
70
71 int h = COVER_HEIGHT;
72 int w = COVER_WIDTH;
73 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
74 int yOffset = HOFFSET;
75
76 if (BORDER != null) {
77 if (BORDER != null) {
78 g.setColor(BORDER);
79 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
80 }
81
82 xOffset++;
83 yOffset++;
84 }
85
86 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
87 xOffset + w + SPINE_WIDTH, xOffset + w };
88 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
89 yOffset + h + SPINE_HEIGHT, yOffset + h };
90 g.setColor(SPINE_COLOR_BOTTOM);
91 g.fillPolygon(new Polygon(xs, ys, xs.length));
92 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
93 xOffset + w + SPINE_WIDTH, xOffset + w };
94 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
95 yOffset + h + SPINE_HEIGHT, yOffset + h };
96 g.setColor(SPINE_COLOR_RIGHT);
97 g.fillPolygon(new Polygon(xs, ys, xs.length));
98
99 Color color = new Color(255, 255, 255, 0);
100 if (!enabled) {
101 } else if (selected && !hovered) {
102 color = new Color(80, 80, 100, 40);
103 } else if (!selected && hovered) {
104 color = new Color(230, 230, 255, 100);
105 } else if (selected && hovered) {
106 color = new Color(200, 200, 255, 100);
107 }
108
109 g.setColor(color);
110 g.fillRect(clip.x, clip.y, clip.width, clip.height);
111
112 if (cached) {
113 UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH
114 + HOFFSET + 30, 10, 20, 20);
115 }
116 }
117
118 /**
119 * Generate a cover icon based upon the given {@link MetaData}.
120 *
121 * @param lib
122 * the library the meta comes from
723647dd 123 * @param meta
bb0cff4b
NR
124 * the {@link MetaData}
125 *
126 * @return the icon
127 */
723647dd
NR
128 static public ImageIcon generateCoverIcon(BasicLibrary lib, MetaData meta) {
129 return generateCoverIcon(lib, GuiReaderBookInfo.fromMeta(meta));
130 }
131
132 /**
133 * Generate a cover icon based upon the given {@link GuiReaderBookInfo}.
134 *
135 * @param lib
136 * the library the meta comes from
137 * @param info
138 * the {@link GuiReaderBookInfo}
139 *
140 * @return the icon
141 */
79a99506
NR
142 static public ImageIcon generateCoverIcon(BasicLibrary lib,
143 GuiReaderBookInfo info) {
bb0cff4b 144 BufferedImage resizedImage = null;
79a99506 145 String id = getIconId(info);
bb0cff4b
NR
146
147 InputStream in = Instance.getCache().getFromCache(id);
148 if (in != null) {
149 try {
150 resizedImage = ImageUtilsAwt.fromImage(new Image(in));
151 in.close();
152 in = null;
153 } catch (IOException e) {
154 Instance.getTraceHandler().error(e);
155 }
156 }
157
158 if (resizedImage == null) {
159 try {
79a99506 160 Image cover = info.getBaseImage(lib);
bb0cff4b
NR
161 resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH,
162 SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
163 BufferedImage.TYPE_4BYTE_ABGR);
79a99506 164
bb0cff4b 165 Graphics2D g = resizedImage.createGraphics();
79a99506
NR
166 try {
167 g.setColor(Color.white);
168 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
169
170 if (cover != null) {
171 BufferedImage coverb = ImageUtilsAwt.fromImage(cover);
172 g.drawImage(coverb, 0, HOFFSET, COVER_WIDTH,
173 COVER_HEIGHT, null);
174 } else {
175 g.setColor(Color.black);
176 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET
177 + COVER_HEIGHT);
178 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET
179 + COVER_HEIGHT);
180 }
181 } finally {
182 g.dispose();
bb0cff4b 183 }
bb0cff4b
NR
184
185 if (id != null) {
186 ByteArrayOutputStream out = new ByteArrayOutputStream();
187 ImageIO.write(resizedImage, "png", out);
188 byte[] imageBytes = out.toByteArray();
189 in = new ByteArrayInputStream(imageBytes);
190 Instance.getCache().addToCache(in, id);
191 in.close();
192 in = null;
193 }
194 } catch (MalformedURLException e) {
195 Instance.getTraceHandler().error(e);
196 } catch (IOException e) {
197 Instance.getTraceHandler().error(e);
198 }
199 }
200
201 return new ImageIcon(resizedImage);
202 }
203
204 /**
205 * Manually clear the icon set for this item.
206 *
79a99506
NR
207 * @param info
208 * the info about the story or source/type or author
bb0cff4b 209 */
79a99506
NR
210 static public void clearIcon(GuiReaderBookInfo info) {
211 String id = getIconId(info);
bb0cff4b
NR
212 Instance.getCache().removeFromCache(id);
213 }
214
215 /**
79a99506
NR
216 * Get a unique ID from this {@link GuiReaderBookInfo} (note that it can be
217 * a story, a fake item for a source/type or a fake item for an author).
bb0cff4b 218 *
79a99506
NR
219 * @param info
220 * the info
bb0cff4b
NR
221 * @return the unique ID
222 */
79a99506
NR
223 static private String getIconId(GuiReaderBookInfo info) {
224 return info.getId() + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+"
bb0cff4b 225 + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET;
bb0cff4b
NR
226 }
227}