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
;
24 * A book item presented in a {@link LocalReaderFrame}.
28 class LocalReaderBook
extends JPanel
{
30 * Action on a book item.
34 interface BookActionListener
extends EventListener
{
36 * The book was selected (single click).
39 * the {@link LocalReaderBook} itself
41 public void select(LocalReaderBook book
);
44 * The book was double-clicked.
47 * the {@link LocalReaderBook} itself
49 public void action(LocalReaderBook book
);
52 * A popup menu was requested for this {@link LocalReaderBook}.
55 * the {@link LocalReaderBook} itself
57 * the {@link MouseEvent} that generated this call
59 public void popupRequested(LocalReaderBook book
, MouseEvent e
);
62 private static final int COVER_WIDTH
= 100;
63 private static final int COVER_HEIGHT
= 150;
64 private static final int SPINE_WIDTH
= 5;
65 private static final int SPINE_HEIGHT
= 5;
66 private static final int HOFFSET
= 20;
67 private static final Color SPINE_COLOR_BOTTOM
= new Color(180, 180, 180);
68 private static final Color SPINE_COLOR_RIGHT
= new Color(100, 100, 100);
69 private static final int TEXT_WIDTH
= COVER_WIDTH
+ 40;
70 private static final int TEXT_HEIGHT
= 50;
71 private static final String AUTHOR_COLOR
= "#888888";
72 private static final long serialVersionUID
= 1L;
76 private boolean selected
;
77 private boolean hovered
;
78 private Date lastClick
;
79 private long doubleClickDelay
= 200; // in ms
80 private List
<BookActionListener
> listeners
;
82 private boolean cached
;
84 public LocalReaderBook(MetaData meta
, boolean cached
) {
85 this.luid
= meta
.getLuid();
88 if (meta
.getCover() != null) {
89 BufferedImage resizedImage
= new BufferedImage(SPINE_WIDTH
90 + COVER_WIDTH
, SPINE_HEIGHT
+ COVER_HEIGHT
+ HOFFSET
,
91 BufferedImage
.TYPE_4BYTE_ABGR
);
92 Graphics2D g
= resizedImage
.createGraphics();
93 g
.setColor(Color
.white
);
94 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
95 g
.drawImage(meta
.getCover(), 0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
,
99 icon
= new JLabel(new ImageIcon(resizedImage
));
101 // TODO: a big black "X" ?
102 icon
= new JLabel(" [ no cover ] ");
105 String optAuthor
= meta
.getAuthor();
106 if (optAuthor
!= null && !optAuthor
.isEmpty()) {
107 optAuthor
= "(" + optAuthor
+ ")";
112 + "<body style='width: %d px; height: %d px; text-align: center'>"
113 + "%s" + "<br>" + "<span style='color: %s;'>"
114 + "%s" + "</span>" + "</body>" + "</html>",
115 TEXT_WIDTH
, TEXT_HEIGHT
, meta
.getTitle(), AUTHOR_COLOR
,
118 this.setLayout(new BorderLayout(10, 10));
119 this.add(icon
, BorderLayout
.CENTER
);
120 this.add(title
, BorderLayout
.SOUTH
);
127 * The book current selection state.
129 * @return the selected
131 public boolean isSelected() {
136 * The book current selection state.
139 * the selected to set
141 public void setSelected(boolean selected
) {
142 this.selected
= selected
;
146 private void setHovered(boolean hovered
) {
147 this.hovered
= hovered
;
151 private void setupListeners() {
152 listeners
= new ArrayList
<LocalReaderBook
.BookActionListener
>();
153 addMouseListener(new MouseListener() {
154 public void mouseReleased(MouseEvent e
) {
155 if (e
.isPopupTrigger()) {
160 public void mousePressed(MouseEvent e
) {
161 if (e
.isPopupTrigger()) {
166 public void mouseExited(MouseEvent e
) {
170 public void mouseEntered(MouseEvent e
) {
174 public void mouseClicked(MouseEvent e
) {
176 Date now
= new Date();
177 if (lastClick
!= null
178 && now
.getTime() - lastClick
.getTime() < doubleClickDelay
) {
188 private void click(boolean doubleClick
) {
189 for (BookActionListener listener
: listeners
) {
191 listener
.action(LocalReaderBook
.this);
193 listener
.select(LocalReaderBook
.this);
198 private void popup(MouseEvent e
) {
199 for (BookActionListener listener
: listeners
) {
200 listener
.select((LocalReaderBook
.this));
201 listener
.popupRequested(LocalReaderBook
.this, e
);
207 public void addActionListener(BookActionListener listener
) {
208 listeners
.add(listener
);
211 public String
getLuid() {
216 * This boos is cached into the {@link LocalReader} library.
220 public boolean isCached() {
225 * This boos is cached into the {@link LocalReader} library.
230 public void setCached(boolean cached
) {
231 this.cached
= cached
;
235 public void paint(Graphics g
) {
238 int h
= COVER_HEIGHT
;
240 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 4;
242 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
243 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
244 int[] ys
= new int[] { HOFFSET
+ h
, HOFFSET
+ h
+ SPINE_HEIGHT
,
245 HOFFSET
+ h
+ SPINE_HEIGHT
, HOFFSET
+ h
};
246 g
.setColor(SPINE_COLOR_BOTTOM
);
247 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
248 xs
= new int[] { xOffset
+ w
, xOffset
+ w
+ SPINE_WIDTH
,
249 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
250 ys
= new int[] { HOFFSET
, HOFFSET
+ SPINE_HEIGHT
,
251 HOFFSET
+ h
+ SPINE_HEIGHT
, HOFFSET
+ h
};
252 g
.setColor(SPINE_COLOR_RIGHT
);
253 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
255 Color color
= new Color(255, 255, 255, 0);
257 } else if (selected
&& !hovered
) {
258 color
= new Color(80, 80, 100, 40);
259 } else if (!selected
&& hovered
) {
260 color
= new Color(230, 230, 255, 100);
261 } else if (selected
&& hovered
) {
262 color
= new Color(200, 200, 255, 100);
265 Rectangle clip
= g
.getClipBounds();
267 g
.setColor(Color
.green
);
268 g
.fillOval(clip
.x
+ clip
.width
- 30, 10, 20, 20);
272 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);