Wordcount (including UI), date of creation
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderBook.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
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;
16
17 import javax.swing.ImageIcon;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20
21 import be.nikiroo.fanfix.data.MetaData;
22 import be.nikiroo.fanfix.data.Story;
23 import be.nikiroo.utils.ui.UIUtils;
24
25 /**
26 * A book item presented in a {@link LocalReaderFrame}.
27 *
28 * @author niki
29 */
30 class LocalReaderBook extends JPanel {
31 /**
32 * Action on a book item.
33 *
34 * @author niki
35 */
36 interface BookActionListener extends EventListener {
37 /**
38 * The book was selected (single click).
39 *
40 * @param book
41 * the {@link LocalReaderBook} itself
42 */
43 public void select(LocalReaderBook book);
44
45 /**
46 * The book was double-clicked.
47 *
48 * @param book
49 * the {@link LocalReaderBook} itself
50 */
51 public void action(LocalReaderBook book);
52
53 /**
54 * A popup menu was requested for this {@link LocalReaderBook}.
55 *
56 * @param book
57 * the {@link LocalReaderBook} itself
58 * @param e
59 * the {@link MouseEvent} that generated this call
60 */
61 public void popupRequested(LocalReaderBook book, MouseEvent e);
62 }
63
64 private static final long serialVersionUID = 1L;
65
66 // TODO: export some of the configuration options?
67 private static final int COVER_WIDTH = 100;
68 private static final int COVER_HEIGHT = 150;
69 private static final int SPINE_WIDTH = 5;
70 private static final int SPINE_HEIGHT = 5;
71 private static final int HOFFSET = 20;
72 private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180);
73 private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100);
74 private static final int TEXT_WIDTH = COVER_WIDTH + 40;
75 private static final int TEXT_HEIGHT = 50;
76 private static final String AUTHOR_COLOR = "#888888";
77 private static final Color BORDER = Color.black;
78 private static final long doubleClickDelay = 200; // in ms
79 //
80
81 private JLabel icon;
82 private JLabel title;
83 private boolean selected;
84 private boolean hovered;
85 private Date lastClick;
86
87 private List<BookActionListener> listeners;
88 private MetaData meta;
89 private boolean cached;
90
91 /**
92 * Create a new {@link LocalReaderBook} item for the given {@link Story}.
93 *
94 * @param meta
95 * the story {@code}link MetaData}
96 * @param cached
97 * TRUE if it is locally cached
98 * @param seeWordcount
99 * TRUE to see word counts, FALSE to see authors
100 */
101 public LocalReaderBook(MetaData meta, boolean cached, boolean seeWordCount) {
102 this.cached = cached;
103 this.meta = meta;
104
105 String optSecondary = meta.getAuthor();
106 if (seeWordCount) {
107 if (meta.getWords() >= 4000) {
108 optSecondary = (meta.getWords() / 1000) + "k words";
109 } else if (meta.getWords() > 0) {
110 optSecondary = meta.getWords() + " words";
111 } else {
112 optSecondary = "empty";
113 }
114 }
115
116 if (optSecondary != null && !optSecondary.isEmpty()) {
117 optSecondary = "(" + optSecondary + ")";
118 }
119
120 icon = new JLabel(generateCoverIcon(meta.getCover()));
121
122 title = new JLabel(
123 String.format(
124 "<html>"
125 + "<body style='width: %d px; height: %d px; text-align: center'>"
126 + "%s" + "<br>" + "<span style='color: %s;'>"
127 + "%s" + "</span>" + "</body>" + "</html>",
128 TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
129 optSecondary));
130
131 setLayout(new BorderLayout(10, 10));
132 add(icon, BorderLayout.CENTER);
133 add(title, BorderLayout.SOUTH);
134
135 setupListeners();
136 }
137
138 /**
139 * The book current selection state.
140 *
141 * @return the selection state
142 */
143 public boolean isSelected() {
144 return selected;
145 }
146
147 /**
148 * The book current selection state.
149 *
150 * @param selected
151 * TRUE if it is selected
152 */
153 public void setSelected(boolean selected) {
154 if (this.selected != selected) {
155 this.selected = selected;
156 repaint();
157 }
158 }
159
160 /**
161 * The item mouse-hover state.
162 *
163 * @param hovered
164 * TRUE if it is mouse-hovered
165 */
166 private void setHovered(boolean hovered) {
167 if (this.hovered != hovered) {
168 this.hovered = hovered;
169 repaint();
170 }
171 }
172
173 /**
174 * Setup the mouse listener that will activate {@link BookActionListener}
175 * events.
176 */
177 private void setupListeners() {
178 listeners = new ArrayList<LocalReaderBook.BookActionListener>();
179 addMouseListener(new MouseListener() {
180 public void mouseReleased(MouseEvent e) {
181 if (e.isPopupTrigger()) {
182 popup(e);
183 }
184 }
185
186 public void mousePressed(MouseEvent e) {
187 if (e.isPopupTrigger()) {
188 popup(e);
189 }
190 }
191
192 public void mouseExited(MouseEvent e) {
193 setHovered(false);
194 }
195
196 public void mouseEntered(MouseEvent e) {
197 setHovered(true);
198 }
199
200 public void mouseClicked(MouseEvent e) {
201 if (isEnabled()) {
202 Date now = new Date();
203 if (lastClick != null
204 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
205 click(true);
206 } else {
207 click(false);
208 }
209
210 lastClick = now;
211 }
212 }
213
214 private void click(boolean doubleClick) {
215 for (BookActionListener listener : listeners) {
216 if (doubleClick) {
217 listener.action(LocalReaderBook.this);
218 } else {
219 listener.select(LocalReaderBook.this);
220 }
221 }
222 }
223
224 private void popup(MouseEvent e) {
225 for (BookActionListener listener : listeners) {
226 listener.select((LocalReaderBook.this));
227 listener.popupRequested(LocalReaderBook.this, e);
228 }
229 }
230 });
231 }
232
233 /**
234 * Add a new {@link BookActionListener} on this item.
235 *
236 * @param listener
237 * the listener
238 */
239 public void addActionListener(BookActionListener listener) {
240 listeners.add(listener);
241 }
242
243 /**
244 * The Library {@code}link MetaData} of the book represented by this item.
245 *
246 * @return the meta
247 */
248 public MetaData getMeta() {
249 return meta;
250 }
251
252 /**
253 * This item {@link LocalReader} library cache state.
254 *
255 * @return TRUE if it is present in the {@link LocalReader} cache
256 */
257 public boolean isCached() {
258 return cached;
259 }
260
261 /**
262 * This item {@link LocalReader} library cache state.
263 *
264 * @param cached
265 * TRUE if it is present in the {@link LocalReader} cache
266 */
267 public void setCached(boolean cached) {
268 if (this.cached != cached) {
269 this.cached = cached;
270 repaint();
271 }
272 }
273
274 /**
275 * Paint the item, then call {@link LocalReaderBook#paintOverlay(Graphics)}.
276 */
277 @Override
278 public void paint(Graphics g) {
279 super.paint(g);
280 paintOverlay(g);
281 }
282
283 /**
284 * Draw a partially transparent overlay if needed depending upon the
285 * selection and mouse-hover states on top of the normal component, as well
286 * as a possible "cached" icon if the item is cached.
287 */
288 public void paintOverlay(Graphics g) {
289 Rectangle clip = g.getClipBounds();
290 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
291 return;
292 }
293
294 int h = COVER_HEIGHT;
295 int w = COVER_WIDTH;
296 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
297 int yOffset = HOFFSET;
298
299 if (BORDER != null) {
300 if (BORDER != null) {
301 g.setColor(BORDER);
302 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
303 }
304
305 xOffset++;
306 yOffset++;
307 }
308
309 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
310 xOffset + w + SPINE_WIDTH, xOffset + w };
311 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
312 yOffset + h + SPINE_HEIGHT, yOffset + h };
313 g.setColor(SPINE_COLOR_BOTTOM);
314 g.fillPolygon(new Polygon(xs, ys, xs.length));
315 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
316 xOffset + w + SPINE_WIDTH, xOffset + w };
317 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
318 yOffset + h + SPINE_HEIGHT, yOffset + h };
319 g.setColor(SPINE_COLOR_RIGHT);
320 g.fillPolygon(new Polygon(xs, ys, xs.length));
321
322 Color color = new Color(255, 255, 255, 0);
323 if (!isEnabled()) {
324 } else if (selected && !hovered) {
325 color = new Color(80, 80, 100, 40);
326 } else if (!selected && hovered) {
327 color = new Color(230, 230, 255, 100);
328 } else if (selected && hovered) {
329 color = new Color(200, 200, 255, 100);
330 }
331
332 g.setColor(color);
333 g.fillRect(clip.x, clip.y, clip.width, clip.height);
334
335 if (cached) {
336 UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH
337 + HOFFSET + 30, 10, 20, 20);
338 }
339 }
340
341 /**
342 * Generate a cover icon based upon the given cover image (which may be
343 * NULL).
344 *
345 * @param image
346 * the cover image, or NULL for none
347 *
348 * @return the icon
349 */
350 private ImageIcon generateCoverIcon(BufferedImage image) {
351 BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH
352 + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
353 BufferedImage.TYPE_4BYTE_ABGR);
354 Graphics2D g = resizedImage.createGraphics();
355 g.setColor(Color.white);
356 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
357 if (image != null) {
358 g.drawImage(image, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, null);
359 } else {
360 g.setColor(Color.black);
361 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
362 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
363 }
364 g.dispose();
365
366 return new ImageIcon(resizedImage);
367 }
368 }