1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Graphics
;
5 import java
.awt
.event
.MouseEvent
;
6 import java
.awt
.event
.MouseListener
;
7 import java
.util
.ArrayList
;
9 import java
.util
.EventListener
;
10 import java
.util
.List
;
12 import javax
.swing
.JLabel
;
13 import javax
.swing
.JPanel
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
16 import be
.nikiroo
.fanfix
.reader
.Reader
;
19 * A book item presented in a {@link GuiReaderFrame}.
23 class GuiReaderBook
extends JPanel
{
25 * Action on a book item.
29 interface BookActionListener
extends EventListener
{
31 * The book was selected (single click).
34 * the {@link GuiReaderBook} itself
36 public void select(GuiReaderBook book
);
39 * The book was double-clicked.
42 * the {@link GuiReaderBook} itself
44 public void action(GuiReaderBook book
);
47 * A popup menu was requested for this {@link GuiReaderBook}.
50 * the {@link GuiReaderBook} itself
52 * the {@link MouseEvent} that generated this call
54 public void popupRequested(GuiReaderBook book
, MouseEvent e
);
57 private static final long serialVersionUID
= 1L;
59 private static final String AUTHOR_COLOR
= "#888888";
60 private static final long doubleClickDelay
= 200; // in ms
64 private boolean selected
;
65 private boolean hovered
;
66 private Date lastClick
;
68 private List
<BookActionListener
> listeners
;
69 private GuiReaderBookInfo info
;
70 private boolean cached
;
73 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
76 * the associated reader
78 * the information about the story to represent
80 * TRUE if it is locally cached
82 * TRUE to see word counts, FALSE to see authors
84 public GuiReaderBook(Reader reader
, GuiReaderBookInfo info
, boolean cached
,
85 boolean seeWordCount
) {
89 String optSecondary
= info
.getSecondaryInfo(seeWordCount
);
91 icon
= new JLabel(GuiReaderCoverImager
.generateCoverIcon(
92 reader
.getLibrary(), info
));
96 + "<body style='width: %d px; height: %d px; text-align: center'>"
97 + "%s" + "<br>" + "<span style='color: %s;'>"
98 + "%s" + "</span>" + "</body>" + "</html>",
99 GuiReaderCoverImager
.TEXT_WIDTH
,
100 GuiReaderCoverImager
.TEXT_HEIGHT
, info
.getMainInfo(),
101 AUTHOR_COLOR
, optSecondary
));
103 setLayout(new BorderLayout(10, 10));
104 add(icon
, BorderLayout
.CENTER
);
105 add(title
, BorderLayout
.SOUTH
);
111 * The book current selection state.
113 * @return the selection state
115 public boolean isSelected() {
120 * The book current selection state.
123 * TRUE if it is selected
125 public void setSelected(boolean selected
) {
126 if (this.selected
!= selected
) {
127 this.selected
= selected
;
133 * The item mouse-hover state.
135 * @return TRUE if it is mouse-hovered
137 private boolean isHovered() {
142 * The item mouse-hover state.
145 * TRUE if it is mouse-hovered
147 private void setHovered(boolean hovered
) {
148 if (this.hovered
!= hovered
) {
149 this.hovered
= hovered
;
155 * Setup the mouse listener that will activate {@link BookActionListener}
158 private void setupListeners() {
159 listeners
= new ArrayList
<GuiReaderBook
.BookActionListener
>();
160 addMouseListener(new MouseListener() {
162 public void mouseReleased(MouseEvent e
) {
163 if (e
.isPopupTrigger()) {
169 public void mousePressed(MouseEvent e
) {
170 if (e
.isPopupTrigger()) {
176 public void mouseExited(MouseEvent e
) {
181 public void mouseEntered(MouseEvent e
) {
186 public void mouseClicked(MouseEvent e
) {
188 Date now
= new Date();
189 if (lastClick
!= null
190 && now
.getTime() - lastClick
.getTime() < doubleClickDelay
) {
200 private void click(boolean doubleClick
) {
201 for (BookActionListener listener
: listeners
) {
203 listener
.action(GuiReaderBook
.this);
205 listener
.select(GuiReaderBook
.this);
210 private void popup(MouseEvent e
) {
211 for (BookActionListener listener
: listeners
) {
212 listener
.select((GuiReaderBook
.this));
213 listener
.popupRequested(GuiReaderBook
.this, e
);
220 * Add a new {@link BookActionListener} on this item.
225 public void addActionListener(BookActionListener listener
) {
226 listeners
.add(listener
);
230 * The information about the book represented by this item.
234 public GuiReaderBookInfo
getInfo() {
239 * This item {@link GuiReader} library cache state.
241 * @return TRUE if it is present in the {@link GuiReader} cache
243 public boolean isCached() {
248 * This item {@link GuiReader} library cache state.
251 * TRUE if it is present in the {@link GuiReader} cache
253 public void setCached(boolean cached
) {
254 if (this.cached
!= cached
) {
255 this.cached
= cached
;
261 * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
264 public void paint(Graphics g
) {
266 GuiReaderCoverImager
.paintOverlay(g
, isEnabled(), isSelected(),
267 isHovered(), isCached());