728964baa3faa82676f3b7a6fb6c875b6529bebf
[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 private boolean seeWordCount;
72
73 /**
74 * Create a new {@link GuiReaderBook} item for the given {@link Story}.
75 *
76 * @param reader
77 * the associated reader
78 * @param info
79 * the information about the story to represent
80 * @param cached
81 * TRUE if it is locally cached
82 * @param seeWordCount
83 * TRUE to see word counts, FALSE to see authors
84 */
85 public GuiReaderBook(Reader reader, GuiReaderBookInfo info, boolean cached,
86 boolean seeWordCount) {
87 this.info = info;
88 this.cached = cached;
89 this.seeWordCount = seeWordCount;
90
91 icon = new JLabel(GuiReaderCoverImager.generateCoverIcon(
92 reader.getLibrary(), info));
93
94 title = new JLabel();
95 updateTitle();
96
97 setLayout(new BorderLayout(10, 10));
98 add(icon, BorderLayout.CENTER);
99 add(title, BorderLayout.SOUTH);
100
101 setupListeners();
102 }
103
104 /**
105 * The book current selection state.
106 *
107 * @return the selection state
108 */
109 public boolean isSelected() {
110 return selected;
111 }
112
113 /**
114 * The book current selection state.
115 * <p>
116 * Setting this value to true can cause a "select" action to occur if the
117 * previous state was "unselected".
118 *
119 * @param selected
120 * TRUE if it is selected
121 */
122 public void setSelected(boolean selected) {
123 if (this.selected != selected) {
124 this.selected = selected;
125 repaint();
126
127 if (selected) {
128 select();
129 }
130 }
131 }
132
133 /**
134 * The item mouse-hover state.
135 *
136 * @return TRUE if it is mouse-hovered
137 */
138 private boolean isHovered() {
139 return this.hovered;
140 }
141
142 /**
143 * The item mouse-hover state.
144 *
145 * @param hovered
146 * TRUE if it is mouse-hovered
147 */
148 private void setHovered(boolean hovered) {
149 if (this.hovered != hovered) {
150 this.hovered = hovered;
151 repaint();
152 }
153 }
154
155 /**
156 * Setup the mouse listener that will activate {@link BookActionListener}
157 * events.
158 */
159 private void setupListeners() {
160 listeners = new ArrayList<GuiReaderBook.BookActionListener>();
161 addMouseListener(new MouseListener() {
162 @Override
163 public void mouseReleased(MouseEvent e) {
164 if (isEnabled() && e.isPopupTrigger()) {
165 popup(e);
166 }
167 }
168
169 @Override
170 public void mousePressed(MouseEvent e) {
171 if (isEnabled() && e.isPopupTrigger()) {
172 popup(e);
173 }
174 }
175
176 @Override
177 public void mouseExited(MouseEvent e) {
178 setHovered(false);
179 }
180
181 @Override
182 public void mouseEntered(MouseEvent e) {
183 setHovered(true);
184 }
185
186 @Override
187 public void mouseClicked(MouseEvent e) {
188 if (isEnabled()) {
189 Date now = new Date();
190 if (lastClick != null
191 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
192 click(true);
193 } else {
194 click(false);
195 }
196
197 lastClick = now;
198 e.consume();
199 }
200 }
201
202 private void click(boolean doubleClick) {
203 if (doubleClick) {
204 action();
205 } else {
206 select();
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 e.consume();
217 }
218 });
219 }
220
221 /**
222 * Add a new {@link BookActionListener} on this item.
223 *
224 * @param listener
225 * the listener
226 */
227 public void addActionListener(BookActionListener listener) {
228 listeners.add(listener);
229 }
230
231 /**
232 * Cause an action to occur on this {@link GuiReaderBook}.
233 */
234 public void action() {
235 for (BookActionListener listener : listeners) {
236 listener.action(GuiReaderBook.this);
237 }
238 }
239
240 /**
241 * Cause a select event on this {@link GuiReaderBook}.
242 */
243 private void select() {
244 for (BookActionListener listener : listeners) {
245 listener.select(GuiReaderBook.this);
246 }
247 }
248
249 /**
250 * The information about the book represented by this item.
251 *
252 * @return the meta
253 */
254 public GuiReaderBookInfo getInfo() {
255 return info;
256 }
257
258 /**
259 * This item {@link GuiReader} library cache state.
260 *
261 * @return TRUE if it is present in the {@link GuiReader} cache
262 */
263 public boolean isCached() {
264 return cached;
265 }
266
267 /**
268 * This item {@link GuiReader} library cache state.
269 *
270 * @param cached
271 * TRUE if it is present in the {@link GuiReader} cache
272 */
273 public void setCached(boolean cached) {
274 if (this.cached != cached) {
275 this.cached = cached;
276 repaint();
277 }
278 }
279
280 /**
281 * Update the title, paint the item, then call
282 * {@link GuiReaderCoverImager#paintOverlay(Graphics, boolean, boolean, boolean, boolean)}
283 * .
284 */
285 @Override
286 public void paint(Graphics g) {
287 updateTitle();
288 super.paint(g);
289 GuiReaderCoverImager.paintOverlay(g, isEnabled(), isSelected(),
290 isHovered(), isCached());
291 }
292
293 /**
294 * Update the title with the currently registered information.
295 */
296 private void updateTitle() {
297 String optSecondary = info.getSecondaryInfo(seeWordCount);
298 title.setText(String
299 .format("<html>"
300 + "<body style='width: %d px; height: %d px; text-align: center'>"
301 + "%s" + "<br>" + "<span style='color: %s;'>" + "%s"
302 + "</span>" + "</body>" + "</html>",
303 GuiReaderCoverImager.TEXT_WIDTH,
304 GuiReaderCoverImager.TEXT_HEIGHT, info.getMainInfo(),
305 AUTHOR_COLOR, optSecondary));
306 }
307 }