Commit | Line | Data |
---|---|---|
333f0e7b NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
3 | import java.awt.BorderLayout; | |
4 | import java.awt.Color; | |
d3c84ac3 | 5 | import java.awt.Graphics; |
333f0e7b | 6 | import java.awt.Graphics2D; |
b4dc6ab5 | 7 | import java.awt.Polygon; |
d3c84ac3 | 8 | import java.awt.Rectangle; |
333f0e7b NR |
9 | import java.awt.event.MouseEvent; |
10 | import java.awt.event.MouseListener; | |
11 | import java.awt.image.BufferedImage; | |
57f02339 NR |
12 | import java.io.ByteArrayInputStream; |
13 | import java.io.ByteArrayOutputStream; | |
14 | import java.io.IOException; | |
15 | import java.io.InputStream; | |
16 | import java.net.MalformedURLException; | |
333f0e7b NR |
17 | import java.util.ArrayList; |
18 | import java.util.Date; | |
19 | import java.util.EventListener; | |
20 | import java.util.List; | |
21 | ||
57f02339 | 22 | import javax.imageio.ImageIO; |
333f0e7b NR |
23 | import javax.swing.ImageIcon; |
24 | import javax.swing.JLabel; | |
25 | import javax.swing.JPanel; | |
26 | ||
57f02339 | 27 | import be.nikiroo.fanfix.Instance; |
333f0e7b | 28 | import be.nikiroo.fanfix.data.MetaData; |
4d205683 | 29 | import be.nikiroo.fanfix.data.Story; |
57f02339 | 30 | import be.nikiroo.utils.IOUtils; |
c39a3e30 | 31 | import be.nikiroo.utils.ui.UIUtils; |
333f0e7b NR |
32 | |
33 | /** | |
34 | * A book item presented in a {@link LocalReaderFrame}. | |
35 | * | |
36 | * @author niki | |
37 | */ | |
38 | class LocalReaderBook extends JPanel { | |
39 | /** | |
40 | * Action on a book item. | |
41 | * | |
42 | * @author niki | |
43 | */ | |
92fb0719 | 44 | interface BookActionListener extends EventListener { |
333f0e7b NR |
45 | /** |
46 | * The book was selected (single click). | |
47 | * | |
48 | * @param book | |
49 | * the {@link LocalReaderBook} itself | |
50 | */ | |
51 | public void select(LocalReaderBook book); | |
52 | ||
53 | /** | |
54 | * The book was double-clicked. | |
55 | * | |
56 | * @param book | |
57 | * the {@link LocalReaderBook} itself | |
58 | */ | |
59 | public void action(LocalReaderBook book); | |
9843a5e5 NR |
60 | |
61 | /** | |
62 | * A popup menu was requested for this {@link LocalReaderBook}. | |
63 | * | |
64 | * @param book | |
65 | * the {@link LocalReaderBook} itself | |
66 | * @param e | |
67 | * the {@link MouseEvent} that generated this call | |
68 | */ | |
69 | public void popupRequested(LocalReaderBook book, MouseEvent e); | |
333f0e7b NR |
70 | } |
71 | ||
4d205683 NR |
72 | private static final long serialVersionUID = 1L; |
73 | ||
74 | // TODO: export some of the configuration options? | |
b4dc6ab5 NR |
75 | private static final int COVER_WIDTH = 100; |
76 | private static final int COVER_HEIGHT = 150; | |
77 | private static final int SPINE_WIDTH = 5; | |
78 | private static final int SPINE_HEIGHT = 5; | |
79 | private static final int HOFFSET = 20; | |
80 | private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180); | |
81 | private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100); | |
82 | private static final int TEXT_WIDTH = COVER_WIDTH + 40; | |
83 | private static final int TEXT_HEIGHT = 50; | |
84 | private static final String AUTHOR_COLOR = "#888888"; | |
4d205683 NR |
85 | private static final Color BORDER = Color.black; |
86 | private static final long doubleClickDelay = 200; // in ms | |
87 | // | |
3b2b638f | 88 | |
333f0e7b | 89 | private JLabel icon; |
3b2b638f | 90 | private JLabel title; |
333f0e7b NR |
91 | private boolean selected; |
92 | private boolean hovered; | |
93 | private Date lastClick; | |
4d205683 | 94 | |
92fb0719 | 95 | private List<BookActionListener> listeners; |
22848428 | 96 | private MetaData meta; |
10d558d2 NR |
97 | private boolean cached; |
98 | ||
4d205683 | 99 | /** |
edd46289 | 100 | * Create a new {@link LocalReaderBook} item for the given {@link Story}. |
4d205683 NR |
101 | * |
102 | * @param meta | |
103 | * the story {@code}link MetaData} | |
104 | * @param cached | |
105 | * TRUE if it is locally cached | |
793f1071 NR |
106 | * @param seeWordcount |
107 | * TRUE to see word counts, FALSE to see authors | |
4d205683 | 108 | */ |
793f1071 | 109 | public LocalReaderBook(MetaData meta, boolean cached, boolean seeWordCount) { |
10d558d2 | 110 | this.cached = cached; |
22848428 | 111 | this.meta = meta; |
333f0e7b | 112 | |
793f1071 NR |
113 | String optSecondary = meta.getAuthor(); |
114 | if (seeWordCount) { | |
115 | if (meta.getWords() >= 4000) { | |
116 | optSecondary = (meta.getWords() / 1000) + "k words"; | |
117 | } else if (meta.getWords() > 0) { | |
118 | optSecondary = meta.getWords() + " words"; | |
119 | } else { | |
a3641e4b | 120 | optSecondary = ""; |
793f1071 NR |
121 | } |
122 | } | |
123 | ||
124 | if (optSecondary != null && !optSecondary.isEmpty()) { | |
125 | optSecondary = "(" + optSecondary + ")"; | |
b4dc6ab5 | 126 | } |
4d205683 | 127 | |
57f02339 | 128 | icon = new JLabel(generateCoverIcon(meta)); |
3b2b638f | 129 | title = new JLabel( |
b4dc6ab5 NR |
130 | String.format( |
131 | "<html>" | |
132 | + "<body style='width: %d px; height: %d px; text-align: center'>" | |
133 | + "%s" + "<br>" + "<span style='color: %s;'>" | |
134 | + "%s" + "</span>" + "</body>" + "</html>", | |
135 | TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR, | |
793f1071 | 136 | optSecondary)); |
b4dc6ab5 | 137 | |
edd46289 NR |
138 | setLayout(new BorderLayout(10, 10)); |
139 | add(icon, BorderLayout.CENTER); | |
140 | add(title, BorderLayout.SOUTH); | |
333f0e7b NR |
141 | |
142 | setupListeners(); | |
333f0e7b NR |
143 | } |
144 | ||
145 | /** | |
146 | * The book current selection state. | |
147 | * | |
4d205683 | 148 | * @return the selection state |
333f0e7b NR |
149 | */ |
150 | public boolean isSelected() { | |
151 | return selected; | |
152 | } | |
153 | ||
154 | /** | |
155 | * The book current selection state. | |
156 | * | |
157 | * @param selected | |
4d205683 | 158 | * TRUE if it is selected |
333f0e7b NR |
159 | */ |
160 | public void setSelected(boolean selected) { | |
edd46289 NR |
161 | if (this.selected != selected) { |
162 | this.selected = selected; | |
163 | repaint(); | |
164 | } | |
333f0e7b NR |
165 | } |
166 | ||
4d205683 NR |
167 | /** |
168 | * The item mouse-hover state. | |
169 | * | |
170 | * @param hovered | |
171 | * TRUE if it is mouse-hovered | |
172 | */ | |
333f0e7b | 173 | private void setHovered(boolean hovered) { |
edd46289 NR |
174 | if (this.hovered != hovered) { |
175 | this.hovered = hovered; | |
176 | repaint(); | |
177 | } | |
333f0e7b NR |
178 | } |
179 | ||
4d205683 NR |
180 | /** |
181 | * Setup the mouse listener that will activate {@link BookActionListener} | |
182 | * events. | |
183 | */ | |
333f0e7b | 184 | private void setupListeners() { |
92fb0719 | 185 | listeners = new ArrayList<LocalReaderBook.BookActionListener>(); |
333f0e7b NR |
186 | addMouseListener(new MouseListener() { |
187 | public void mouseReleased(MouseEvent e) { | |
9843a5e5 NR |
188 | if (e.isPopupTrigger()) { |
189 | popup(e); | |
190 | } | |
333f0e7b NR |
191 | } |
192 | ||
193 | public void mousePressed(MouseEvent e) { | |
9843a5e5 NR |
194 | if (e.isPopupTrigger()) { |
195 | popup(e); | |
196 | } | |
333f0e7b NR |
197 | } |
198 | ||
199 | public void mouseExited(MouseEvent e) { | |
200 | setHovered(false); | |
201 | } | |
202 | ||
203 | public void mouseEntered(MouseEvent e) { | |
204 | setHovered(true); | |
205 | } | |
206 | ||
207 | public void mouseClicked(MouseEvent e) { | |
3b2b638f NR |
208 | if (isEnabled()) { |
209 | Date now = new Date(); | |
210 | if (lastClick != null | |
211 | && now.getTime() - lastClick.getTime() < doubleClickDelay) { | |
212 | click(true); | |
213 | } else { | |
214 | click(false); | |
215 | } | |
9843a5e5 | 216 | |
3b2b638f | 217 | lastClick = now; |
333f0e7b | 218 | } |
333f0e7b | 219 | } |
333f0e7b | 220 | |
9843a5e5 NR |
221 | private void click(boolean doubleClick) { |
222 | for (BookActionListener listener : listeners) { | |
223 | if (doubleClick) { | |
224 | listener.action(LocalReaderBook.this); | |
225 | } else { | |
226 | listener.select(LocalReaderBook.this); | |
227 | } | |
228 | } | |
333f0e7b | 229 | } |
9843a5e5 NR |
230 | |
231 | private void popup(MouseEvent e) { | |
232 | for (BookActionListener listener : listeners) { | |
233 | listener.select((LocalReaderBook.this)); | |
234 | listener.popupRequested(LocalReaderBook.this, e); | |
235 | } | |
236 | } | |
237 | }); | |
333f0e7b NR |
238 | } |
239 | ||
4d205683 NR |
240 | /** |
241 | * Add a new {@link BookActionListener} on this item. | |
242 | * | |
243 | * @param listener | |
244 | * the listener | |
245 | */ | |
92fb0719 | 246 | public void addActionListener(BookActionListener listener) { |
333f0e7b NR |
247 | listeners.add(listener); |
248 | } | |
d3c84ac3 | 249 | |
4d205683 | 250 | /** |
22848428 | 251 | * The Library {@code}link MetaData} of the book represented by this item. |
4d205683 | 252 | * |
22848428 | 253 | * @return the meta |
4d205683 | 254 | */ |
22848428 NR |
255 | public MetaData getMeta() { |
256 | return meta; | |
10d558d2 NR |
257 | } |
258 | ||
259 | /** | |
4d205683 | 260 | * This item {@link LocalReader} library cache state. |
10d558d2 | 261 | * |
4d205683 | 262 | * @return TRUE if it is present in the {@link LocalReader} cache |
10d558d2 NR |
263 | */ |
264 | public boolean isCached() { | |
265 | return cached; | |
266 | } | |
267 | ||
268 | /** | |
4d205683 | 269 | * This item {@link LocalReader} library cache state. |
10d558d2 NR |
270 | * |
271 | * @param cached | |
4d205683 | 272 | * TRUE if it is present in the {@link LocalReader} cache |
10d558d2 NR |
273 | */ |
274 | public void setCached(boolean cached) { | |
f977d05b NR |
275 | if (this.cached != cached) { |
276 | this.cached = cached; | |
edd46289 | 277 | repaint(); |
f977d05b | 278 | } |
10d558d2 NR |
279 | } |
280 | ||
4d205683 | 281 | /** |
edd46289 | 282 | * Paint the item, then call {@link LocalReaderBook#paintOverlay(Graphics)}. |
4d205683 | 283 | */ |
d3c84ac3 NR |
284 | @Override |
285 | public void paint(Graphics g) { | |
286 | super.paint(g); | |
edd46289 NR |
287 | paintOverlay(g); |
288 | } | |
d3c84ac3 | 289 | |
edd46289 NR |
290 | /** |
291 | * Draw a partially transparent overlay if needed depending upon the | |
292 | * selection and mouse-hover states on top of the normal component, as well | |
293 | * as a possible "cached" icon if the item is cached. | |
294 | */ | |
295 | public void paintOverlay(Graphics g) { | |
4d205683 NR |
296 | Rectangle clip = g.getClipBounds(); |
297 | if (clip.getWidth() <= 0 || clip.getHeight() <= 0) { | |
298 | return; | |
299 | } | |
300 | ||
b4dc6ab5 NR |
301 | int h = COVER_HEIGHT; |
302 | int w = COVER_WIDTH; | |
4d205683 NR |
303 | int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1; |
304 | int yOffset = HOFFSET; | |
305 | ||
306 | if (BORDER != null) { | |
307 | if (BORDER != null) { | |
308 | g.setColor(BORDER); | |
309 | g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT); | |
310 | } | |
311 | ||
312 | xOffset++; | |
313 | yOffset++; | |
314 | } | |
b4dc6ab5 NR |
315 | |
316 | int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, | |
317 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
4d205683 NR |
318 | int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT, |
319 | yOffset + h + SPINE_HEIGHT, yOffset + h }; | |
b4dc6ab5 NR |
320 | g.setColor(SPINE_COLOR_BOTTOM); |
321 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
322 | xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH, | |
323 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
4d205683 NR |
324 | ys = new int[] { yOffset, yOffset + SPINE_HEIGHT, |
325 | yOffset + h + SPINE_HEIGHT, yOffset + h }; | |
b4dc6ab5 NR |
326 | g.setColor(SPINE_COLOR_RIGHT); |
327 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
328 | ||
d3c84ac3 | 329 | Color color = new Color(255, 255, 255, 0); |
3b2b638f NR |
330 | if (!isEnabled()) { |
331 | } else if (selected && !hovered) { | |
d3c84ac3 NR |
332 | color = new Color(80, 80, 100, 40); |
333 | } else if (!selected && hovered) { | |
334 | color = new Color(230, 230, 255, 100); | |
335 | } else if (selected && hovered) { | |
336 | color = new Color(200, 200, 255, 100); | |
337 | } | |
338 | ||
4d205683 NR |
339 | g.setColor(color); |
340 | g.fillRect(clip.x, clip.y, clip.width, clip.height); | |
341 | ||
10d558d2 | 342 | if (cached) { |
c39a3e30 NR |
343 | UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH |
344 | + HOFFSET + 30, 10, 20, 20); | |
10d558d2 | 345 | } |
d3c84ac3 | 346 | } |
edd46289 NR |
347 | |
348 | /** | |
57f02339 | 349 | * Generate a cover icon based upon the given {@link MetaData}. |
edd46289 | 350 | * |
57f02339 NR |
351 | * @param meta |
352 | * the {@link MetaData} about the target {@link Story} | |
edd46289 NR |
353 | * |
354 | * @return the icon | |
355 | */ | |
57f02339 NR |
356 | private ImageIcon generateCoverIcon(MetaData meta) { |
357 | String id = meta.getUuid() + ".thumb_" + SPINE_WIDTH + "x" | |
358 | + COVER_WIDTH + "+" + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" | |
359 | + HOFFSET; | |
360 | BufferedImage resizedImage = null; | |
361 | ||
362 | InputStream in = Instance.getCache().getFromCache(id); | |
363 | if (in != null) { | |
364 | try { | |
365 | resizedImage = IOUtils.toImage(in); | |
366 | in.close(); | |
367 | in = null; | |
368 | } catch (IOException e) { | |
369 | Instance.syserr(e); | |
370 | } | |
371 | } | |
372 | ||
373 | if (resizedImage == null) { | |
374 | try { | |
375 | BufferedImage cover = Instance.getLibrary().getCover( | |
376 | meta.getLuid()); | |
377 | ||
378 | resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, | |
379 | SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, | |
380 | BufferedImage.TYPE_4BYTE_ABGR); | |
381 | Graphics2D g = resizedImage.createGraphics(); | |
382 | g.setColor(Color.white); | |
383 | g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); | |
384 | if (cover != null) { | |
385 | g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, | |
386 | null); | |
387 | } else { | |
388 | g.setColor(Color.black); | |
389 | g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT); | |
390 | g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT); | |
391 | } | |
392 | g.dispose(); | |
393 | ||
394 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
395 | ImageIO.write(resizedImage, "png", out); | |
396 | byte[] imageBytes = out.toByteArray(); | |
397 | in = new ByteArrayInputStream(imageBytes); | |
398 | Instance.getCache().addToCache(in, id); | |
399 | in.close(); | |
400 | in = null; | |
401 | } catch (MalformedURLException e) { | |
402 | Instance.syserr(e); | |
403 | } catch (IOException e) { | |
404 | Instance.syserr(e); | |
405 | } | |
edd46289 | 406 | } |
edd46289 NR |
407 | |
408 | return new ImageIcon(resizedImage); | |
409 | } | |
333f0e7b | 410 | } |