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 private static final int COVER_WIDTH
= 100;
53 private static final int COVER_HEIGHT
= 150;
54 private static final int SPINE_WIDTH
= 5;
55 private static final int SPINE_HEIGHT
= 5;
56 private static final int HOFFSET
= 20;
57 private static final Color SPINE_COLOR_BOTTOM
= new Color(180, 180, 180);
58 private static final Color SPINE_COLOR_RIGHT
= new Color(100, 100, 100);
59 private static final int TEXT_WIDTH
= COVER_WIDTH
+ 40;
60 private static final int TEXT_HEIGHT
= 50;
61 private static final String AUTHOR_COLOR
= "#888888";
63 private static final long serialVersionUID
= 1L;
66 private boolean selected
;
67 private boolean hovered
;
68 private Date lastClick
;
69 private long doubleClickDelay
= 200; // in ms
70 private List
<BookActionListener
> listeners
;
72 public LocalReaderBook(MetaData meta
) {
73 if (meta
.getCover() != null) {
74 BufferedImage resizedImage
= new BufferedImage(SPINE_WIDTH
75 + COVER_WIDTH
, SPINE_HEIGHT
+ COVER_HEIGHT
+ HOFFSET
,
76 BufferedImage
.TYPE_4BYTE_ABGR
);
77 Graphics2D g
= resizedImage
.createGraphics();
78 g
.setColor(Color
.white
);
79 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
80 g
.drawImage(meta
.getCover(), 0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
,
84 icon
= new JLabel(new ImageIcon(resizedImage
));
86 // TODO: a big black "X" ?
87 icon
= new JLabel(" [ no cover ] ");
90 String optAuthor
= meta
.getAuthor();
91 if (optAuthor
!= null && !optAuthor
.isEmpty()) {
92 optAuthor
= "(" + optAuthor
+ ")";
97 + "<body style='width: %d px; height: %d px; text-align: center'>"
98 + "%s" + "<br>" + "<span style='color: %s;'>"
99 + "%s" + "</span>" + "</body>" + "</html>",
100 TEXT_WIDTH
, TEXT_HEIGHT
, meta
.getTitle(), AUTHOR_COLOR
,
103 this.setLayout(new BorderLayout(10, 10));
104 this.add(icon
, BorderLayout
.CENTER
);
105 this.add(tt
, BorderLayout
.SOUTH
);
112 * The book current selection state.
114 * @return the selected
116 public boolean isSelected() {
121 * The book current selection state.
124 * the selected to set
126 public void setSelected(boolean selected
) {
127 this.selected
= selected
;
131 private void setHovered(boolean hovered
) {
132 this.hovered
= hovered
;
136 private void setupListeners() {
137 listeners
= new ArrayList
<LocalReaderBook
.BookActionListener
>();
138 addMouseListener(new MouseListener() {
139 public void mouseReleased(MouseEvent e
) {
142 public void mousePressed(MouseEvent e
) {
145 public void mouseExited(MouseEvent e
) {
149 public void mouseEntered(MouseEvent e
) {
153 public void mouseClicked(MouseEvent e
) {
154 Date now
= new Date();
155 if (lastClick
!= null
156 && now
.getTime() - lastClick
.getTime() < doubleClickDelay
) {
166 private void click(boolean doubleClick
) {
167 for (BookActionListener listener
: listeners
) {
169 listener
.action(this);
171 listener
.select(this);
176 public void addActionListener(BookActionListener listener
) {
177 listeners
.add(listener
);
181 public void paint(Graphics g
) {
184 int h
= COVER_HEIGHT
;
186 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 4;
188 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
189 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
190 int[] ys
= new int[] { HOFFSET
+ h
, HOFFSET
+ h
+ SPINE_HEIGHT
,
191 HOFFSET
+ h
+ SPINE_HEIGHT
, HOFFSET
+ h
};
192 g
.setColor(SPINE_COLOR_BOTTOM
);
193 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
194 xs
= new int[] { xOffset
+ w
, xOffset
+ w
+ SPINE_WIDTH
,
195 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
196 ys
= new int[] { HOFFSET
, HOFFSET
+ SPINE_HEIGHT
,
197 HOFFSET
+ h
+ SPINE_HEIGHT
, HOFFSET
+ h
};
198 g
.setColor(SPINE_COLOR_RIGHT
);
199 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
201 Color color
= new Color(255, 255, 255, 0);
202 if (selected
&& !hovered
) {
203 color
= new Color(80, 80, 100, 40);
204 } else if (!selected
&& hovered
) {
205 color
= new Color(230, 230, 255, 100);
206 } else if (selected
&& hovered
) {
207 color
= new Color(200, 200, 255, 100);
210 Rectangle clip
= g
.getClipBounds();
212 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);