Add more warnings source to 1.6) and fix warnings
[nikiroo-utils.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}
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 }
132
133 icon = new JLabel(generateCoverIcon(meta));
134 title = new JLabel(
135 String.format(
136 "<html>"
137 + "<body style='width: %d px; height: %d px; text-align: center'>"
138 + "%s" + "<br>" + "<span style='color: %s;'>"
139 + "%s" + "</span>" + "</body>" + "</html>",
140 TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
141 optSecondary));
142
143 setLayout(new BorderLayout(10, 10));
144 add(icon, BorderLayout.CENTER);
145 add(title, BorderLayout.SOUTH);
146
147 setupListeners();
148 }
149
150 /**
151 * The book current selection state.
152 *
153 * @return the selection state
154 */
155 public boolean isSelected() {
156 return selected;
157 }
158
159 /**
160 * The book current selection state.
161 *
162 * @param selected
163 * TRUE if it is selected
164 */
165 public void setSelected(boolean selected) {
166 if (this.selected != selected) {
167 this.selected = selected;
168 repaint();
169 }
170 }
171
172 /**
173 * The item mouse-hover state.
174 *
175 * @param hovered
176 * TRUE if it is mouse-hovered
177 */
178 private void setHovered(boolean hovered) {
179 if (this.hovered != hovered) {
180 this.hovered = hovered;
181 repaint();
182 }
183 }
184
185 /**
186 * Setup the mouse listener that will activate {@link BookActionListener}
187 * events.
188 */
189 private void setupListeners() {
190 listeners = new ArrayList<GuiReaderBook.BookActionListener>();
191 addMouseListener(new MouseListener() {
192 @Override
193 public void mouseReleased(MouseEvent e) {
194 if (e.isPopupTrigger()) {
195 popup(e);
196 }
197 }
198
199 @Override
200 public void mousePressed(MouseEvent e) {
201 if (e.isPopupTrigger()) {
202 popup(e);
203 }
204 }
205
206 @Override
207 public void mouseExited(MouseEvent e) {
208 setHovered(false);
209 }
210
211 @Override
212 public void mouseEntered(MouseEvent e) {
213 setHovered(true);
214 }
215
216 @Override
217 public void mouseClicked(MouseEvent e) {
218 if (isEnabled()) {
219 Date now = new Date();
220 if (lastClick != null
221 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
222 click(true);
223 } else {
224 click(false);
225 }
226
227 lastClick = now;
228 }
229 }
230
231 private void click(boolean doubleClick) {
232 for (BookActionListener listener : listeners) {
233 if (doubleClick) {
234 listener.action(GuiReaderBook.this);
235 } else {
236 listener.select(GuiReaderBook.this);
237 }
238 }
239 }
240
241 private void popup(MouseEvent e) {
242 for (BookActionListener listener : listeners) {
243 listener.select((GuiReaderBook.this));
244 listener.popupRequested(GuiReaderBook.this, e);
245 }
246 }
247 });
248 }
249
250 /**
251 * Add a new {@link BookActionListener} on this item.
252 *
253 * @param listener
254 * the listener
255 */
256 public void addActionListener(BookActionListener listener) {
257 listeners.add(listener);
258 }
259
260 /**
261 * The Library {@link MetaData} of the book represented by this item.
262 *
263 * @return the meta
264 */
265 public MetaData getMeta() {
266 return meta;
267 }
268
269 /**
270 * This item {@link GuiReader} library cache state.
271 *
272 * @return TRUE if it is present in the {@link GuiReader} cache
273 */
274 public boolean isCached() {
275 return cached;
276 }
277
278 /**
279 * This item {@link GuiReader} library cache state.
280 *
281 * @param cached
282 * TRUE if it is present in the {@link GuiReader} cache
283 */
284 public void setCached(boolean cached) {
285 if (this.cached != cached) {
286 this.cached = cached;
287 repaint();
288 }
289 }
290
291 /**
292 * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
293 */
294 @Override
295 public void paint(Graphics g) {
296 super.paint(g);
297 paintOverlay(g);
298 }
299
300 /**
301 * Draw a partially transparent overlay if needed depending upon the
302 * selection and mouse-hover states on top of the normal component, as well
303 * as a possible "cached" icon if the item is cached.
304 *
305 * @param g
306 * the {@link Graphics} to paint onto
307 */
308 public void paintOverlay(Graphics g) {
309 Rectangle clip = g.getClipBounds();
310 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
311 return;
312 }
313
314 int h = COVER_HEIGHT;
315 int w = COVER_WIDTH;
316 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
317 int yOffset = HOFFSET;
318
319 if (BORDER != null) {
320 if (BORDER != null) {
321 g.setColor(BORDER);
322 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
323 }
324
325 xOffset++;
326 yOffset++;
327 }
328
329 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
330 xOffset + w + SPINE_WIDTH, xOffset + w };
331 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
332 yOffset + h + SPINE_HEIGHT, yOffset + h };
333 g.setColor(SPINE_COLOR_BOTTOM);
334 g.fillPolygon(new Polygon(xs, ys, xs.length));
335 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
336 xOffset + w + SPINE_WIDTH, xOffset + w };
337 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
338 yOffset + h + SPINE_HEIGHT, yOffset + h };
339 g.setColor(SPINE_COLOR_RIGHT);
340 g.fillPolygon(new Polygon(xs, ys, xs.length));
341
342 Color color = new Color(255, 255, 255, 0);
343 if (!isEnabled()) {
344 } else if (selected && !hovered) {
345 color = new Color(80, 80, 100, 40);
346 } else if (!selected && hovered) {
347 color = new Color(230, 230, 255, 100);
348 } else if (selected && hovered) {
349 color = new Color(200, 200, 255, 100);
350 }
351
352 g.setColor(color);
353 g.fillRect(clip.x, clip.y, clip.width, clip.height);
354
355 if (cached) {
356 UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH
357 + HOFFSET + 30, 10, 20, 20);
358 }
359 }
360
361 /**
362 * Generate a cover icon based upon the given {@link MetaData}.
363 *
364 * @param meta
365 * the {@link MetaData} about the target {@link Story}
366 *
367 * @return the icon
368 */
369 private ImageIcon generateCoverIcon(MetaData meta) {
370 String id = meta.getUuid() + ".thumb_" + SPINE_WIDTH + "x"
371 + COVER_WIDTH + "+" + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@"
372 + HOFFSET;
373 BufferedImage resizedImage = null;
374
375 InputStream in = Instance.getCache().getFromCache(id);
376 if (in != null) {
377 try {
378 resizedImage = ImageUtils.fromStream(in);
379 in.close();
380 in = null;
381 } catch (IOException e) {
382 Instance.syserr(e);
383 }
384 }
385
386 if (resizedImage == null) {
387 try {
388 BufferedImage cover = reader.getLibrary().getCover(
389 meta.getLuid());
390
391 resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH,
392 SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
393 BufferedImage.TYPE_4BYTE_ABGR);
394 Graphics2D g = resizedImage.createGraphics();
395 g.setColor(Color.white);
396 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
397 if (cover != null) {
398 g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
399 null);
400 } else {
401 g.setColor(Color.black);
402 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
403 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
404 }
405 g.dispose();
406
407 ByteArrayOutputStream out = new ByteArrayOutputStream();
408 ImageIO.write(resizedImage, "png", out);
409 byte[] imageBytes = out.toByteArray();
410 in = new ByteArrayInputStream(imageBytes);
411 Instance.getCache().addToCache(in, id);
412 in.close();
413 in = null;
414 } catch (MalformedURLException e) {
415 Instance.syserr(e);
416 } catch (IOException e) {
417 Instance.syserr(e);
418 }
419 }
420
421 return new ImageIcon(resizedImage);
422 }
423 }