1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.BorderLayout
;
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
;
22 import javax
.imageio
.ImageIO
;
23 import javax
.swing
.ImageIcon
;
24 import javax
.swing
.JLabel
;
25 import javax
.swing
.JPanel
;
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
;
34 * A book item presented in a {@link GuiReaderFrame}.
38 class GuiReaderBook
extends JPanel
{
40 * Action on a book item.
44 interface BookActionListener
extends EventListener
{
46 * The book was selected (single click).
49 * the {@link GuiReaderBook} itself
51 public void select(GuiReaderBook book
);
54 * The book was double-clicked.
57 * the {@link GuiReaderBook} itself
59 public void action(GuiReaderBook book
);
62 * A popup menu was requested for this {@link GuiReaderBook}.
65 * the {@link GuiReaderBook} itself
67 * the {@link MouseEvent} that generated this call
69 public void popupRequested(GuiReaderBook book
, MouseEvent e
);
72 private static final long serialVersionUID
= 1L;
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
91 private boolean selected
;
92 private boolean hovered
;
93 private Date lastClick
;
95 private List
<BookActionListener
> listeners
;
96 private Reader reader
;
97 private MetaData meta
;
98 private boolean cached
;
101 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
104 * the associated reader
106 * the story {@link MetaData} or source (if no LUID)
108 * TRUE if it is locally cached
109 * @param seeWordCount
110 * TRUE to see word counts, FALSE to see authors
112 public GuiReaderBook(Reader reader
, MetaData meta
, boolean cached
,
113 boolean seeWordCount
) {
114 this.reader
= reader
;
115 this.cached
= cached
;
118 String optSecondary
= meta
.getAuthor();
120 if (meta
.getWords() >= 4000) {
121 optSecondary
= "" + (meta
.getWords() / 1000) + "k";
122 } else if (meta
.getWords() > 0) {
123 optSecondary
= "" + meta
.getWords();
128 if (!optSecondary
.isEmpty()) {
129 if (meta
.isImageDocument()) {
130 optSecondary
+= " images";
132 optSecondary
+= " words";
137 if (optSecondary
!= null && !optSecondary
.isEmpty()) {
138 optSecondary
= "(" + optSecondary
+ ")";
143 icon
= new JLabel(generateCoverIcon());
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
,
153 setLayout(new BorderLayout(10, 10));
154 add(icon
, BorderLayout
.CENTER
);
155 add(title
, BorderLayout
.SOUTH
);
161 * The book current selection state.
163 * @return the selection state
165 public boolean isSelected() {
170 * The book current selection state.
173 * TRUE if it is selected
175 public void setSelected(boolean selected
) {
176 if (this.selected
!= selected
) {
177 this.selected
= selected
;
183 * The item mouse-hover state.
186 * TRUE if it is mouse-hovered
188 private void setHovered(boolean hovered
) {
189 if (this.hovered
!= hovered
) {
190 this.hovered
= hovered
;
196 * Setup the mouse listener that will activate {@link BookActionListener}
199 private void setupListeners() {
200 listeners
= new ArrayList
<GuiReaderBook
.BookActionListener
>();
201 addMouseListener(new MouseListener() {
203 public void mouseReleased(MouseEvent e
) {
204 if (e
.isPopupTrigger()) {
210 public void mousePressed(MouseEvent e
) {
211 if (e
.isPopupTrigger()) {
217 public void mouseExited(MouseEvent e
) {
222 public void mouseEntered(MouseEvent e
) {
227 public void mouseClicked(MouseEvent e
) {
229 Date now
= new Date();
230 if (lastClick
!= null
231 && now
.getTime() - lastClick
.getTime() < doubleClickDelay
) {
241 private void click(boolean doubleClick
) {
242 for (BookActionListener listener
: listeners
) {
244 listener
.action(GuiReaderBook
.this);
246 listener
.select(GuiReaderBook
.this);
251 private void popup(MouseEvent e
) {
252 for (BookActionListener listener
: listeners
) {
253 listener
.select((GuiReaderBook
.this));
254 listener
.popupRequested(GuiReaderBook
.this, e
);
261 * Add a new {@link BookActionListener} on this item.
266 public void addActionListener(BookActionListener listener
) {
267 listeners
.add(listener
);
271 * The Library {@link MetaData} of the book represented by this item.
275 public MetaData
getMeta() {
280 * This item {@link GuiReader} library cache state.
282 * @return TRUE if it is present in the {@link GuiReader} cache
284 public boolean isCached() {
289 * This item {@link GuiReader} library cache state.
292 * TRUE if it is present in the {@link GuiReader} cache
294 public void setCached(boolean cached
) {
295 if (this.cached
!= cached
) {
296 this.cached
= cached
;
302 * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
305 public void paint(Graphics g
) {
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.
316 * the {@link Graphics} to paint onto
318 public void paintOverlay(Graphics g
) {
319 Rectangle clip
= g
.getClipBounds();
320 if (clip
.getWidth() <= 0 || clip
.getHeight() <= 0) {
324 int h
= COVER_HEIGHT
;
326 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 1;
327 int yOffset
= HOFFSET
;
329 if (BORDER
!= null) {
330 if (BORDER
!= null) {
332 g
.drawRect(xOffset
, yOffset
, COVER_WIDTH
, COVER_HEIGHT
);
339 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
340 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
341 int[] ys
= new int[] { yOffset
+ h
, yOffset
+ h
+ SPINE_HEIGHT
,
342 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
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
};
347 ys
= new int[] { yOffset
, yOffset
+ SPINE_HEIGHT
,
348 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
349 g
.setColor(SPINE_COLOR_RIGHT
);
350 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
352 Color color
= new Color(255, 255, 255, 0);
354 } else if (selected
&& !hovered
) {
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);
363 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);
366 UIUtils
.drawEllipse3D(g
, Color
.green
.darker(), COVER_WIDTH
367 + HOFFSET
+ 30, 10, 20, 20);
372 * Generate a cover icon based upon the given {@link MetaData}.
376 private ImageIcon
generateCoverIcon() {
377 BufferedImage resizedImage
= null;
378 String id
= getIconId(meta
);
380 InputStream in
= Instance
.getCache().getFromCache(id
);
383 resizedImage
= ImageUtils
.fromStream(in
);
386 } catch (IOException e
) {
391 if (resizedImage
== null) {
393 BufferedImage cover
= null;
394 if (meta
.getLuid() == null) {
395 cover
= reader
.getLibrary()
396 .getSourceCover(meta
.getSource());
398 cover
= reader
.getLibrary().getCover(meta
.getLuid());
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
);
408 g
.drawImage(cover
, 0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
,
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
);
418 ByteArrayOutputStream out
= new ByteArrayOutputStream();
419 ImageIO
.write(resizedImage
, "png", out
);
420 byte[] imageBytes
= out
.toByteArray();
421 in
= new ByteArrayInputStream(imageBytes
);
422 Instance
.getCache().addToCache(in
, id
);
426 } catch (MalformedURLException e
) {
428 } catch (IOException e
) {
433 return new ImageIcon(resizedImage
);
437 * Manually clear the icon set for this item.
440 * the meta of the story or source (if luid is null)
442 public static void clearIcon(MetaData meta
) {
443 String id
= getIconId(meta
);
444 Instance
.getCache().removeFromCache(id
);
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}).
453 * @return the unique ID
455 private static String
getIconId(MetaData meta
) {
458 String key
= meta
.getUuid();
459 if (meta
.getLuid() == null) {
460 // a fake meta (== a source)
461 key
= "source_" + meta
.getSource();
464 id
= key
+ ".thumb_" + SPINE_WIDTH
+ "x" + COVER_WIDTH
+ "+"
465 + SPINE_HEIGHT
+ "+" + COVER_HEIGHT
+ "@" + HOFFSET
;