7dd41b6ca304a9ef586360d5f452c8062b4fd211
[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 String luid;
89 private boolean cached;
90
91 /**
92 * Create a new {@link LocalReaderBook} item for the givn {@link Story}.
93 *
94 * @param meta
95 * the story {@code}link MetaData}
96 * @param cached
97 * TRUE if it is locally cached
98 */
99 public LocalReaderBook(MetaData meta, boolean cached) {
100 this.luid = meta.getLuid();
101 this.cached = cached;
102
103 BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH
104 + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
105 BufferedImage.TYPE_4BYTE_ABGR);
106 Graphics2D g = resizedImage.createGraphics();
107 g.setColor(Color.white);
108 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
109 if (meta.getCover() != null) {
110 g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
111 null);
112 } else {
113 g.setColor(Color.black);
114 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
115 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
116 }
117 g.dispose();
118
119 icon = new JLabel(new ImageIcon(resizedImage));
120
121 String optAuthor = meta.getAuthor();
122 if (optAuthor != null && !optAuthor.isEmpty()) {
123 optAuthor = "(" + optAuthor + ")";
124 }
125
126 title = new JLabel(
127 String.format(
128 "<html>"
129 + "<body style='width: %d px; height: %d px; text-align: center'>"
130 + "%s" + "<br>" + "<span style='color: %s;'>"
131 + "%s" + "</span>" + "</body>" + "</html>",
132 TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
133 optAuthor));
134
135 this.setLayout(new BorderLayout(10, 10));
136 this.add(icon, BorderLayout.CENTER);
137 this.add(title, BorderLayout.SOUTH);
138
139 setupListeners();
140 setSelected(false);
141 }
142
143 /**
144 * The book current selection state.
145 *
146 * @return the selection state
147 */
148 public boolean isSelected() {
149 return selected;
150 }
151
152 /**
153 * The book current selection state.
154 *
155 * @param selected
156 * TRUE if it is selected
157 */
158 public void setSelected(boolean selected) {
159 this.selected = selected;
160 repaint();
161 }
162
163 /**
164 * The item mouse-hover state.
165 *
166 * @param hovered
167 * TRUE if it is mouse-hovered
168 */
169 private void setHovered(boolean hovered) {
170 this.hovered = hovered;
171 repaint();
172 }
173
174 /**
175 * Setup the mouse listener that will activate {@link BookActionListener}
176 * events.
177 */
178 private void setupListeners() {
179 listeners = new ArrayList<LocalReaderBook.BookActionListener>();
180 addMouseListener(new MouseListener() {
181 public void mouseReleased(MouseEvent e) {
182 if (e.isPopupTrigger()) {
183 popup(e);
184 }
185 }
186
187 public void mousePressed(MouseEvent e) {
188 if (e.isPopupTrigger()) {
189 popup(e);
190 }
191 }
192
193 public void mouseExited(MouseEvent e) {
194 setHovered(false);
195 }
196
197 public void mouseEntered(MouseEvent e) {
198 setHovered(true);
199 }
200
201 public void mouseClicked(MouseEvent e) {
202 if (isEnabled()) {
203 Date now = new Date();
204 if (lastClick != null
205 && now.getTime() - lastClick.getTime() < doubleClickDelay) {
206 click(true);
207 } else {
208 click(false);
209 }
210
211 lastClick = now;
212 }
213 }
214
215 private void click(boolean doubleClick) {
216 for (BookActionListener listener : listeners) {
217 if (doubleClick) {
218 listener.action(LocalReaderBook.this);
219 } else {
220 listener.select(LocalReaderBook.this);
221 }
222 }
223 }
224
225 private void popup(MouseEvent e) {
226 for (BookActionListener listener : listeners) {
227 listener.select((LocalReaderBook.this));
228 listener.popupRequested(LocalReaderBook.this, e);
229 }
230 }
231 });
232 }
233
234 /**
235 * Add a new {@link BookActionListener} on this item.
236 *
237 * @param listener
238 * the listener
239 */
240 public void addActionListener(BookActionListener listener) {
241 listeners.add(listener);
242 }
243
244 /**
245 * The Library UID of the book represented by this item.
246 *
247 * @return the LUID
248 */
249 public String getLuid() {
250 return luid;
251 }
252
253 /**
254 * This item {@link LocalReader} library cache state.
255 *
256 * @return TRUE if it is present in the {@link LocalReader} cache
257 */
258 public boolean isCached() {
259 return cached;
260 }
261
262 /**
263 * This item {@link LocalReader} library cache state.
264 *
265 * @param cached
266 * TRUE if it is present in the {@link LocalReader} cache
267 */
268 public void setCached(boolean cached) {
269 this.cached = cached;
270 }
271
272 /**
273 * Draw a "cached" icon and a partially transparent overlay if needed
274 * depending upon the selection and mouse-hover states on top of the normal
275 * component.
276 */
277 @Override
278 public void paint(Graphics g) {
279 super.paint(g);
280
281 Rectangle clip = g.getClipBounds();
282 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
283 return;
284 }
285
286 int h = COVER_HEIGHT;
287 int w = COVER_WIDTH;
288 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
289 int yOffset = HOFFSET;
290
291 if (BORDER != null) {
292 if (BORDER != null) {
293 g.setColor(BORDER);
294 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
295 }
296
297 xOffset++;
298 yOffset++;
299 }
300
301 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
302 xOffset + w + SPINE_WIDTH, xOffset + w };
303 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
304 yOffset + h + SPINE_HEIGHT, yOffset + h };
305 g.setColor(SPINE_COLOR_BOTTOM);
306 g.fillPolygon(new Polygon(xs, ys, xs.length));
307 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
308 xOffset + w + SPINE_WIDTH, xOffset + w };
309 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
310 yOffset + h + SPINE_HEIGHT, yOffset + h };
311 g.setColor(SPINE_COLOR_RIGHT);
312 g.fillPolygon(new Polygon(xs, ys, xs.length));
313
314 Color color = new Color(255, 255, 255, 0);
315 if (!isEnabled()) {
316 } else if (selected && !hovered) {
317 color = new Color(80, 80, 100, 40);
318 } else if (!selected && hovered) {
319 color = new Color(230, 230, 255, 100);
320 } else if (selected && hovered) {
321 color = new Color(200, 200, 255, 100);
322 }
323
324 g.setColor(color);
325 g.fillRect(clip.x, clip.y, clip.width, clip.height);
326
327 if (cached) {
328 UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH
329 + HOFFSET + 30, 10, 20, 20);
330 }
331 }
332 }