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
.util
.ArrayList
;
13 import java
.util
.Date
;
14 import java
.util
.EventListener
;
15 import java
.util
.List
;
17 import javax
.swing
.ImageIcon
;
18 import javax
.swing
.JLabel
;
19 import javax
.swing
.JPanel
;
21 import be
.nikiroo
.fanfix
.data
.MetaData
;
22 import be
.nikiroo
.fanfix
.data
.Story
;
23 import be
.nikiroo
.utils
.ui
.UIUtils
;
26 * A book item presented in a {@link LocalReaderFrame}.
30 class LocalReaderBook
extends JPanel
{
32 * Action on a book item.
36 interface BookActionListener
extends EventListener
{
38 * The book was selected (single click).
41 * the {@link LocalReaderBook} itself
43 public void select(LocalReaderBook book
);
46 * The book was double-clicked.
49 * the {@link LocalReaderBook} itself
51 public void action(LocalReaderBook book
);
54 * A popup menu was requested for this {@link LocalReaderBook}.
57 * the {@link LocalReaderBook} itself
59 * the {@link MouseEvent} that generated this call
61 public void popupRequested(LocalReaderBook book
, MouseEvent e
);
64 private static final long serialVersionUID
= 1L;
66 // TODO: export some of the configuration options?
67 private static final int COVER_WIDTH
= 100;
68 private static final int COVER_HEIGHT
= 150;
69 private static final int SPINE_WIDTH
= 5;
70 private static final int SPINE_HEIGHT
= 5;
71 private static final int HOFFSET
= 20;
72 private static final Color SPINE_COLOR_BOTTOM
= new Color(180, 180, 180);
73 private static final Color SPINE_COLOR_RIGHT
= new Color(100, 100, 100);
74 private static final int TEXT_WIDTH
= COVER_WIDTH
+ 40;
75 private static final int TEXT_HEIGHT
= 50;
76 private static final String AUTHOR_COLOR
= "#888888";
77 private static final Color BORDER
= Color
.black
;
78 private static final long doubleClickDelay
= 200; // in ms
83 private boolean selected
;
84 private boolean hovered
;
85 private Date lastClick
;
87 private List
<BookActionListener
> listeners
;
88 private MetaData meta
;
89 private boolean cached
;
92 * Create a new {@link LocalReaderBook} item for the given {@link Story}.
95 * the story {@code}link MetaData}
97 * TRUE if it is locally cached
99 * TRUE to see word counts, FALSE to see authors
101 public LocalReaderBook(MetaData meta
, boolean cached
, boolean seeWordCount
) {
102 this.cached
= cached
;
105 String optSecondary
= meta
.getAuthor();
107 if (meta
.getWords() >= 4000) {
108 optSecondary
= (meta
.getWords() / 1000) + "k words";
109 } else if (meta
.getWords() > 0) {
110 optSecondary
= meta
.getWords() + " words";
116 if (optSecondary
!= null && !optSecondary
.isEmpty()) {
117 optSecondary
= "(" + optSecondary
+ ")";
120 icon
= new JLabel(generateCoverIcon(meta
.getCover()));
125 + "<body style='width: %d px; height: %d px; text-align: center'>"
126 + "%s" + "<br>" + "<span style='color: %s;'>"
127 + "%s" + "</span>" + "</body>" + "</html>",
128 TEXT_WIDTH
, TEXT_HEIGHT
, meta
.getTitle(), AUTHOR_COLOR
,
131 setLayout(new BorderLayout(10, 10));
132 add(icon
, BorderLayout
.CENTER
);
133 add(title
, BorderLayout
.SOUTH
);
139 * The book current selection state.
141 * @return the selection state
143 public boolean isSelected() {
148 * The book current selection state.
151 * TRUE if it is selected
153 public void setSelected(boolean selected
) {
154 if (this.selected
!= selected
) {
155 this.selected
= selected
;
161 * The item mouse-hover state.
164 * TRUE if it is mouse-hovered
166 private void setHovered(boolean hovered
) {
167 if (this.hovered
!= hovered
) {
168 this.hovered
= hovered
;
174 * Setup the mouse listener that will activate {@link BookActionListener}
177 private void setupListeners() {
178 listeners
= new ArrayList
<LocalReaderBook
.BookActionListener
>();
179 addMouseListener(new MouseListener() {
180 public void mouseReleased(MouseEvent e
) {
181 if (e
.isPopupTrigger()) {
186 public void mousePressed(MouseEvent e
) {
187 if (e
.isPopupTrigger()) {
192 public void mouseExited(MouseEvent e
) {
196 public void mouseEntered(MouseEvent e
) {
200 public void mouseClicked(MouseEvent e
) {
202 Date now
= new Date();
203 if (lastClick
!= null
204 && now
.getTime() - lastClick
.getTime() < doubleClickDelay
) {
214 private void click(boolean doubleClick
) {
215 for (BookActionListener listener
: listeners
) {
217 listener
.action(LocalReaderBook
.this);
219 listener
.select(LocalReaderBook
.this);
224 private void popup(MouseEvent e
) {
225 for (BookActionListener listener
: listeners
) {
226 listener
.select((LocalReaderBook
.this));
227 listener
.popupRequested(LocalReaderBook
.this, e
);
234 * Add a new {@link BookActionListener} on this item.
239 public void addActionListener(BookActionListener listener
) {
240 listeners
.add(listener
);
244 * The Library {@code}link MetaData} of the book represented by this item.
248 public MetaData
getMeta() {
253 * This item {@link LocalReader} library cache state.
255 * @return TRUE if it is present in the {@link LocalReader} cache
257 public boolean isCached() {
262 * This item {@link LocalReader} library cache state.
265 * TRUE if it is present in the {@link LocalReader} cache
267 public void setCached(boolean cached
) {
268 if (this.cached
!= cached
) {
269 this.cached
= cached
;
275 * Paint the item, then call {@link LocalReaderBook#paintOverlay(Graphics)}.
278 public void paint(Graphics g
) {
284 * Draw a partially transparent overlay if needed depending upon the
285 * selection and mouse-hover states on top of the normal component, as well
286 * as a possible "cached" icon if the item is cached.
288 public void paintOverlay(Graphics g
) {
289 Rectangle clip
= g
.getClipBounds();
290 if (clip
.getWidth() <= 0 || clip
.getHeight() <= 0) {
294 int h
= COVER_HEIGHT
;
296 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 1;
297 int yOffset
= HOFFSET
;
299 if (BORDER
!= null) {
300 if (BORDER
!= null) {
302 g
.drawRect(xOffset
, yOffset
, COVER_WIDTH
, COVER_HEIGHT
);
309 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
310 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
311 int[] ys
= new int[] { yOffset
+ h
, yOffset
+ h
+ SPINE_HEIGHT
,
312 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
313 g
.setColor(SPINE_COLOR_BOTTOM
);
314 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
315 xs
= new int[] { xOffset
+ w
, xOffset
+ w
+ SPINE_WIDTH
,
316 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
317 ys
= new int[] { yOffset
, yOffset
+ SPINE_HEIGHT
,
318 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
319 g
.setColor(SPINE_COLOR_RIGHT
);
320 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
322 Color color
= new Color(255, 255, 255, 0);
324 } else if (selected
&& !hovered
) {
325 color
= new Color(80, 80, 100, 40);
326 } else if (!selected
&& hovered
) {
327 color
= new Color(230, 230, 255, 100);
328 } else if (selected
&& hovered
) {
329 color
= new Color(200, 200, 255, 100);
333 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);
336 UIUtils
.drawEllipse3D(g
, Color
.green
.darker(), COVER_WIDTH
337 + HOFFSET
+ 30, 10, 20, 20);
342 * Generate a cover icon based upon the given cover image (which may be
346 * the cover image, or NULL for none
350 private ImageIcon
generateCoverIcon(BufferedImage image
) {
351 BufferedImage resizedImage
= new BufferedImage(SPINE_WIDTH
352 + COVER_WIDTH
, SPINE_HEIGHT
+ COVER_HEIGHT
+ HOFFSET
,
353 BufferedImage
.TYPE_4BYTE_ABGR
);
354 Graphics2D g
= resizedImage
.createGraphics();
355 g
.setColor(Color
.white
);
356 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
358 g
.drawImage(image
, 0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
, null);
360 g
.setColor(Color
.black
);
361 g
.drawLine(0, HOFFSET
, COVER_WIDTH
, HOFFSET
+ COVER_HEIGHT
);
362 g
.drawLine(COVER_WIDTH
, HOFFSET
, 0, HOFFSET
+ COVER_HEIGHT
);
366 return new ImageIcon(resizedImage
);