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