Improve UI, implement "Save as..." menu item
[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
24 /**
25 * A book item presented in a {@link LocalReaderFrame}.
26 *
27 * @author niki
28 */
29 class LocalReaderBook extends JPanel {
30 /**
31 * Action on a book item.
32 *
33 * @author niki
34 */
35 interface BookActionListener extends EventListener {
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);
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);
61 }
62
63 private static final long serialVersionUID = 1L;
64
65 // TODO: export some of the configuration options?
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";
76 private static final Color BORDER = Color.black;
77 private static final long doubleClickDelay = 200; // in ms
78 //
79
80 private JLabel icon;
81 private JLabel title;
82 private boolean selected;
83 private boolean hovered;
84 private Date lastClick;
85
86 private List<BookActionListener> listeners;
87 private String luid;
88 private boolean cached;
89
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 */
98 public LocalReaderBook(MetaData meta, boolean cached) {
99 this.luid = meta.getLuid();
100 this.cached = cached;
101
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);
108 if (meta.getCover() != null) {
109 g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT,
110 null);
111 } else {
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);
115 }
116 g.dispose();
117
118 icon = new JLabel(new ImageIcon(resizedImage));
119
120 String optAuthor = meta.getAuthor();
121 if (optAuthor != null && !optAuthor.isEmpty()) {
122 optAuthor = "(" + optAuthor + ")";
123 }
124
125 title = new JLabel(
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));
135 this.add(icon, BorderLayout.CENTER);
136 this.add(title, BorderLayout.SOUTH);
137
138 setupListeners();
139 setSelected(false);
140 }
141
142 /**
143 * The book current selection state.
144 *
145 * @return the selection state
146 */
147 public boolean isSelected() {
148 return selected;
149 }
150
151 /**
152 * The book current selection state.
153 *
154 * @param selected
155 * TRUE if it is selected
156 */
157 public void setSelected(boolean selected) {
158 this.selected = selected;
159 repaint();
160 }
161
162 /**
163 * The item mouse-hover state.
164 *
165 * @param hovered
166 * TRUE if it is mouse-hovered
167 */
168 private void setHovered(boolean hovered) {
169 this.hovered = hovered;
170 repaint();
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 UID of the book represented by this item.
245 *
246 * @return the LUID
247 */
248 public String getLuid() {
249 return luid;
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 this.cached = cached;
269 }
270
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 */
276 @Override
277 public void paint(Graphics g) {
278 super.paint(g);
279
280 Rectangle clip = g.getClipBounds();
281 if (clip.getWidth() <= 0 || clip.getHeight() <= 0) {
282 return;
283 }
284
285 int h = COVER_HEIGHT;
286 int w = COVER_WIDTH;
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 }
299
300 int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH,
301 xOffset + w + SPINE_WIDTH, xOffset + w };
302 int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT,
303 yOffset + h + SPINE_HEIGHT, yOffset + h };
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 };
308 ys = new int[] { yOffset, yOffset + SPINE_HEIGHT,
309 yOffset + h + SPINE_HEIGHT, yOffset + h };
310 g.setColor(SPINE_COLOR_RIGHT);
311 g.fillPolygon(new Polygon(xs, ys, xs.length));
312
313 Color color = new Color(255, 255, 255, 0);
314 if (!isEnabled()) {
315 } else if (selected && !hovered) {
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
323 g.setColor(color);
324 g.fillRect(clip.x, clip.y, clip.width, clip.height);
325
326 if (cached) {
327 g.setColor(Color.green);
328 g.fillOval(COVER_WIDTH + HOFFSET + 30, 10, 20, 20);
329 }
330 }
331 }