Improve UI, implement "Save as..." menu item
[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;
333f0e7b
NR
23
24/**
25 * A book item presented in a {@link LocalReaderFrame}.
26 *
27 * @author niki
28 */
29class LocalReaderBook extends JPanel {
30 /**
31 * Action on a book item.
32 *
33 * @author niki
34 */
92fb0719 35 interface BookActionListener extends EventListener {
333f0e7b
NR
36 /**
37 * The book was selected (single click).
38 *
39 * @param book
40 * the {@link LocalReaderBook} itself
41 */
42 public void select(LocalReaderBook book);
43
44 /**
45 * The book was double-clicked.
46 *
47 * @param book
48 * the {@link LocalReaderBook} itself
49 */
50 public void action(LocalReaderBook book);
9843a5e5
NR
51
52 /**
53 * A popup menu was requested for this {@link LocalReaderBook}.
54 *
55 * @param book
56 * the {@link LocalReaderBook} itself
57 * @param e
58 * the {@link MouseEvent} that generated this call
59 */
60 public void popupRequested(LocalReaderBook book, MouseEvent e);
333f0e7b
NR
61 }
62
4d205683
NR
63 private static final long serialVersionUID = 1L;
64
65 // TODO: export some of the configuration options?
b4dc6ab5
NR
66 private static final int COVER_WIDTH = 100;
67 private static final int COVER_HEIGHT = 150;
68 private static final int SPINE_WIDTH = 5;
69 private static final int SPINE_HEIGHT = 5;
70 private static final int HOFFSET = 20;
71 private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180);
72 private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100);
73 private static final int TEXT_WIDTH = COVER_WIDTH + 40;
74 private static final int TEXT_HEIGHT = 50;
75 private static final String AUTHOR_COLOR = "#888888";
4d205683
NR
76 private static final Color BORDER = Color.black;
77 private static final long doubleClickDelay = 200; // in ms
78 //
3b2b638f 79
333f0e7b 80 private JLabel icon;
3b2b638f 81 private JLabel title;
333f0e7b
NR
82 private boolean selected;
83 private boolean hovered;
84 private Date lastClick;
4d205683 85
92fb0719 86 private List<BookActionListener> listeners;
10d558d2
NR
87 private String luid;
88 private boolean cached;
89
4d205683
NR
90 /**
91 * Create a new {@link LocalReaderBook} item for the givn {@link Story}.
92 *
93 * @param meta
94 * the story {@code}link MetaData}
95 * @param cached
96 * TRUE if it is locally cached
97 */
10d558d2
NR
98 public LocalReaderBook(MetaData meta, boolean cached) {
99 this.luid = meta.getLuid();
100 this.cached = cached;
333f0e7b 101
4d205683
NR
102 BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH
103 + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET,
104 BufferedImage.TYPE_4BYTE_ABGR);
105 Graphics2D g = resizedImage.createGraphics();
106 g.setColor(Color.white);
107 g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT);
333f0e7b 108 if (meta.getCover() != null) {
b4dc6ab5
NR
109 g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
110 null);
333f0e7b 111 } else {
4d205683
NR
112 g.setColor(Color.black);
113 g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT);
114 g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT);
333f0e7b 115 }
4d205683
NR
116 g.dispose();
117
118 icon = new JLabel(new ImageIcon(resizedImage));
333f0e7b 119
b4dc6ab5
NR
120 String optAuthor = meta.getAuthor();
121 if (optAuthor != null && !optAuthor.isEmpty()) {
122 optAuthor = "(" + optAuthor + ")";
123 }
4d205683 124
3b2b638f 125 title = new JLabel(
b4dc6ab5
NR
126 String.format(
127 "<html>"
128 + "<body style='width: %d px; height: %d px; text-align: center'>"
129 + "%s" + "<br>" + "<span style='color: %s;'>"
130 + "%s" + "</span>" + "</body>" + "</html>",
131 TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR,
132 optAuthor));
133
134 this.setLayout(new BorderLayout(10, 10));
333f0e7b 135 this.add(icon, BorderLayout.CENTER);
3b2b638f 136 this.add(title, BorderLayout.SOUTH);
333f0e7b
NR
137
138 setupListeners();
139 setSelected(false);
140 }
141
142 /**
143 * The book current selection state.
144 *
4d205683 145 * @return the selection state
333f0e7b
NR
146 */
147 public boolean isSelected() {
148 return selected;
149 }
150
151 /**
152 * The book current selection state.
153 *
154 * @param selected
4d205683 155 * TRUE if it is selected
333f0e7b
NR
156 */
157 public void setSelected(boolean selected) {
158 this.selected = selected;
d3c84ac3 159 repaint();
333f0e7b
NR
160 }
161
4d205683
NR
162 /**
163 * The item mouse-hover state.
164 *
165 * @param hovered
166 * TRUE if it is mouse-hovered
167 */
333f0e7b
NR
168 private void setHovered(boolean hovered) {
169 this.hovered = hovered;
d3c84ac3 170 repaint();
333f0e7b
NR
171 }
172
4d205683
NR
173 /**
174 * Setup the mouse listener that will activate {@link BookActionListener}
175 * events.
176 */
333f0e7b 177 private void setupListeners() {
92fb0719 178 listeners = new ArrayList<LocalReaderBook.BookActionListener>();
333f0e7b
NR
179 addMouseListener(new MouseListener() {
180 public void mouseReleased(MouseEvent e) {
9843a5e5
NR
181 if (e.isPopupTrigger()) {
182 popup(e);
183 }
333f0e7b
NR
184 }
185
186 public void mousePressed(MouseEvent e) {
9843a5e5
NR
187 if (e.isPopupTrigger()) {
188 popup(e);
189 }
333f0e7b
NR
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) {
3b2b638f
NR
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 }
9843a5e5 209
3b2b638f 210 lastClick = now;
333f0e7b 211 }
333f0e7b 212 }
333f0e7b 213
9843a5e5
NR
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 }
333f0e7b 222 }
9843a5e5
NR
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 });
333f0e7b
NR
231 }
232
4d205683
NR
233 /**
234 * Add a new {@link BookActionListener} on this item.
235 *
236 * @param listener
237 * the listener
238 */
92fb0719 239 public void addActionListener(BookActionListener listener) {
333f0e7b
NR
240 listeners.add(listener);
241 }
d3c84ac3 242
4d205683
NR
243 /**
244 * The Library UID of the book represented by this item.
245 *
246 * @return the LUID
247 */
10d558d2
NR
248 public String getLuid() {
249 return luid;
250 }
251
252 /**
4d205683 253 * This item {@link LocalReader} library cache state.
10d558d2 254 *
4d205683 255 * @return TRUE if it is present in the {@link LocalReader} cache
10d558d2
NR
256 */
257 public boolean isCached() {
258 return cached;
259 }
260
261 /**
4d205683 262 * This item {@link LocalReader} library cache state.
10d558d2
NR
263 *
264 * @param cached
4d205683 265 * TRUE if it is present in the {@link LocalReader} cache
10d558d2
NR
266 */
267 public void setCached(boolean cached) {
268 this.cached = cached;
269 }
270
4d205683
NR
271 /**
272 * Draw a "cached" icon and a partially transparent overlay if needed
273 * depending upon the selection and mouse-hover states on top of the normal
274 * component.
275 */
d3c84ac3
NR
276 @Override
277 public void paint(Graphics g) {
278 super.paint(g);
279
4d205683
NR
280 Rectangle clip = g.getClipBounds();
281 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
282 return;
283 }
284
b4dc6ab5
NR
285 int h = COVER_HEIGHT;
286 int w = COVER_WIDTH;
4d205683
NR
287 int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1;
288 int yOffset = HOFFSET;
289
290 if (BORDER != null) {
291 if (BORDER != null) {
292 g.setColor(BORDER);
293 g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT);
294 }
295
296 xOffset++;
297 yOffset++;
298 }
b4dc6ab5
NR
299
300 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
301 xOffset + w + SPINE_WIDTH, xOffset + w };
4d205683
NR
302 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
303 yOffset + h + SPINE_HEIGHT, yOffset + h };
b4dc6ab5
NR
304 g.setColor(SPINE_COLOR_BOTTOM);
305 g.fillPolygon(new Polygon(xs, ys, xs.length));
306 xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH,
307 xOffset + w + SPINE_WIDTH, xOffset + w };
4d205683
NR
308 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
309 yOffset + h + SPINE_HEIGHT, yOffset + h };
b4dc6ab5
NR
310 g.setColor(SPINE_COLOR_RIGHT);
311 g.fillPolygon(new Polygon(xs, ys, xs.length));
312
d3c84ac3 313 Color color = new Color(255, 255, 255, 0);
3b2b638f
NR
314 if (!isEnabled()) {
315 } else if (selected && !hovered) {
d3c84ac3
NR
316 color = new Color(80, 80, 100, 40);
317 } else if (!selected && hovered) {
318 color = new Color(230, 230, 255, 100);
319 } else if (selected && hovered) {
320 color = new Color(200, 200, 255, 100);
321 }
322
4d205683
NR
323 g.setColor(color);
324 g.fillRect(clip.x, clip.y, clip.width, clip.height);
325
10d558d2
NR
326 if (cached) {
327 g.setColor(Color.green);
4d205683 328 g.fillOval(COVER_WIDTH + HOFFSET + 30, 10, 20, 20);
10d558d2 329 }
d3c84ac3 330 }
333f0e7b 331}