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
.IOUtils
;
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 MetaData meta
;
97 private boolean cached
;
100 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
103 * the story {@link MetaData}
105 * TRUE if it is locally cached
106 * @param seeWordCount
107 * TRUE to see word counts, FALSE to see authors
109 public GuiReaderBook(MetaData meta
, boolean cached
, boolean seeWordCount
) {
110 this.cached
= cached
;
113 String optSecondary
= meta
.getAuthor();
115 if (meta
.getWords() >= 4000) {
116 optSecondary
= (meta
.getWords() / 1000) + "k words";
117 } else if (meta
.getWords() > 0) {
118 optSecondary
= meta
.getWords() + " words";
124 if (optSecondary
!= null && !optSecondary
.isEmpty()) {
125 optSecondary
= "(" + optSecondary
+ ")";
128 icon
= new JLabel(generateCoverIcon(meta
));
132 + "<body style='width: %d px; height: %d px; text-align: center'>"
133 + "%s" + "<br>" + "<span style='color: %s;'>"
134 + "%s" + "</span>" + "</body>" + "</html>",
135 TEXT_WIDTH
, TEXT_HEIGHT
, meta
.getTitle(), AUTHOR_COLOR
,
138 setLayout(new BorderLayout(10, 10));
139 add(icon
, BorderLayout
.CENTER
);
140 add(title
, BorderLayout
.SOUTH
);
146 * The book current selection state.
148 * @return the selection state
150 public boolean isSelected() {
155 * The book current selection state.
158 * TRUE if it is selected
160 public void setSelected(boolean selected
) {
161 if (this.selected
!= selected
) {
162 this.selected
= selected
;
168 * The item mouse-hover state.
171 * TRUE if it is mouse-hovered
173 private void setHovered(boolean hovered
) {
174 if (this.hovered
!= hovered
) {
175 this.hovered
= hovered
;
181 * Setup the mouse listener that will activate {@link BookActionListener}
184 private void setupListeners() {
185 listeners
= new ArrayList
<GuiReaderBook
.BookActionListener
>();
186 addMouseListener(new MouseListener() {
187 public void mouseReleased(MouseEvent e
) {
188 if (e
.isPopupTrigger()) {
193 public void mousePressed(MouseEvent e
) {
194 if (e
.isPopupTrigger()) {
199 public void mouseExited(MouseEvent e
) {
203 public void mouseEntered(MouseEvent e
) {
207 public void mouseClicked(MouseEvent e
) {
209 Date now
= new Date();
210 if (lastClick
!= null
211 && now
.getTime() - lastClick
.getTime() < doubleClickDelay
) {
221 private void click(boolean doubleClick
) {
222 for (BookActionListener listener
: listeners
) {
224 listener
.action(GuiReaderBook
.this);
226 listener
.select(GuiReaderBook
.this);
231 private void popup(MouseEvent e
) {
232 for (BookActionListener listener
: listeners
) {
233 listener
.select((GuiReaderBook
.this));
234 listener
.popupRequested(GuiReaderBook
.this, e
);
241 * Add a new {@link BookActionListener} on this item.
246 public void addActionListener(BookActionListener listener
) {
247 listeners
.add(listener
);
251 * The Library {@link MetaData} of the book represented by this item.
255 public MetaData
getMeta() {
260 * This item {@link GuiReader} library cache state.
262 * @return TRUE if it is present in the {@link GuiReader} cache
264 public boolean isCached() {
269 * This item {@link GuiReader} library cache state.
272 * TRUE if it is present in the {@link GuiReader} cache
274 public void setCached(boolean cached
) {
275 if (this.cached
!= cached
) {
276 this.cached
= cached
;
282 * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
285 public void paint(Graphics g
) {
291 * Draw a partially transparent overlay if needed depending upon the
292 * selection and mouse-hover states on top of the normal component, as well
293 * as a possible "cached" icon if the item is cached.
296 * the {@link Graphics} to paint onto
298 public void paintOverlay(Graphics g
) {
299 Rectangle clip
= g
.getClipBounds();
300 if (clip
.getWidth() <= 0 || clip
.getHeight() <= 0) {
304 int h
= COVER_HEIGHT
;
306 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 1;
307 int yOffset
= HOFFSET
;
309 if (BORDER
!= null) {
310 if (BORDER
!= null) {
312 g
.drawRect(xOffset
, yOffset
, COVER_WIDTH
, COVER_HEIGHT
);
319 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
320 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
321 int[] ys
= new int[] { yOffset
+ h
, yOffset
+ h
+ SPINE_HEIGHT
,
322 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
323 g
.setColor(SPINE_COLOR_BOTTOM
);
324 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
325 xs
= new int[] { xOffset
+ w
, xOffset
+ w
+ SPINE_WIDTH
,
326 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
327 ys
= new int[] { yOffset
, yOffset
+ SPINE_HEIGHT
,
328 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
329 g
.setColor(SPINE_COLOR_RIGHT
);
330 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
332 Color color
= new Color(255, 255, 255, 0);
334 } else if (selected
&& !hovered
) {
335 color
= new Color(80, 80, 100, 40);
336 } else if (!selected
&& hovered
) {
337 color
= new Color(230, 230, 255, 100);
338 } else if (selected
&& hovered
) {
339 color
= new Color(200, 200, 255, 100);
343 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);
346 UIUtils
.drawEllipse3D(g
, Color
.green
.darker(), COVER_WIDTH
347 + HOFFSET
+ 30, 10, 20, 20);
352 * Generate a cover icon based upon the given {@link MetaData}.
355 * the {@link MetaData} about the target {@link Story}
359 private ImageIcon
generateCoverIcon(MetaData meta
) {
360 String id
= meta
.getUuid() + ".thumb_" + SPINE_WIDTH
+ "x"
361 + COVER_WIDTH
+ "+" + SPINE_HEIGHT
+ "+" + COVER_HEIGHT
+ "@"
363 BufferedImage resizedImage
= null;
365 InputStream in
= Instance
.getCache().getFromCache(id
);
368 resizedImage
= IOUtils
.toImage(in
);
371 } catch (IOException e
) {
376 if (resizedImage
== null) {
378 BufferedImage cover
= Instance
.getLibrary().getCover(
381 resizedImage
= new BufferedImage(SPINE_WIDTH
+ COVER_WIDTH
,
382 SPINE_HEIGHT
+ COVER_HEIGHT
+ HOFFSET
,
383 BufferedImage
.TYPE_4BYTE_ABGR
);
384 Graphics2D g
= resizedImage
.createGraphics();
385 g
.setColor(Color
.white
);
386 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
388 g
.drawImage(cover
, 0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
,
391 g
.setColor(Color
.black
);
392 g
.drawLine(0, HOFFSET
, COVER_WIDTH
, HOFFSET
+ COVER_HEIGHT
);
393 g
.drawLine(COVER_WIDTH
, HOFFSET
, 0, HOFFSET
+ COVER_HEIGHT
);
397 ByteArrayOutputStream out
= new ByteArrayOutputStream();
398 ImageIO
.write(resizedImage
, "png", out
);
399 byte[] imageBytes
= out
.toByteArray();
400 in
= new ByteArrayInputStream(imageBytes
);
401 Instance
.getCache().addToCache(in
, id
);
404 } catch (MalformedURLException e
) {
406 } catch (IOException e
) {
411 return new ImageIcon(resizedImage
);