Improve remote, fix bugs, update nikiroo-utils
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / GuiReaderBook.java
CommitLineData
333f0e7b
NR
1package be.nikiroo.fanfix.reader;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
d3c84ac3 5import java.awt.Graphics;
333f0e7b 6import java.awt.Graphics2D;
b4dc6ab5 7import java.awt.Polygon;
d3c84ac3 8import java.awt.Rectangle;
333f0e7b
NR
9import java.awt.event.MouseEvent;
10import java.awt.event.MouseListener;
11import java.awt.image.BufferedImage;
57f02339
NR
12import java.io.ByteArrayInputStream;
13import java.io.ByteArrayOutputStream;
14import java.io.IOException;
15import java.io.InputStream;
16import java.net.MalformedURLException;
333f0e7b
NR
17import java.util.ArrayList;
18import java.util.Date;
19import java.util.EventListener;
20import java.util.List;
21
57f02339 22import javax.imageio.ImageIO;
333f0e7b
NR
23import javax.swing.ImageIcon;
24import javax.swing.JLabel;
25import javax.swing.JPanel;
26
57f02339 27import be.nikiroo.fanfix.Instance;
333f0e7b 28import be.nikiroo.fanfix.data.MetaData;
4d205683 29import be.nikiroo.fanfix.data.Story;
326093dc 30import be.nikiroo.utils.ImageUtils;
c39a3e30 31import be.nikiroo.utils.ui.UIUtils;
333f0e7b
NR
32
33/**
5dd985cf 34 * A book item presented in a {@link GuiReaderFrame}.
333f0e7b
NR
35 *
36 * @author niki
37 */
5dd985cf 38class GuiReaderBook extends JPanel {
333f0e7b
NR
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
5dd985cf 49 * the {@link GuiReaderBook} itself
333f0e7b 50 */
5dd985cf 51 public void select(GuiReaderBook book);
333f0e7b
NR
52
53 /**
54 * The book was double-clicked.
55 *
56 * @param book
5dd985cf 57 * the {@link GuiReaderBook} itself
333f0e7b 58 */
5dd985cf 59 public void action(GuiReaderBook book);
9843a5e5
NR
60
61 /**
5dd985cf 62 * A popup menu was requested for this {@link GuiReaderBook}.
9843a5e5
NR
63 *
64 * @param book
5dd985cf 65 * the {@link GuiReaderBook} itself
9843a5e5
NR
66 * @param e
67 * the {@link MouseEvent} that generated this call
68 */
5dd985cf 69 public void popupRequested(GuiReaderBook 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;
e42573a0 96 private Reader reader;
22848428 97 private MetaData meta;
10d558d2
NR
98 private boolean cached;
99
4d205683 100 /**
5dd985cf 101 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
4d205683 102 *
e42573a0
NR
103 * @param reader
104 * the associated reader
4d205683 105 * @param meta
14b57448 106 * the story {@link MetaData} or source (if no LUID)
4d205683
NR
107 * @param cached
108 * TRUE if it is locally cached
5dd985cf 109 * @param seeWordCount
793f1071 110 * TRUE to see word counts, FALSE to see authors
4d205683 111 */
e42573a0
NR
112 public GuiReaderBook(Reader reader, MetaData meta, boolean cached,
113 boolean seeWordCount) {
114 this.reader = reader;
10d558d2 115 this.cached = cached;
22848428 116 this.meta = meta;
333f0e7b 117
793f1071
NR
118 String optSecondary = meta.getAuthor();
119 if (seeWordCount) {
120 if (meta.getWords() >= 4000) {
c8faa52a 121 optSecondary = "" + (meta.getWords() / 1000) + "k";
793f1071 122 } else if (meta.getWords() > 0) {
c8faa52a 123 optSecondary = "" + meta.getWords();
793f1071 124 } else {
a3641e4b 125 optSecondary = "";
793f1071 126 }
c8faa52a
NR
127
128 if (!optSecondary.isEmpty()) {
129 if (meta.isImageDocument()) {
130 optSecondary += " images";
131 } else {
132 optSecondary += " words";
133 }
134 }
793f1071
NR
135 }
136
137 if (optSecondary != null && !optSecondary.isEmpty()) {
138 optSecondary = "(" + optSecondary + ")";
14b57448
NR
139 } else {
140 optSecondary = "";
b4dc6ab5 141 }
4d205683 142
085a2f9a 143 icon = new JLabel(generateCoverIcon());
3b2b638f 144 title = new JLabel(
b4dc6ab5
NR
145 String.format(
146 "<html>"
147 + "<body style='width: %d px; height: %d px; text-align: center'>"
148 + "%s" + "<br>" + "<span style='color: %s;'>"
149 + "%s" + "</span>" + "</body>" + "</html>",
150 TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
793f1071 151 optSecondary));
b4dc6ab5 152
edd46289
NR
153 setLayout(new BorderLayout(10, 10));
154 add(icon, BorderLayout.CENTER);
155 add(title, BorderLayout.SOUTH);
333f0e7b
NR
156
157 setupListeners();
333f0e7b
NR
158 }
159
160 /**
161 * The book current selection state.
162 *
4d205683 163 * @return the selection state
333f0e7b
NR
164 */
165 public boolean isSelected() {
166 return selected;
167 }
168
169 /**
170 * The book current selection state.
171 *
172 * @param selected
4d205683 173 * TRUE if it is selected
333f0e7b
NR
174 */
175 public void setSelected(boolean selected) {
edd46289
NR
176 if (this.selected != selected) {
177 this.selected = selected;
178 repaint();
179 }
333f0e7b
NR
180 }
181
4d205683
NR
182 /**
183 * The item mouse-hover state.
184 *
185 * @param hovered
186 * TRUE if it is mouse-hovered
187 */
333f0e7b 188 private void setHovered(boolean hovered) {
edd46289
NR
189 if (this.hovered != hovered) {
190 this.hovered = hovered;
191 repaint();
192 }
333f0e7b
NR
193 }
194
4d205683
NR
195 /**
196 * Setup the mouse listener that will activate {@link BookActionListener}
197 * events.
198 */
333f0e7b 199 private void setupListeners() {
5dd985cf 200 listeners = new ArrayList<GuiReaderBook.BookActionListener>();
333f0e7b 201 addMouseListener(new MouseListener() {
211f7ddb 202 @Override
333f0e7b 203 public void mouseReleased(MouseEvent e) {
9843a5e5
NR
204 if (e.isPopupTrigger()) {
205 popup(e);
206 }
333f0e7b
NR
207 }
208
211f7ddb 209 @Override
333f0e7b 210 public void mousePressed(MouseEvent e) {
9843a5e5
NR
211 if (e.isPopupTrigger()) {
212 popup(e);
213 }
333f0e7b
NR
214 }
215
211f7ddb 216 @Override
333f0e7b
NR
217 public void mouseExited(MouseEvent e) {
218 setHovered(false);
219 }
220
211f7ddb 221 @Override
333f0e7b
NR
222 public void mouseEntered(MouseEvent e) {
223 setHovered(true);
224 }
225
211f7ddb 226 @Override
333f0e7b 227 public void mouseClicked(MouseEvent e) {
3b2b638f
NR
228 if (isEnabled()) {
229 Date now = new Date();
230 if (lastClick != null
231 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
232 click(true);
233 } else {
234 click(false);
235 }
9843a5e5 236
3b2b638f 237 lastClick = now;
333f0e7b 238 }
333f0e7b 239 }
333f0e7b 240
9843a5e5
NR
241 private void click(boolean doubleClick) {
242 for (BookActionListener listener : listeners) {
243 if (doubleClick) {
5dd985cf 244 listener.action(GuiReaderBook.this);
9843a5e5 245 } else {
5dd985cf 246 listener.select(GuiReaderBook.this);
9843a5e5
NR
247 }
248 }
333f0e7b 249 }
9843a5e5
NR
250
251 private void popup(MouseEvent e) {
252 for (BookActionListener listener : listeners) {
5dd985cf
NR
253 listener.select((GuiReaderBook.this));
254 listener.popupRequested(GuiReaderBook.this, e);
9843a5e5
NR
255 }
256 }
257 });
333f0e7b
NR
258 }
259
4d205683
NR
260 /**
261 * Add a new {@link BookActionListener} on this item.
262 *
263 * @param listener
264 * the listener
265 */
92fb0719 266 public void addActionListener(BookActionListener listener) {
333f0e7b
NR
267 listeners.add(listener);
268 }
d3c84ac3 269
4d205683 270 /**
5dd985cf 271 * The Library {@link MetaData} of the book represented by this item.
4d205683 272 *
22848428 273 * @return the meta
4d205683 274 */
22848428
NR
275 public MetaData getMeta() {
276 return meta;
10d558d2
NR
277 }
278
279 /**
5dd985cf 280 * This item {@link GuiReader} library cache state.
10d558d2 281 *
5dd985cf 282 * @return TRUE if it is present in the {@link GuiReader} cache
10d558d2
NR
283 */
284 public boolean isCached() {
285 return cached;
286 }
287
288 /**
5dd985cf 289 * This item {@link GuiReader} library cache state.
10d558d2
NR
290 *
291 * @param cached
5dd985cf 292 * TRUE if it is present in the {@link GuiReader} cache
10d558d2
NR
293 */
294 public void setCached(boolean cached) {
f977d05b
NR
295 if (this.cached != cached) {
296 this.cached = cached;
edd46289 297 repaint();
f977d05b 298 }
10d558d2
NR
299 }
300
4d205683 301 /**
5dd985cf 302 * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
4d205683 303 */
d3c84ac3
NR
304 @Override
305 public void paint(Graphics g) {
306 super.paint(g);
edd46289
NR
307 paintOverlay(g);
308 }
d3c84ac3 309
edd46289
NR
310 /**
311 * Draw a partially transparent overlay if needed depending upon the
312 * selection and mouse-hover states on top of the normal component, as well
313 * as a possible "cached" icon if the item is cached.
5dd985cf
NR
314 *
315 * @param g
316 * the {@link Graphics} to paint onto
edd46289
NR
317 */
318 public void paintOverlay(Graphics g) {
4d205683
NR
319 Rectangle clip = g.getClipBounds();
320 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
321 return;
322 }
323
b4dc6ab5
NR
324 int h = COVER_HEIGHT;
325 int w = COVER_WIDTH;
4d205683
NR
326 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
327 int yOffset = HOFFSET;
328
329 if (BORDER != null) {
330 if (BORDER != null) {
331 g.setColor(BORDER);
332 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
333 }
334
335 xOffset++;
336 yOffset++;
337 }
b4dc6ab5
NR
338
339 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
340 xOffset + w + SPINE_WIDTH, xOffset + w };
4d205683
NR
341 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
342 yOffset + h + SPINE_HEIGHT, yOffset + h };
b4dc6ab5
NR
343 g.setColor(SPINE_COLOR_BOTTOM);
344 g.fillPolygon(new Polygon(xs, ys, xs.length));
345 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
346 xOffset + w + SPINE_WIDTH, xOffset + w };
4d205683
NR
347 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
348 yOffset + h + SPINE_HEIGHT, yOffset + h };
b4dc6ab5
NR
349 g.setColor(SPINE_COLOR_RIGHT);
350 g.fillPolygon(new Polygon(xs, ys, xs.length));
351
d3c84ac3 352 Color color = new Color(255, 255, 255, 0);
3b2b638f
NR
353 if (!isEnabled()) {
354 } else if (selected && !hovered) {
d3c84ac3
NR
355 color = new Color(80, 80, 100, 40);
356 } else if (!selected && hovered) {
357 color = new Color(230, 230, 255, 100);
358 } else if (selected && hovered) {
359 color = new Color(200, 200, 255, 100);
360 }
361
4d205683
NR
362 g.setColor(color);
363 g.fillRect(clip.x, clip.y, clip.width, clip.height);
364
10d558d2 365 if (cached) {
c39a3e30
NR
366 UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH
367 + HOFFSET + 30, 10, 20, 20);
10d558d2 368 }
d3c84ac3 369 }
edd46289
NR
370
371 /**
57f02339 372 * Generate a cover icon based upon the given {@link MetaData}.
edd46289 373 *
edd46289
NR
374 * @return the icon
375 */
085a2f9a 376 private ImageIcon generateCoverIcon() {
57f02339 377 BufferedImage resizedImage = null;
085a2f9a 378 String id = getIconId(meta);
e604986c 379
e604986c
NR
380 InputStream in = Instance.getCache().getFromCache(id);
381 if (in != null) {
382 try {
383 resizedImage = ImageUtils.fromStream(in);
384 in.close();
385 in = null;
386 } catch (IOException e) {
387 Instance.syserr(e);
57f02339
NR
388 }
389 }
390
391 if (resizedImage == null) {
392 try {
14b57448
NR
393 BufferedImage cover = null;
394 if (meta.getLuid() == null) {
395 cover = reader.getLibrary()
396 .getSourceCover(meta.getSource());
397 } else {
398 cover = reader.getLibrary().getCover(meta.getLuid());
399 }
57f02339
NR
400
401 resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH,
402 SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
403 BufferedImage.TYPE_4BYTE_ABGR);
404 Graphics2D g = resizedImage.createGraphics();
405 g.setColor(Color.white);
406 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
407 if (cover != null) {
408 g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
409 null);
410 } else {
411 g.setColor(Color.black);
412 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
413 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
414 }
415 g.dispose();
416
14b57448
NR
417 if (id != null) {
418 ByteArrayOutputStream out = new ByteArrayOutputStream();
419 ImageIO.write(resizedImage, "png", out);
420 byte[] imageBytes = out.toByteArray();
e604986c 421 in = new ByteArrayInputStream(imageBytes);
14b57448
NR
422 Instance.getCache().addToCache(in, id);
423 in.close();
424 in = null;
425 }
57f02339
NR
426 } catch (MalformedURLException e) {
427 Instance.syserr(e);
428 } catch (IOException e) {
429 Instance.syserr(e);
430 }
edd46289 431 }
edd46289
NR
432
433 return new ImageIcon(resizedImage);
434 }
085a2f9a
NR
435
436 /**
437 * Manually clear the icon set for this item.
438 *
439 * @param meta
440 * the meta of the story or source (if luid is null)
441 */
442 public static void clearIcon(MetaData meta) {
443 String id = getIconId(meta);
444 Instance.getCache().removeFromCache(id);
445 }
446
447 /**
448 * Get a unique ID from this meta (note that if the luid is null, it is
449 * considered a source and not a {@link Story}).
450 *
451 * @param meta
452 * the meta
453 * @return the unique ID
454 */
455 private static String getIconId(MetaData meta) {
456 String id = null;
457
458 String key = meta.getUuid();
459 if (meta.getLuid() == null) {
460 // a fake meta (== a source)
461 key = "source_" + meta.getSource();
462 }
463
464 id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+"
465 + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET;
466
467 return id;
468 }
333f0e7b 469}