Fix breaks (<hr>) CSS style, GUI update bug fixed
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderBook.java
CommitLineData
333f0e7b
NR
1package be.nikiroo.fanfix.reader;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
d3c84ac3 5import java.awt.Graphics;
333f0e7b 6import java.awt.Graphics2D;
b4dc6ab5 7import java.awt.Polygon;
d3c84ac3 8import java.awt.Rectangle;
333f0e7b
NR
9import java.awt.event.MouseEvent;
10import java.awt.event.MouseListener;
11import java.awt.image.BufferedImage;
12import java.util.ArrayList;
13import java.util.Date;
14import java.util.EventListener;
15import java.util.List;
16
17import javax.swing.ImageIcon;
18import javax.swing.JLabel;
19import javax.swing.JPanel;
20
21import be.nikiroo.fanfix.data.MetaData;
4d205683 22import be.nikiroo.fanfix.data.Story;
c39a3e30 23import be.nikiroo.utils.ui.UIUtils;
333f0e7b
NR
24
25/**
26 * A book item presented in a {@link LocalReaderFrame}.
27 *
28 * @author niki
29 */
30class LocalReaderBook extends JPanel {
31 /**
32 * Action on a book item.
33 *
34 * @author niki
35 */
92fb0719 36 interface BookActionListener extends EventListener {
333f0e7b
NR
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);
9843a5e5
NR
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);
333f0e7b
NR
62 }
63
4d205683
NR
64 private static final long serialVersionUID = 1L;
65
66 // TODO: export some of the configuration options?
b4dc6ab5
NR
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";
4d205683
NR
77 private static final Color BORDER = Color.black;
78 private static final long doubleClickDelay = 200; // in ms
79 //
3b2b638f 80
333f0e7b 81 private JLabel icon;
3b2b638f 82 private JLabel title;
333f0e7b
NR
83 private boolean selected;
84 private boolean hovered;
85 private Date lastClick;
4d205683 86
92fb0719 87 private List<BookActionListener> listeners;
10d558d2
NR
88 private String luid;
89 private boolean cached;
90
4d205683
NR
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 */
10d558d2
NR
99 public LocalReaderBook(MetaData meta, boolean cached) {
100 this.luid = meta.getLuid();
101 this.cached = cached;
333f0e7b 102
4d205683
NR
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);
333f0e7b 109 if (meta.getCover() != null) {
b4dc6ab5
NR
110 g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
111 null);
333f0e7b 112 } else {
4d205683
NR
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);
333f0e7b 116 }
4d205683
NR
117 g.dispose();
118
119 icon = new JLabel(new ImageIcon(resizedImage));
333f0e7b 120
b4dc6ab5
NR
121 String optAuthor = meta.getAuthor();
122 if (optAuthor != null && !optAuthor.isEmpty()) {
123 optAuthor = "(" + optAuthor + ")";
124 }
4d205683 125
3b2b638f 126 title = new JLabel(
b4dc6ab5
NR
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));
333f0e7b 136 this.add(icon, BorderLayout.CENTER);
3b2b638f 137 this.add(title, BorderLayout.SOUTH);
333f0e7b
NR
138
139 setupListeners();
140 setSelected(false);
141 }
142
143 /**
144 * The book current selection state.
145 *
4d205683 146 * @return the selection state
333f0e7b
NR
147 */
148 public boolean isSelected() {
149 return selected;
150 }
151
152 /**
153 * The book current selection state.
154 *
155 * @param selected
4d205683 156 * TRUE if it is selected
333f0e7b
NR
157 */
158 public void setSelected(boolean selected) {
159 this.selected = selected;
d3c84ac3 160 repaint();
333f0e7b
NR
161 }
162
4d205683
NR
163 /**
164 * The item mouse-hover state.
165 *
166 * @param hovered
167 * TRUE if it is mouse-hovered
168 */
333f0e7b
NR
169 private void setHovered(boolean hovered) {
170 this.hovered = hovered;
d3c84ac3 171 repaint();
333f0e7b
NR
172 }
173
4d205683
NR
174 /**
175 * Setup the mouse listener that will activate {@link BookActionListener}
176 * events.
177 */
333f0e7b 178 private void setupListeners() {
92fb0719 179 listeners = new ArrayList<LocalReaderBook.BookActionListener>();
333f0e7b
NR
180 addMouseListener(new MouseListener() {
181 public void mouseReleased(MouseEvent e) {
9843a5e5
NR
182 if (e.isPopupTrigger()) {
183 popup(e);
184 }
333f0e7b
NR
185 }
186
187 public void mousePressed(MouseEvent e) {
9843a5e5
NR
188 if (e.isPopupTrigger()) {
189 popup(e);
190 }
333f0e7b
NR
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) {
3b2b638f
NR
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 }
9843a5e5 210
3b2b638f 211 lastClick = now;
333f0e7b 212 }
333f0e7b 213 }
333f0e7b 214
9843a5e5
NR
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 }
333f0e7b 223 }
9843a5e5
NR
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 });
333f0e7b
NR
232 }
233
4d205683
NR
234 /**
235 * Add a new {@link BookActionListener} on this item.
236 *
237 * @param listener
238 * the listener
239 */
92fb0719 240 public void addActionListener(BookActionListener listener) {
333f0e7b
NR
241 listeners.add(listener);
242 }
d3c84ac3 243
4d205683
NR
244 /**
245 * The Library UID of the book represented by this item.
246 *
247 * @return the LUID
248 */
10d558d2
NR
249 public String getLuid() {
250 return luid;
251 }
252
253 /**
4d205683 254 * This item {@link LocalReader} library cache state.
10d558d2 255 *
4d205683 256 * @return TRUE if it is present in the {@link LocalReader} cache
10d558d2
NR
257 */
258 public boolean isCached() {
259 return cached;
260 }
261
262 /**
4d205683 263 * This item {@link LocalReader} library cache state.
10d558d2
NR
264 *
265 * @param cached
4d205683 266 * TRUE if it is present in the {@link LocalReader} cache
10d558d2
NR
267 */
268 public void setCached(boolean cached) {
f977d05b
NR
269 if (this.cached != cached) {
270 this.cached = cached;
271 invalidate();
272 }
10d558d2
NR
273 }
274
4d205683
NR
275 /**
276 * Draw a "cached" icon and a partially transparent overlay if needed
277 * depending upon the selection and mouse-hover states on top of the normal
278 * component.
279 */
d3c84ac3
NR
280 @Override
281 public void paint(Graphics g) {
282 super.paint(g);
283
4d205683
NR
284 Rectangle clip = g.getClipBounds();
285 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
286 return;
287 }
288
b4dc6ab5
NR
289 int h = COVER_HEIGHT;
290 int w = COVER_WIDTH;
4d205683
NR
291 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
292 int yOffset = HOFFSET;
293
294 if (BORDER != null) {
295 if (BORDER != null) {
296 g.setColor(BORDER);
297 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
298 }
299
300 xOffset++;
301 yOffset++;
302 }
b4dc6ab5
NR
303
304 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
305 xOffset + w + SPINE_WIDTH, xOffset + w };
4d205683
NR
306 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
307 yOffset + h + SPINE_HEIGHT, yOffset + h };
b4dc6ab5
NR
308 g.setColor(SPINE_COLOR_BOTTOM);
309 g.fillPolygon(new Polygon(xs, ys, xs.length));
310 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
311 xOffset + w + SPINE_WIDTH, xOffset + w };
4d205683
NR
312 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
313 yOffset + h + SPINE_HEIGHT, yOffset + h };
b4dc6ab5
NR
314 g.setColor(SPINE_COLOR_RIGHT);
315 g.fillPolygon(new Polygon(xs, ys, xs.length));
316
d3c84ac3 317 Color color = new Color(255, 255, 255, 0);
3b2b638f
NR
318 if (!isEnabled()) {
319 } else if (selected && !hovered) {
d3c84ac3
NR
320 color = new Color(80, 80, 100, 40);
321 } else if (!selected && hovered) {
322 color = new Color(230, 230, 255, 100);
323 } else if (selected && hovered) {
324 color = new Color(200, 200, 255, 100);
325 }
326
4d205683
NR
327 g.setColor(color);
328 g.fillRect(clip.x, clip.y, clip.width, clip.height);
329
10d558d2 330 if (cached) {
c39a3e30
NR
331 UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH
332 + HOFFSET + 30, 10, 20, 20);
10d558d2 333 }
d3c84ac3 334 }
333f0e7b 335}