GUI: automatically select URLs from clipboard
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderFrame.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Frame;
6 import java.awt.Toolkit;
7 import java.awt.datatransfer.DataFlavor;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.KeyEvent;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.WindowEvent;
13 import java.io.File;
14 import java.io.IOException;
15 import java.net.URL;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 import javax.swing.JFileChooser;
23 import javax.swing.JFrame;
24 import javax.swing.JMenu;
25 import javax.swing.JMenuBar;
26 import javax.swing.JMenuItem;
27 import javax.swing.JOptionPane;
28 import javax.swing.JPanel;
29 import javax.swing.JPopupMenu;
30 import javax.swing.JScrollPane;
31 import javax.swing.SwingUtilities;
32 import javax.swing.filechooser.FileFilter;
33 import javax.swing.filechooser.FileNameExtensionFilter;
34
35 import be.nikiroo.fanfix.Instance;
36 import be.nikiroo.fanfix.Library;
37 import be.nikiroo.fanfix.bundles.UiConfig;
38 import be.nikiroo.fanfix.data.MetaData;
39 import be.nikiroo.fanfix.data.Story;
40 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
41 import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener;
42 import be.nikiroo.utils.Progress;
43 import be.nikiroo.utils.Version;
44 import be.nikiroo.utils.ui.ProgressBar;
45 import be.nikiroo.utils.ui.WrapLayout;
46
47 /**
48 * A {@link Frame} that will show a {@link LocalReaderBook} item for each
49 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
50 * way to copy them to the {@link LocalReader} cache ({@link LocalReader#lib}),
51 * read them, delete them...
52 *
53 * @author niki
54 */
55 class LocalReaderFrame extends JFrame {
56 private static final long serialVersionUID = 1L;
57 private LocalReader reader;
58 private List<MetaData> stories;
59 private List<LocalReaderBook> books;
60 private JPanel bookPane;
61 private String type;
62 private Color color;
63 private ProgressBar pgBar;
64 private JMenuBar bar;
65 private LocalReaderBook selectedBook;
66
67 /**
68 * Create a new {@link LocalReaderFrame}.
69 *
70 * @param reader
71 * the associated {@link LocalReader} to forward some commands
72 * and access its {@link Library}
73 * @param type
74 * the type of {@link Story} to load, or NULL for all types
75 */
76 public LocalReaderFrame(LocalReader reader, String type) {
77 super(String.format("Fanfix %s Library", Version.getCurrentVersion()));
78
79 this.reader = reader;
80
81 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82 setSize(800, 600);
83 setLayout(new BorderLayout());
84
85 books = new ArrayList<LocalReaderBook>();
86 bookPane = new JPanel(new WrapLayout(WrapLayout.LEADING, 5, 5));
87
88 color = Instance.getUiConfig().getColor(UiConfig.BACKGROUND_COLOR);
89
90 if (color != null) {
91 setBackground(color);
92 bookPane.setBackground(color);
93 }
94
95 JScrollPane scroll = new JScrollPane(bookPane);
96 scroll.getVerticalScrollBar().setUnitIncrement(16);
97 add(scroll, BorderLayout.CENTER);
98
99 pgBar = new ProgressBar();
100 add(pgBar, BorderLayout.SOUTH);
101
102 refreshBooks(type);
103 setJMenuBar(createMenu());
104
105 setVisible(true);
106 }
107
108 /**
109 * Refresh the list of {@link LocalReaderBook}s from disk.
110 *
111 * @param type
112 * the type of {@link Story} to load, or NULL for all types
113 */
114 private void refreshBooks(String type) {
115 this.type = type;
116 stories = Instance.getLibrary().getList(type);
117 books.clear();
118 bookPane.invalidate();
119 bookPane.removeAll();
120 for (MetaData meta : stories) {
121 LocalReaderBook book = new LocalReaderBook(meta,
122 reader.isCached(meta.getLuid()));
123 if (color != null) {
124 book.setBackground(color);
125 }
126
127 books.add(book);
128
129 book.addActionListener(new BookActionListener() {
130 public void select(LocalReaderBook book) {
131 selectedBook = book;
132 for (LocalReaderBook abook : books) {
133 abook.setSelected(abook == book);
134 }
135 }
136
137 public void popupRequested(LocalReaderBook book, MouseEvent e) {
138 JPopupMenu popup = new JPopupMenu();
139 popup.add(createMenuItemOpenBook());
140 popup.addSeparator();
141 popup.add(createMenuItemExport());
142 popup.add(createMenuItemClearCache());
143 popup.add(createMenuItemRedownload());
144 popup.addSeparator();
145 popup.add(createMenuItemDelete());
146 popup.show(e.getComponent(), e.getX(), e.getY());
147 }
148
149 public void action(final LocalReaderBook book) {
150 openBook(book);
151 }
152 });
153
154 bookPane.add(book);
155 }
156
157 bookPane.validate();
158 bookPane.repaint();
159 }
160
161 /**
162 * Create the main menu bar.
163 *
164 * @return the bar
165 */
166 private JMenuBar createMenu() {
167 bar = new JMenuBar();
168
169 JMenu file = new JMenu("File");
170 file.setMnemonic(KeyEvent.VK_F);
171
172 JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U);
173 imprt.addActionListener(new ActionListener() {
174 public void actionPerformed(ActionEvent e) {
175 imprt(true);
176 }
177 });
178 JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F);
179 imprtF.addActionListener(new ActionListener() {
180 public void actionPerformed(ActionEvent e) {
181 imprt(false);
182 }
183 });
184 JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X);
185 exit.addActionListener(new ActionListener() {
186 public void actionPerformed(ActionEvent e) {
187 LocalReaderFrame.this.dispatchEvent(new WindowEvent(
188 LocalReaderFrame.this, WindowEvent.WINDOW_CLOSING));
189 }
190 });
191
192 file.add(createMenuItemOpenBook());
193 file.add(createMenuItemExport());
194 file.addSeparator();
195 file.add(imprt);
196 file.add(imprtF);
197 file.addSeparator();
198 file.add(exit);
199
200 bar.add(file);
201
202 JMenu edit = new JMenu("Edit");
203 edit.setMnemonic(KeyEvent.VK_E);
204
205 edit.add(createMenuItemClearCache());
206 edit.add(createMenuItemRedownload());
207 edit.addSeparator();
208 edit.add(createMenuItemDelete());
209
210 bar.add(edit);
211
212 JMenu view = new JMenu("View");
213 view.setMnemonic(KeyEvent.VK_V);
214
215 List<String> tt = Instance.getLibrary().getTypes();
216 tt.add(0, null);
217 for (final String type : tt) {
218 JMenuItem item = new JMenuItem(type == null ? "All books" : type);
219 item.addActionListener(new ActionListener() {
220 public void actionPerformed(ActionEvent e) {
221 refreshBooks(type);
222 }
223 });
224 view.add(item);
225
226 if (type == null) {
227 view.addSeparator();
228 }
229 }
230
231 bar.add(view);
232
233 return bar;
234 }
235
236 /**
237 * Create the export menu item.
238 *
239 * @return the item
240 */
241 private JMenuItem createMenuItemExport() {
242 final JFileChooser fc = new JFileChooser();
243 fc.setAcceptAllFileFilterUsed(false);
244
245 final Map<FileFilter, OutputType> filters = new HashMap<FileFilter, OutputType>();
246 for (OutputType type : OutputType.values()) {
247 String ext = type.getDefaultExtension(false);
248 String desc = type.getDesc(false);
249 if (ext == null || ext.isEmpty()) {
250 filters.put(createAllFilter(desc), type);
251 } else {
252 filters.put(new FileNameExtensionFilter(desc, ext), type);
253 }
254 }
255
256 // First the "ALL" filters, then, the extension filters
257 for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
258 if (!(entry.getKey() instanceof FileNameExtensionFilter)) {
259 fc.addChoosableFileFilter(entry.getKey());
260 }
261 }
262 for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
263 if (entry.getKey() instanceof FileNameExtensionFilter) {
264 fc.addChoosableFileFilter(entry.getKey());
265 }
266 }
267 //
268
269 JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S);
270 export.addActionListener(new ActionListener() {
271 public void actionPerformed(ActionEvent e) {
272 if (selectedBook != null) {
273 fc.showDialog(LocalReaderFrame.this, "Save");
274 final OutputType type = filters.get(fc.getFileFilter());
275 final String path = fc.getSelectedFile().getAbsolutePath()
276 + type.getDefaultExtension(false);
277 final Progress pg = new Progress();
278 outOfUi(pg, new Runnable() {
279 public void run() {
280 try {
281 Instance.getLibrary().export(
282 selectedBook.getMeta().getLuid(), type,
283 path, pg);
284 } catch (IOException e) {
285 Instance.syserr(e);
286 }
287 }
288 });
289 }
290 }
291 });
292
293 return export;
294 }
295
296 /**
297 * Create a {@link FileFilter} that accepts all files and return the given
298 * description.
299 *
300 * @param desc
301 * the description
302 *
303 * @return the filter
304 */
305 private FileFilter createAllFilter(final String desc) {
306 return new FileFilter() {
307 @Override
308 public String getDescription() {
309 return desc;
310 }
311
312 @Override
313 public boolean accept(File f) {
314 return true;
315 }
316 };
317 }
318
319 /**
320 * Create the refresh (delete cache) menu item.
321 *
322 * @return the item
323 */
324 private JMenuItem createMenuItemClearCache() {
325 JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C);
326 refresh.addActionListener(new ActionListener() {
327 public void actionPerformed(ActionEvent e) {
328 if (selectedBook != null) {
329 outOfUi(null, new Runnable() {
330 public void run() {
331 reader.refresh(selectedBook.getMeta().getLuid());
332 selectedBook.setCached(false);
333 SwingUtilities.invokeLater(new Runnable() {
334 public void run() {
335 selectedBook.repaint();
336 }
337 });
338 }
339 });
340 }
341 }
342 });
343
344 return refresh;
345 }
346
347 /**
348 * Create the redownload (then delete original) menu item.
349 *
350 * @return the item
351 */
352 private JMenuItem createMenuItemRedownload() {
353 JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R);
354 refresh.addActionListener(new ActionListener() {
355 public void actionPerformed(ActionEvent e) {
356 if (selectedBook != null) {
357 imprt(selectedBook.getMeta().getUrl(), new Runnable() {
358 public void run() {
359 reader.delete(selectedBook.getMeta().getLuid());
360 selectedBook = null;
361 }
362 });
363 }
364 }
365 });
366
367 return refresh;
368 }
369
370 /**
371 * Create the delete menu item.
372 *
373 * @return the item
374 */
375 private JMenuItem createMenuItemDelete() {
376 JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D);
377 delete.addActionListener(new ActionListener() {
378 public void actionPerformed(ActionEvent e) {
379 if (selectedBook != null) {
380 outOfUi(null, new Runnable() {
381 public void run() {
382 reader.delete(selectedBook.getMeta().getLuid());
383 selectedBook = null;
384 SwingUtilities.invokeLater(new Runnable() {
385 public void run() {
386 refreshBooks(type);
387 }
388 });
389 }
390 });
391 }
392 }
393 });
394
395 return delete;
396 }
397
398 /**
399 * Create the open menu item.
400 *
401 * @return the item
402 */
403 private JMenuItem createMenuItemOpenBook() {
404 JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O);
405 open.addActionListener(new ActionListener() {
406 public void actionPerformed(ActionEvent e) {
407 if (selectedBook != null) {
408 openBook(selectedBook);
409 }
410 }
411 });
412
413 return open;
414 }
415
416 /**
417 * Open a {@link LocalReaderBook} item.
418 *
419 * @param book
420 * the {@link LocalReaderBook} to open
421 */
422 private void openBook(final LocalReaderBook book) {
423 final Progress pg = new Progress();
424 outOfUi(pg, new Runnable() {
425 public void run() {
426 try {
427 reader.open(book.getMeta().getLuid(), pg);
428 SwingUtilities.invokeLater(new Runnable() {
429 public void run() {
430 book.setCached(true);
431 }
432 });
433 } catch (IOException e) {
434 // TODO: error message?
435 Instance.syserr(e);
436 }
437 }
438 });
439 }
440
441 /**
442 * Process the given action out of the Swing UI thread and link the given
443 * {@link ProgressBar} to the action.
444 * <p>
445 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
446 * to done when the action is done.
447 *
448 * @param pg
449 * the {@link ProgressBar} or NULL
450 * @param run
451 * the action to run
452 */
453 private void outOfUi(final Progress pg, final Runnable run) {
454 pgBar.setProgress(pg);
455
456 setEnabled(false);
457 pgBar.addActioListener(new ActionListener() {
458 public void actionPerformed(ActionEvent e) {
459 pgBar.setProgress(null);
460 setEnabled(true);
461 }
462 });
463
464 new Thread(new Runnable() {
465 public void run() {
466 run.run();
467 if (pg == null) {
468 SwingUtilities.invokeLater(new Runnable() {
469 public void run() {
470 setEnabled(true);
471 }
472 });
473 } else if (!pg.isDone()) {
474 pg.setProgress(pg.getMax());
475 }
476 }
477 }).start();
478 }
479
480 /**
481 * Import a {@link Story} into the main {@link Library}.
482 * <p>
483 * Should be called inside the UI thread.
484 *
485 * @param askUrl
486 * TRUE for an {@link URL}, false for a {@link File}
487 */
488 private void imprt(boolean askUrl) {
489 JFileChooser fc = new JFileChooser();
490
491 Object url;
492 if (askUrl) {
493 String clipboard = "";
494 try {
495 clipboard = ("" + Toolkit.getDefaultToolkit()
496 .getSystemClipboard().getData(DataFlavor.stringFlavor))
497 .trim();
498 } catch (Exception e) {
499 // No data will be handled
500 }
501
502 if (clipboard == null || !clipboard.startsWith("http")) {
503 clipboard = "";
504 }
505
506 url = JOptionPane.showInputDialog(LocalReaderFrame.this,
507 "url of the story to import?", "Importing from URL",
508 JOptionPane.QUESTION_MESSAGE, null, null, clipboard);
509 } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) {
510 url = fc.getSelectedFile().getAbsolutePath();
511 } else {
512 url = null;
513 }
514
515 if (url != null && !url.toString().isEmpty()) {
516 imprt(url.toString(), null);
517 }
518 }
519
520 /**
521 * Actually import the {@link Story} into the main {@link Library}.
522 * <p>
523 * Should be called inside the UI thread.
524 *
525 * @param url
526 * the {@link Story} to import by {@link URL}
527 * @param onSuccess
528 * Action to execute on success
529 */
530 private void imprt(final String url, final Runnable onSuccess) {
531 final Progress pg = new Progress("Importing " + url);
532 outOfUi(pg, new Runnable() {
533 public void run() {
534 Exception ex = null;
535 try {
536 Instance.getLibrary().imprt(BasicReader.getUrl(url), pg);
537 } catch (IOException e) {
538 ex = e;
539 }
540
541 final Exception e = ex;
542
543 final boolean ok = (e == null);
544 SwingUtilities.invokeLater(new Runnable() {
545 public void run() {
546 if (!ok) {
547 Instance.syserr(e);
548 JOptionPane.showMessageDialog(
549 LocalReaderFrame.this, "Cannot import: "
550 + url, e.getMessage(),
551 JOptionPane.ERROR_MESSAGE);
552
553 setEnabled(true);
554 } else {
555 refreshBooks(type);
556 if (onSuccess != null) {
557 onSuccess.run();
558 refreshBooks(type);
559 }
560 }
561 }
562 });
563 }
564 });
565 }
566
567 /**
568 * Enables or disables this component, depending on the value of the
569 * parameter <code>b</code>. An enabled component can respond to user input
570 * and generate events. Components are enabled initially by default.
571 * <p>
572 * Disabling this component will also affect its children.
573 *
574 * @param b
575 * If <code>true</code>, this component is enabled; otherwise
576 * this component is disabled
577 */
578 @Override
579 public void setEnabled(boolean b) {
580 for (LocalReaderBook book : books) {
581 book.setEnabled(b);
582 book.repaint();
583 }
584
585 bar.setEnabled(b);
586 bookPane.setEnabled(b);
587 bookPane.repaint();
588
589 super.setEnabled(b);
590 repaint();
591 }
592 }