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
;
25 * A book item presented in a {@link LocalReaderFrame}.
29 class LocalReaderBook
extends JPanel
{
31 * Action on a book item.
35 interface BookActionListener
extends EventListener
{
37 * The book was selected (single click).
40 * the {@link LocalReaderBook} itself
42 public void select(LocalReaderBook book
);
45 * The book was double-clicked.
48 * the {@link LocalReaderBook} itself
50 public void action(LocalReaderBook book
);
53 * A popup menu was requested for this {@link LocalReaderBook}.
56 * the {@link LocalReaderBook} itself
58 * the {@link MouseEvent} that generated this call
60 public void popupRequested(LocalReaderBook book
, MouseEvent e
);
63 private static final long serialVersionUID
= 1L;
65 // TODO: export some of the configuration options?
66 private static final int COVER_WIDTH
= 100;
67 private static final int COVER_HEIGHT
= 150;
68 private static final int SPINE_WIDTH
= 5;
69 private static final int SPINE_HEIGHT
= 5;
70 private static final int HOFFSET
= 20;
71 private static final Color SPINE_COLOR_BOTTOM
= new Color(180, 180, 180);
72 private static final Color SPINE_COLOR_RIGHT
= new Color(100, 100, 100);
73 private static final int TEXT_WIDTH
= COVER_WIDTH
+ 40;
74 private static final int TEXT_HEIGHT
= 50;
75 private static final String AUTHOR_COLOR
= "#888888";
76 private static final Color BORDER
= Color
.black
;
77 private static final long doubleClickDelay
= 200; // in ms
82 private boolean selected
;
83 private boolean hovered
;
84 private Date lastClick
;
86 private List
<BookActionListener
> listeners
;
88 private boolean cached
;
91 * Create a new {@link LocalReaderBook} item for the givn {@link Story}.
94 * the story {@code}link MetaData}
96 * TRUE if it is locally cached
98 public LocalReaderBook(MetaData meta
, boolean cached
) {
99 this.luid
= meta
.getLuid();
100 this.cached
= cached
;
102 BufferedImage resizedImage
= new BufferedImage(SPINE_WIDTH
103 + COVER_WIDTH
, SPINE_HEIGHT
+ COVER_HEIGHT
+ HOFFSET
,
104 BufferedImage
.TYPE_4BYTE_ABGR
);
105 Graphics2D g
= resizedImage
.createGraphics();
106 g
.setColor(Color
.white
);
107 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
108 if (meta
.getCover() != null) {
109 g
.drawImage(meta
.getCover(), 0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
,
112 g
.setColor(Color
.black
);
113 g
.drawLine(0, HOFFSET
, COVER_WIDTH
, HOFFSET
+ COVER_HEIGHT
);
114 g
.drawLine(COVER_WIDTH
, HOFFSET
, 0, HOFFSET
+ COVER_HEIGHT
);
118 icon
= new JLabel(new ImageIcon(resizedImage
));
120 String optAuthor
= meta
.getAuthor();
121 if (optAuthor
!= null && !optAuthor
.isEmpty()) {
122 optAuthor
= "(" + optAuthor
+ ")";
128 + "<body style='width: %d px; height: %d px; text-align: center'>"
129 + "%s" + "<br>" + "<span style='color: %s;'>"
130 + "%s" + "</span>" + "</body>" + "</html>",
131 TEXT_WIDTH
, TEXT_HEIGHT
, meta
.getTitle(), AUTHOR_COLOR
,
134 this.setLayout(new BorderLayout(10, 10));
135 this.add(icon
, BorderLayout
.CENTER
);
136 this.add(title
, BorderLayout
.SOUTH
);
143 * The book current selection state.
145 * @return the selection state
147 public boolean isSelected() {
152 * The book current selection state.
155 * TRUE if it is selected
157 public void setSelected(boolean selected
) {
158 this.selected
= selected
;
163 * The item mouse-hover state.
166 * TRUE if it is mouse-hovered
168 private void setHovered(boolean hovered
) {
169 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 UID of the book represented by this item.
248 public String
getLuid() {
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 this.cached
= cached
;
272 * Draw a "cached" icon and a partially transparent overlay if needed
273 * depending upon the selection and mouse-hover states on top of the normal
277 public void paint(Graphics g
) {
280 Rectangle clip
= g
.getClipBounds();
281 if (clip
.getWidth() <= 0 || clip
.getHeight() <= 0) {
285 int h
= COVER_HEIGHT
;
287 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 1;
288 int yOffset
= HOFFSET
;
290 if (BORDER
!= null) {
291 if (BORDER
!= null) {
293 g
.drawRect(xOffset
, yOffset
, COVER_WIDTH
, COVER_HEIGHT
);
300 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
301 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
302 int[] ys
= new int[] { yOffset
+ h
, yOffset
+ h
+ SPINE_HEIGHT
,
303 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
304 g
.setColor(SPINE_COLOR_BOTTOM
);
305 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
306 xs
= new int[] { xOffset
+ w
, xOffset
+ w
+ SPINE_WIDTH
,
307 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
308 ys
= new int[] { yOffset
, yOffset
+ SPINE_HEIGHT
,
309 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
310 g
.setColor(SPINE_COLOR_RIGHT
);
311 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
313 Color color
= new Color(255, 255, 255, 0);
315 } else if (selected
&& !hovered
) {
316 color
= new Color(80, 80, 100, 40);
317 } else if (!selected
&& hovered
) {
318 color
= new Color(230, 230, 255, 100);
319 } else if (selected
&& hovered
) {
320 color
= new Color(200, 200, 255, 100);
324 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);
327 g
.setColor(Color
.green
);
328 g
.fillOval(COVER_WIDTH
+ HOFFSET
+ 30, 10, 20, 20);