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