Instance: use getInstance()
[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
b02c7819
NR
132 /**
133 * The width of a cover image.
134 *
135 * @return the width
136 */
137 static public int getCoverWidth() {
138 return SPINE_WIDTH + COVER_WIDTH;
139 }
140
141 /**
142 * The height of a cover image.
143 *
144 * @return the height
145 */
146 static public int getCoverHeight() {
147 return COVER_HEIGHT + HOFFSET;
148 }
149
723647dd
NR
150 /**
151 * Generate a cover icon based upon the given {@link GuiReaderBookInfo}.
152 *
153 * @param lib
154 * the library the meta comes from
155 * @param info
156 * the {@link GuiReaderBookInfo}
157 *
158 * @return the icon
159 */
79a99506
NR
160 static public ImageIcon generateCoverIcon(BasicLibrary lib,
161 GuiReaderBookInfo info) {
bb0cff4b 162 BufferedImage resizedImage = null;
79a99506 163 String id = getIconId(info);
bb0cff4b 164
d66deb8d 165 InputStream in = Instance.getInstance().getCache().getFromCache(id);
bb0cff4b
NR
166 if (in != null) {
167 try {
168 resizedImage = ImageUtilsAwt.fromImage(new Image(in));
169 in.close();
170 in = null;
171 } catch (IOException e) {
d66deb8d 172 Instance.getInstance().getTraceHandler().error(e);
bb0cff4b
NR
173 }
174 }
175
176 if (resizedImage == null) {
177 try {
79a99506 178 Image cover = info.getBaseImage(lib);
b02c7819
NR
179 resizedImage = new BufferedImage(getCoverWidth(),
180 getCoverHeight(), BufferedImage.TYPE_4BYTE_ABGR);
79a99506 181
bb0cff4b 182 Graphics2D g = resizedImage.createGraphics();
79a99506
NR
183 try {
184 g.setColor(Color.white);
185 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
186
187 if (cover != null) {
188 BufferedImage coverb = ImageUtilsAwt.fromImage(cover);
189 g.drawImage(coverb, 0, HOFFSET, COVER_WIDTH,
190 COVER_HEIGHT, null);
191 } else {
192 g.setColor(Color.black);
193 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET
194 + COVER_HEIGHT);
195 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET
196 + COVER_HEIGHT);
197 }
198 } finally {
199 g.dispose();
bb0cff4b 200 }
bb0cff4b
NR
201
202 if (id != null) {
203 ByteArrayOutputStream out = new ByteArrayOutputStream();
204 ImageIO.write(resizedImage, "png", out);
205 byte[] imageBytes = out.toByteArray();
206 in = new ByteArrayInputStream(imageBytes);
d66deb8d 207 Instance.getInstance().getCache().addToCache(in, id);
bb0cff4b
NR
208 in.close();
209 in = null;
210 }
211 } catch (MalformedURLException e) {
d66deb8d 212 Instance.getInstance().getTraceHandler().error(e);
bb0cff4b 213 } catch (IOException e) {
d66deb8d 214 Instance.getInstance().getTraceHandler().error(e);
bb0cff4b
NR
215 }
216 }
217
218 return new ImageIcon(resizedImage);
219 }
220
221 /**
222 * Manually clear the icon set for this item.
223 *
79a99506
NR
224 * @param info
225 * the info about the story or source/type or author
bb0cff4b 226 */
79a99506
NR
227 static public void clearIcon(GuiReaderBookInfo info) {
228 String id = getIconId(info);
d66deb8d 229 Instance.getInstance().getCache().removeFromCache(id);
bb0cff4b
NR
230 }
231
232 /**
79a99506
NR
233 * Get a unique ID from this {@link GuiReaderBookInfo} (note that it can be
234 * a story, a fake item for a source/type or a fake item for an author).
bb0cff4b 235 *
79a99506
NR
236 * @param info
237 * the info
bb0cff4b
NR
238 * @return the unique ID
239 */
79a99506
NR
240 static private String getIconId(GuiReaderBookInfo info) {
241 return info.getId() + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+"
bb0cff4b 242 + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET;
bb0cff4b
NR
243 }
244}