allow pagination for search keywords
[nikiroo-utils.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.Component;
5 import java.awt.Graphics;
6 import java.awt.event.MouseEvent;
7 import java.awt.event.MouseListener;
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.EventListener;
11 import java.util.List;
12
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15
16 import be.nikiroo.fanfix.data.Story;
17 import be.nikiroo.fanfix.reader.Reader;
18
19 /**
20 * A book item presented in a {@link GuiReaderFrame}.
21 *
22 * @author niki
23 */
24 class GuiReaderBook extends JPanel {
25 /**
26 * Action on a book item.
27 *
28 * @author niki
29 */
30 interface BookActionListener extends EventListener {
31 /**
32 * The book was selected (single click).
33 *
34 * @param book
35 * the {@link GuiReaderBook} itself
36 */
37 public void select(GuiReaderBook book);
38
39 /**
40 * The book was double-clicked.
41 *
42 * @param book
43 * the {@link GuiReaderBook} itself
44 */
45 public void action(GuiReaderBook book);
46
47 /**
48 * A popup menu was requested for this {@link GuiReaderBook}.
49 *
50 * @param book
51 * the {@link GuiReaderBook} itself
52 * @param target
53 * the target component for the popup
54 * @param x
55 * the X position of the click/request (in case of popup
56 * request from the keyboard, the center of the target is
57 * selected as point of reference)
58 * @param y
59 * the Y position of the click/request (in case of popup
60 * request from the keyboard, the center of the target is
61 * selected as point of reference)
62 */
63 public void popupRequested(GuiReaderBook book, Component target, int x,
64 int y);
65 }
66
67 private static final long serialVersionUID = 1L;
68
69 private static final String AUTHOR_COLOR = "#888888";
70 private static final long doubleClickDelay = 200; // in ms
71
72 private JLabel icon;
73 private JLabel title;
74 private boolean selected;
75 private boolean hovered;
76 private Date lastClick;
77
78 private List<BookActionListener> listeners;
79 private GuiReaderBookInfo info;
80 private boolean cached;
81 private boolean seeWordCount;
82
83 /**
84 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
85 *
86 * @param reader
87 * the associated reader
88 * @param info
89 * the information about the story to represent
90 * @param cached
91 * TRUE if it is locally cached
92 * @param seeWordCount
93 * TRUE to see word counts, FALSE to see authors
94 */
95 public GuiReaderBook(Reader reader, GuiReaderBookInfo info, boolean cached,
96 boolean seeWordCount) {
97 this.info = info;
98 this.cached = cached;
99 this.seeWordCount = seeWordCount;
100
101 icon = new JLabel(GuiReaderCoverImager.generateCoverIcon(
102 reader.getLibrary(), info));
103
104 title = new JLabel();
105 updateTitle();
106
107 setLayout(new BorderLayout(10, 10));
108 add(icon, BorderLayout.CENTER);
109 add(title, BorderLayout.SOUTH);
110
111 setupListeners();
112 }
113
114 /**
115 * The book current selection state.
116 *
117 * @return the selection state
118 */
119 public boolean isSelected() {
120 return selected;
121 }
122
123 /**
124 * The book current selection state.
125 * <p>
126 * Setting this value to true can cause a "select" action to occur if the
127 * previous state was "unselected".
128 *
129 * @param selected
130 * TRUE if it is selected
131 */
132 public void setSelected(boolean selected) {
133 if (this.selected != selected) {
134 this.selected = selected;
135 repaint();
136
137 if (selected) {
138 select();
139 }
140 }
141 }
142
143 /**
144 * The item mouse-hover state.
145 *
146 * @return TRUE if it is mouse-hovered
147 */
148 public boolean isHovered() {
149 return this.hovered;
150 }
151
152 /**
153 * The item mouse-hover state.
154 *
155 * @param hovered
156 * TRUE if it is mouse-hovered
157 */
158 public void setHovered(boolean hovered) {
159 if (this.hovered != hovered) {
160 this.hovered = hovered;
161 repaint();
162 }
163 }
164
165 /**
166 * Setup the mouse listener that will activate {@link BookActionListener}
167 * events.
168 */
169 private void setupListeners() {
170 listeners = new ArrayList<GuiReaderBook.BookActionListener>();
171 addMouseListener(new MouseListener() {
172 @Override
173 public void mouseReleased(MouseEvent e) {
174 if (isEnabled() && e.isPopupTrigger()) {
175 popup(e);
176 }
177 }
178
179 @Override
180 public void mousePressed(MouseEvent e) {
181 if (isEnabled() && e.isPopupTrigger()) {
182 popup(e);
183 }
184 }
185
186 @Override
187 public void mouseExited(MouseEvent e) {
188 setHovered(false);
189 }
190
191 @Override
192 public void mouseEntered(MouseEvent e) {
193 setHovered(true);
194 }
195
196 @Override
197 public void mouseClicked(MouseEvent e) {
198 if (isEnabled()) {
199 Date now = new Date();
200 if (lastClick != null
201 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
202 click(true);
203 } else {
204 click(false);
205 }
206
207 lastClick = now;
208 e.consume();
209 }
210 }
211
212 private void click(boolean doubleClick) {
213 if (doubleClick) {
214 action();
215 } else {
216 select();
217 }
218 }
219
220 private void popup(MouseEvent e) {
221 GuiReaderBook.this
222 .popup(GuiReaderBook.this, e.getX(), e.getY());
223 e.consume();
224 }
225 });
226 }
227
228 /**
229 * Add a new {@link BookActionListener} on this item.
230 *
231 * @param listener
232 * the listener
233 */
234 public void addActionListener(BookActionListener listener) {
235 listeners.add(listener);
236 }
237
238 /**
239 * Cause an action to occur on this {@link GuiReaderBook}.
240 */
241 public void action() {
242 for (BookActionListener listener : listeners) {
243 listener.action(GuiReaderBook.this);
244 }
245 }
246
247 /**
248 * Cause a select event on this {@link GuiReaderBook}.
249 * <p>
250 * Have a look at {@link GuiReaderBook#setSelected(boolean)}.
251 */
252 private void select() {
253 for (BookActionListener listener : listeners) {
254 listener.select(GuiReaderBook.this);
255 }
256 }
257
258 /**
259 * Request a popup.
260 *
261 * @param target
262 * the target component for the popup
263 * @param x
264 * the X position of the click/request (in case of popup request
265 * from the keyboard, the center of the target should be selected
266 * as point of reference)
267 * @param y
268 * the Y position of the click/request (in case of popup request
269 * from the keyboard, the center of the target should be selected
270 * as point of reference)
271 */
272 public void popup(Component target, int x, int y) {
273 for (BookActionListener listener : listeners) {
274 listener.select((GuiReaderBook.this));
275 listener.popupRequested(GuiReaderBook.this, target, x, y);
276 }
277 }
278
279 /**
280 * The information about the book represented by this item.
281 *
282 * @return the meta
283 */
284 public GuiReaderBookInfo getInfo() {
285 return info;
286 }
287
288 /**
289 * This item {@link GuiReader} library cache state.
290 *
291 * @return TRUE if it is present in the {@link GuiReader} cache
292 */
293 public boolean isCached() {
294 return cached;
295 }
296
297 /**
298 * This item {@link GuiReader} library cache state.
299 *
300 * @param cached
301 * TRUE if it is present in the {@link GuiReader} cache
302 */
303 public void setCached(boolean cached) {
304 if (this.cached != cached) {
305 this.cached = cached;
306 repaint();
307 }
308 }
309
310 /**
311 * Update the title, paint the item, then call
312 * {@link GuiReaderCoverImager#paintOverlay(Graphics, boolean, boolean, boolean, boolean)}
313 * .
314 */
315 @Override
316 public void paint(Graphics g) {
317 updateTitle();
318 super.paint(g);
319 GuiReaderCoverImager.paintOverlay(g, isEnabled(), isSelected(),
320 isHovered(), isCached());
321 }
322
323 /**
324 * Update the title with the currently registered information.
325 */
326 private void updateTitle() {
327 String optSecondary = info.getSecondaryInfo(seeWordCount);
328 title.setText(String
329 .format("<html>"
330 + "<body style='width: %d px; height: %d px; text-align: center'>"
331 + "%s" + "<br>" + "<span style='color: %s;'>" + "%s"
332 + "</span>" + "</body>" + "</html>",
333 GuiReaderCoverImager.TEXT_WIDTH,
334 GuiReaderCoverImager.TEXT_HEIGHT, info.getMainInfo(),
335 AUTHOR_COLOR, optSecondary));
336 }
337 }