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