gui: add support for author cover
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBook.java
1 package be.nikiroo.fanfix.reader.ui;
2
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;
8 import java.util.Date;
9 import java.util.EventListener;
10 import java.util.List;
11
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14
15 import be.nikiroo.fanfix.data.Story;
16 import be.nikiroo.fanfix.reader.Reader;
17
18 /**
19 * A book item presented in a {@link GuiReaderFrame}.
20 *
21 * @author niki
22 */
23 class GuiReaderBook extends JPanel {
24 /**
25 * Action on a book item.
26 *
27 * @author niki
28 */
29 interface BookActionListener extends EventListener {
30 /**
31 * The book was selected (single click).
32 *
33 * @param book
34 * the {@link GuiReaderBook} itself
35 */
36 public void select(GuiReaderBook book);
37
38 /**
39 * The book was double-clicked.
40 *
41 * @param book
42 * the {@link GuiReaderBook} itself
43 */
44 public void action(GuiReaderBook book);
45
46 /**
47 * A popup menu was requested for this {@link GuiReaderBook}.
48 *
49 * @param book
50 * the {@link GuiReaderBook} itself
51 * @param e
52 * the {@link MouseEvent} that generated this call
53 */
54 public void popupRequested(GuiReaderBook book, MouseEvent e);
55 }
56
57 private static final long serialVersionUID = 1L;
58
59 private static final String AUTHOR_COLOR = "#888888";
60 private static final long doubleClickDelay = 200; // in ms
61
62 private JLabel icon;
63 private JLabel title;
64 private boolean selected;
65 private boolean hovered;
66 private Date lastClick;
67
68 private List<BookActionListener> listeners;
69 private GuiReaderBookInfo info;
70 private boolean cached;
71
72 /**
73 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
74 *
75 * @param reader
76 * the associated reader
77 * @param info
78 * the information about the story to represent
79 * @param cached
80 * TRUE if it is locally cached
81 * @param seeWordCount
82 * TRUE to see word counts, FALSE to see authors
83 */
84 public GuiReaderBook(Reader reader, GuiReaderBookInfo info, boolean cached,
85 boolean seeWordCount) {
86 this.cached = cached;
87 this.info = info;
88
89 String optSecondary = info.getSecondaryInfo(seeWordCount);
90
91 icon = new JLabel(GuiReaderCoverImager.generateCoverIcon(
92 reader.getLibrary(), info));
93 title = new JLabel(
94 String.format(
95 "<html>"
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));
102
103 setLayout(new BorderLayout(10, 10));
104 add(icon, BorderLayout.CENTER);
105 add(title, BorderLayout.SOUTH);
106
107 setupListeners();
108 }
109
110 /**
111 * The book current selection state.
112 *
113 * @return the selection state
114 */
115 public boolean isSelected() {
116 return selected;
117 }
118
119 /**
120 * The book current selection state.
121 *
122 * @param selected
123 * TRUE if it is selected
124 */
125 public void setSelected(boolean selected) {
126 if (this.selected != selected) {
127 this.selected = selected;
128 repaint();
129 }
130 }
131
132 /**
133 * The item mouse-hover state.
134 *
135 * @return TRUE if it is mouse-hovered
136 */
137 private boolean isHovered() {
138 return this.hovered;
139 }
140
141 /**
142 * The item mouse-hover state.
143 *
144 * @param hovered
145 * TRUE if it is mouse-hovered
146 */
147 private void setHovered(boolean hovered) {
148 if (this.hovered != hovered) {
149 this.hovered = hovered;
150 repaint();
151 }
152 }
153
154 /**
155 * Setup the mouse listener that will activate {@link BookActionListener}
156 * events.
157 */
158 private void setupListeners() {
159 listeners = new ArrayList<GuiReaderBook.BookActionListener>();
160 addMouseListener(new MouseListener() {
161 @Override
162 public void mouseReleased(MouseEvent e) {
163 if (e.isPopupTrigger()) {
164 popup(e);
165 }
166 }
167
168 @Override
169 public void mousePressed(MouseEvent e) {
170 if (e.isPopupTrigger()) {
171 popup(e);
172 }
173 }
174
175 @Override
176 public void mouseExited(MouseEvent e) {
177 setHovered(false);
178 }
179
180 @Override
181 public void mouseEntered(MouseEvent e) {
182 setHovered(true);
183 }
184
185 @Override
186 public void mouseClicked(MouseEvent e) {
187 if (isEnabled()) {
188 Date now = new Date();
189 if (lastClick != null
190 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
191 click(true);
192 } else {
193 click(false);
194 }
195
196 lastClick = now;
197 }
198 }
199
200 private void click(boolean doubleClick) {
201 for (BookActionListener listener : listeners) {
202 if (doubleClick) {
203 listener.action(GuiReaderBook.this);
204 } else {
205 listener.select(GuiReaderBook.this);
206 }
207 }
208 }
209
210 private void popup(MouseEvent e) {
211 for (BookActionListener listener : listeners) {
212 listener.select((GuiReaderBook.this));
213 listener.popupRequested(GuiReaderBook.this, e);
214 }
215 }
216 });
217 }
218
219 /**
220 * Add a new {@link BookActionListener} on this item.
221 *
222 * @param listener
223 * the listener
224 */
225 public void addActionListener(BookActionListener listener) {
226 listeners.add(listener);
227 }
228
229 /**
230 * The information about the book represented by this item.
231 *
232 * @return the meta
233 */
234 public GuiReaderBookInfo getInfo() {
235 return info;
236 }
237
238 /**
239 * This item {@link GuiReader} library cache state.
240 *
241 * @return TRUE if it is present in the {@link GuiReader} cache
242 */
243 public boolean isCached() {
244 return cached;
245 }
246
247 /**
248 * This item {@link GuiReader} library cache state.
249 *
250 * @param cached
251 * TRUE if it is present in the {@link GuiReader} cache
252 */
253 public void setCached(boolean cached) {
254 if (this.cached != cached) {
255 this.cached = cached;
256 repaint();
257 }
258 }
259
260 /**
261 * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}.
262 */
263 @Override
264 public void paint(Graphics g) {
265 super.paint(g);
266 GuiReaderCoverImager.paintOverlay(g, isEnabled(), isSelected(),
267 isHovered(), isCached());
268 }
269 }