New one-item-per-source-type mode
[fanfix.git] / src / be / nikiroo / fanfix / reader / GuiReaderFrame.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.BoxLayout;
23 import javax.swing.JFileChooser;
24 import javax.swing.JFrame;
25 import javax.swing.JMenu;
26 import javax.swing.JMenuBar;
27 import javax.swing.JMenuItem;
28 import javax.swing.JOptionPane;
29 import javax.swing.JPanel;
30 import javax.swing.JPopupMenu;
31 import javax.swing.JScrollPane;
32 import javax.swing.SwingUtilities;
33 import javax.swing.filechooser.FileFilter;
34 import javax.swing.filechooser.FileNameExtensionFilter;
35
36 import be.nikiroo.fanfix.Instance;
37 import be.nikiroo.fanfix.bundles.Config;
38 import be.nikiroo.fanfix.bundles.UiConfig;
39 import be.nikiroo.fanfix.data.MetaData;
40 import be.nikiroo.fanfix.data.Story;
41 import be.nikiroo.fanfix.library.LocalLibrary;
42 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
43 import be.nikiroo.fanfix.reader.GuiReaderBook.BookActionListener;
44 import be.nikiroo.utils.Progress;
45 import be.nikiroo.utils.Version;
46 import be.nikiroo.utils.ui.ConfigEditor;
47 import be.nikiroo.utils.ui.ProgressBar;
48
49 /**
50 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
51 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
52 * way to copy them to the {@link GuiReader} cache (
53 * {@link BasicReader#getLibrary()}), read them, delete them...
54 *
55 * @author niki
56 */
57 class GuiReaderFrame extends JFrame {
58 private static final long serialVersionUID = 1L;
59 private GuiReader reader;
60 private Map<GuiReaderGroup, String> booksByType;
61 private Map<GuiReaderGroup, String> booksByAuthor;
62 private JPanel pane;
63 private Color color;
64 private ProgressBar pgBar;
65 private JMenuBar bar;
66 private GuiReaderBook selectedBook;
67 private boolean words; // words or authors (secondary info on books)
68
69 /**
70 * Create a new {@link GuiReaderFrame}.
71 *
72 * @param reader
73 * the associated {@link GuiReader} to forward some commands and
74 * access its {@link LocalLibrary}
75 * @param type
76 * the type of {@link Story} to load, or NULL for all types
77 */
78 public GuiReaderFrame(GuiReader reader, String type) {
79 super(String.format("Fanfix %s Library", Version.getCurrentVersion()));
80
81 this.reader = reader;
82
83 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84 setSize(800, 600);
85 setLayout(new BorderLayout());
86
87 pane = new JPanel();
88 pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
89
90 color = Instance.getUiConfig().getColor(UiConfig.BACKGROUND_COLOR);
91 if (color != null) {
92 setBackground(color);
93 pane.setBackground(color);
94 }
95
96 JScrollPane scroll = new JScrollPane(pane);
97 scroll.getVerticalScrollBar().setUnitIncrement(16);
98 add(scroll, BorderLayout.CENTER);
99
100 pgBar = new ProgressBar();
101 add(pgBar, BorderLayout.SOUTH);
102
103 pgBar.addActionListener(new ActionListener() {
104 @Override
105 public void actionPerformed(ActionEvent e) {
106 invalidate();
107 pgBar.setProgress(null);
108 validate();
109 setEnabled(true);
110 }
111 });
112
113 pgBar.addUpdateListener(new ActionListener() {
114 @Override
115 public void actionPerformed(ActionEvent e) {
116 invalidate();
117 validate();
118 repaint();
119 }
120 });
121
122 booksByType = new HashMap<GuiReaderGroup, String>();
123 booksByAuthor = new HashMap<GuiReaderGroup, String>();
124
125 pane.setVisible(false);
126 final Progress pg = new Progress();
127 final String typeF = type;
128 outOfUi(pg, new Runnable() {
129 @Override
130 public void run() {
131 GuiReaderFrame.this.reader.getLibrary().refresh(false, pg);
132 invalidate();
133 setJMenuBar(createMenu());
134 addBookPane(typeF, true);
135 refreshBooks();
136 validate();
137 pane.setVisible(true);
138 }
139 });
140
141 setVisible(true);
142 }
143
144 private void addSourcePanes() {
145 // Sources -> i18n
146 GuiReaderGroup bookPane = new GuiReaderGroup(reader, "Sources", color);
147
148 List<MetaData> sources = new ArrayList<MetaData>();
149 for (String source : reader.getLibrary().getSources()) {
150 MetaData mSource = new MetaData();
151 mSource.setLuid(null);
152 mSource.setTitle(source);
153 mSource.setSource(source);
154 sources.add(mSource);
155 }
156
157 bookPane.refreshBooks(sources, false);
158
159 this.invalidate();
160 pane.invalidate();
161 pane.add(bookPane);
162 pane.validate();
163 this.validate();
164
165 bookPane.setActionListener(new BookActionListener() {
166 @Override
167 public void select(GuiReaderBook book) {
168 selectedBook = book;
169 }
170
171 @Override
172 public void popupRequested(GuiReaderBook book, MouseEvent e) {
173 JPopupMenu popup = new JPopupMenu();
174 popup.add(createMenuItemOpenBook());
175 popup.show(e.getComponent(), e.getX(), e.getY());
176 }
177
178 @Override
179 public void action(final GuiReaderBook book) {
180 removeBookPanes();
181 addBookPane(book.getMeta().getSource(), true);
182 refreshBooks();
183 }
184 });
185 }
186
187 /**
188 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
189 * selected type or author.
190 *
191 * @param value
192 * the author or the type, or NULL to get all the
193 * authors-or-types
194 * @param type
195 * TRUE for type, FALSE for author
196 */
197 private void addBookPane(String value, boolean type) {
198 if (value == null) {
199 if (type) {
200 if (Instance.getUiConfig().getBoolean(UiConfig.SOURCE_PAGE,
201 false)) {
202 addSourcePanes();
203 } else {
204 for (String tt : reader.getLibrary().getSources()) {
205 if (tt != null) {
206 addBookPane(tt, type);
207 }
208 }
209 }
210 } else {
211 for (String tt : reader.getLibrary().getAuthors()) {
212 if (tt != null) {
213 addBookPane(tt, type);
214 }
215 }
216 }
217
218 return;
219 }
220
221 GuiReaderGroup bookPane = new GuiReaderGroup(reader, value, color);
222 if (type) {
223 booksByType.put(bookPane, value);
224 } else {
225 booksByAuthor.put(bookPane, value);
226 }
227
228 this.invalidate();
229 pane.invalidate();
230 pane.add(bookPane);
231 pane.validate();
232 this.validate();
233
234 bookPane.setActionListener(new BookActionListener() {
235 @Override
236 public void select(GuiReaderBook book) {
237 selectedBook = book;
238 }
239
240 @Override
241 public void popupRequested(GuiReaderBook book, MouseEvent e) {
242 JPopupMenu popup = new JPopupMenu();
243 popup.add(createMenuItemOpenBook());
244 popup.addSeparator();
245 popup.add(createMenuItemExport());
246 popup.add(createMenuItemMove());
247 popup.add(createMenuItemSetCover());
248 popup.add(createMenuItemClearCache());
249 popup.add(createMenuItemRedownload());
250 popup.addSeparator();
251 popup.add(createMenuItemDelete());
252 popup.show(e.getComponent(), e.getX(), e.getY());
253 }
254
255 @Override
256 public void action(final GuiReaderBook book) {
257 openBook(book);
258 }
259 });
260 }
261
262 private void removeBookPanes() {
263 booksByType.clear();
264 booksByAuthor.clear();
265 pane.invalidate();
266 this.invalidate();
267 pane.removeAll();
268 pane.validate();
269 this.validate();
270 }
271
272 /**
273 * Refresh the list of {@link GuiReaderBook}s from disk.
274 *
275 */
276 private void refreshBooks() {
277 for (GuiReaderGroup group : booksByType.keySet()) {
278 List<MetaData> stories = reader.getLibrary().getListBySource(
279 booksByType.get(group));
280 group.refreshBooks(stories, words);
281 }
282
283 for (GuiReaderGroup group : booksByAuthor.keySet()) {
284 List<MetaData> stories = reader.getLibrary().getListByAuthor(
285 booksByAuthor.get(group));
286 group.refreshBooks(stories, words);
287 }
288
289 pane.repaint();
290 this.repaint();
291 }
292
293 /**
294 * Create the main menu bar.
295 *
296 * @return the bar
297 */
298 private JMenuBar createMenu() {
299 bar = new JMenuBar();
300
301 JMenu file = new JMenu("File");
302 file.setMnemonic(KeyEvent.VK_F);
303
304 JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U);
305 imprt.addActionListener(new ActionListener() {
306 @Override
307 public void actionPerformed(ActionEvent e) {
308 imprt(true);
309 }
310 });
311 JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F);
312 imprtF.addActionListener(new ActionListener() {
313 @Override
314 public void actionPerformed(ActionEvent e) {
315 imprt(false);
316 }
317 });
318 JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X);
319 exit.addActionListener(new ActionListener() {
320 @Override
321 public void actionPerformed(ActionEvent e) {
322 GuiReaderFrame.this.dispatchEvent(new WindowEvent(
323 GuiReaderFrame.this, WindowEvent.WINDOW_CLOSING));
324 }
325 });
326
327 file.add(createMenuItemOpenBook());
328 file.add(createMenuItemExport());
329 file.add(createMenuItemMove());
330 file.addSeparator();
331 file.add(imprt);
332 file.add(imprtF);
333 file.addSeparator();
334 file.add(exit);
335
336 bar.add(file);
337
338 JMenu edit = new JMenu("Edit");
339 edit.setMnemonic(KeyEvent.VK_E);
340
341 edit.add(createMenuItemClearCache());
342 edit.add(createMenuItemRedownload());
343 edit.addSeparator();
344 edit.add(createMenuItemDelete());
345
346 bar.add(edit);
347
348 JMenu view = new JMenu("View");
349 view.setMnemonic(KeyEvent.VK_V);
350 JMenuItem vauthors = new JMenuItem("Author");
351 vauthors.setMnemonic(KeyEvent.VK_A);
352 vauthors.addActionListener(new ActionListener() {
353 @Override
354 public void actionPerformed(ActionEvent e) {
355 words = false;
356 refreshBooks();
357 }
358 });
359 view.add(vauthors);
360 JMenuItem vwords = new JMenuItem("Word count");
361 vwords.setMnemonic(KeyEvent.VK_W);
362 vwords.addActionListener(new ActionListener() {
363 @Override
364 public void actionPerformed(ActionEvent e) {
365 words = true;
366 refreshBooks();
367 }
368 });
369 view.add(vwords);
370 bar.add(view);
371
372 JMenu sources = new JMenu("Sources");
373 sources.setMnemonic(KeyEvent.VK_S);
374
375 List<String> tt = reader.getLibrary().getSources();
376 tt.add(0, null);
377 for (final String type : tt) {
378 JMenuItem item = new JMenuItem(type == null ? "All" : type);
379 item.addActionListener(new ActionListener() {
380 @Override
381 public void actionPerformed(ActionEvent e) {
382 removeBookPanes();
383 addBookPane(type, true);
384 refreshBooks();
385 }
386 });
387 sources.add(item);
388
389 if (type == null) {
390 sources.addSeparator();
391 }
392 }
393
394 bar.add(sources);
395
396 JMenu authors = new JMenu("Authors");
397 authors.setMnemonic(KeyEvent.VK_A);
398
399 List<String> aa = reader.getLibrary().getAuthors();
400 aa.add(0, null);
401 for (final String author : aa) {
402 JMenuItem item = new JMenuItem(author == null ? "All"
403 : author.isEmpty() ? "[unknown]" : author);
404 item.addActionListener(new ActionListener() {
405 @Override
406 public void actionPerformed(ActionEvent e) {
407 removeBookPanes();
408 addBookPane(author, false);
409 refreshBooks();
410 }
411 });
412 authors.add(item);
413
414 if (author == null || author.isEmpty()) {
415 authors.addSeparator();
416 }
417 }
418
419 bar.add(authors);
420
421 JMenu options = new JMenu("Options");
422 options.setMnemonic(KeyEvent.VK_O);
423 options.add(createMenuItemConfig());
424 options.add(createMenuItemUiConfig());
425 bar.add(options);
426
427 return bar;
428 }
429
430 /**
431 * Create the Fanfix Configuration menu item.
432 *
433 * @return the item
434 */
435 private JMenuItem createMenuItemConfig() {
436 final String title = "Fanfix Configuration";
437 JMenuItem item = new JMenuItem(title);
438 item.setMnemonic(KeyEvent.VK_F);
439
440 item.addActionListener(new ActionListener() {
441 @Override
442 public void actionPerformed(ActionEvent e) {
443 ConfigEditor<Config> ed = new ConfigEditor<Config>(
444 Config.class, Instance.getConfig(),
445 "This is where you configure the options of the program.");
446 JFrame frame = new JFrame(title);
447 frame.add(ed);
448 frame.setSize(800, 600);
449 frame.setVisible(true);
450 }
451 });
452
453 return item;
454 }
455
456 /**
457 * Create the UI Configuration menu item.
458 *
459 * @return the item
460 */
461 private JMenuItem createMenuItemUiConfig() {
462 final String title = "UI Configuration";
463 JMenuItem item = new JMenuItem(title);
464 item.setMnemonic(KeyEvent.VK_U);
465
466 item.addActionListener(new ActionListener() {
467 @Override
468 public void actionPerformed(ActionEvent e) {
469 ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
470 UiConfig.class, Instance.getUiConfig(),
471 "This is where you configure the graphical appearence of the program.");
472 JFrame frame = new JFrame(title);
473 frame.add(ed);
474 frame.setSize(800, 600);
475 frame.setVisible(true);
476 }
477 });
478
479 return item;
480 }
481
482 /**
483 * Create the export menu item.
484 *
485 * @return the item
486 */
487 private JMenuItem createMenuItemExport() {
488 final JFileChooser fc = new JFileChooser();
489 fc.setAcceptAllFileFilterUsed(false);
490
491 final Map<FileFilter, OutputType> filters = new HashMap<FileFilter, OutputType>();
492 for (OutputType type : OutputType.values()) {
493 String ext = type.getDefaultExtension(false);
494 String desc = type.getDesc(false);
495
496 if (ext == null || ext.isEmpty()) {
497 filters.put(createAllFilter(desc), type);
498 } else {
499 filters.put(new FileNameExtensionFilter(desc, ext), type);
500 }
501 }
502
503 // First the "ALL" filters, then, the extension filters
504 for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
505 if (!(entry.getKey() instanceof FileNameExtensionFilter)) {
506 fc.addChoosableFileFilter(entry.getKey());
507 }
508 }
509 for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
510 if (entry.getKey() instanceof FileNameExtensionFilter) {
511 fc.addChoosableFileFilter(entry.getKey());
512 }
513 }
514 //
515
516 JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S);
517 export.addActionListener(new ActionListener() {
518 @Override
519 public void actionPerformed(ActionEvent e) {
520 if (selectedBook != null) {
521 fc.showDialog(GuiReaderFrame.this, "Save");
522 if (fc.getSelectedFile() != null) {
523 final OutputType type = filters.get(fc.getFileFilter());
524 final String path = fc.getSelectedFile()
525 .getAbsolutePath()
526 + type.getDefaultExtension(false);
527 final Progress pg = new Progress();
528 outOfUi(pg, new Runnable() {
529 @Override
530 public void run() {
531 try {
532 reader.getLibrary().export(
533 selectedBook.getMeta().getLuid(),
534 type, path, pg);
535 } catch (IOException e) {
536 Instance.syserr(e);
537 }
538 }
539 });
540 }
541 }
542 }
543 });
544
545 return export;
546 }
547
548 /**
549 * Create a {@link FileFilter} that accepts all files and return the given
550 * description.
551 *
552 * @param desc
553 * the description
554 *
555 * @return the filter
556 */
557 private FileFilter createAllFilter(final String desc) {
558 return new FileFilter() {
559 @Override
560 public String getDescription() {
561 return desc;
562 }
563
564 @Override
565 public boolean accept(File f) {
566 return true;
567 }
568 };
569 }
570
571 /**
572 * Create the refresh (delete cache) menu item.
573 *
574 * @return the item
575 */
576 private JMenuItem createMenuItemClearCache() {
577 JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C);
578 refresh.addActionListener(new ActionListener() {
579 @Override
580 public void actionPerformed(ActionEvent e) {
581 if (selectedBook != null) {
582 outOfUi(null, new Runnable() {
583 @Override
584 public void run() {
585 reader.clearLocalReaderCache(selectedBook.getMeta()
586 .getLuid());
587 selectedBook.setCached(false);
588 SwingUtilities.invokeLater(new Runnable() {
589 @Override
590 public void run() {
591 selectedBook.repaint();
592 }
593 });
594 }
595 });
596 }
597 }
598 });
599
600 return refresh;
601 }
602
603 /**
604 * Create the delete menu item.
605 *
606 * @return the item
607 */
608 private JMenuItem createMenuItemMove() {
609 JMenu moveTo = new JMenu("Move to...");
610 moveTo.setMnemonic(KeyEvent.VK_M);
611
612 List<String> types = new ArrayList<String>();
613 types.add(null);
614 types.addAll(reader.getLibrary().getSources());
615
616 for (String type : types) {
617 JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
618
619 moveTo.add(item);
620 if (type == null) {
621 moveTo.addSeparator();
622 }
623
624 final String ftype = type;
625 item.addActionListener(new ActionListener() {
626 @Override
627 public void actionPerformed(ActionEvent e) {
628 if (selectedBook != null) {
629 String type = ftype;
630 if (type == null) {
631 Object rep = JOptionPane.showInputDialog(
632 GuiReaderFrame.this, "Move to:",
633 "Moving story",
634 JOptionPane.QUESTION_MESSAGE, null, null,
635 selectedBook.getMeta().getSource());
636
637 if (rep == null) {
638 return;
639 }
640
641 type = rep.toString();
642 }
643
644 final String ftype = type;
645 outOfUi(null, new Runnable() {
646 @Override
647 public void run() {
648 reader.changeType(selectedBook.getMeta()
649 .getLuid(), ftype);
650
651 selectedBook = null;
652
653 SwingUtilities.invokeLater(new Runnable() {
654 @Override
655 public void run() {
656 setJMenuBar(createMenu());
657 }
658 });
659 }
660 });
661 }
662 }
663 });
664 }
665
666 return moveTo;
667 }
668
669 /**
670 * Create the redownload (then delete original) menu item.
671 *
672 * @return the item
673 */
674 private JMenuItem createMenuItemRedownload() {
675 JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R);
676 refresh.addActionListener(new ActionListener() {
677 @Override
678 public void actionPerformed(ActionEvent e) {
679 if (selectedBook != null) {
680 final MetaData meta = selectedBook.getMeta();
681 imprt(meta.getUrl(), new Runnable() {
682 @Override
683 public void run() {
684 reader.delete(meta.getLuid());
685 GuiReaderFrame.this.selectedBook = null;
686 }
687 }, "Removing old copy");
688 }
689 }
690 });
691
692 return refresh;
693 }
694
695 /**
696 * Create the delete menu item.
697 *
698 * @return the item
699 */
700 private JMenuItem createMenuItemDelete() {
701 JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D);
702 delete.addActionListener(new ActionListener() {
703 @Override
704 public void actionPerformed(ActionEvent e) {
705 if (selectedBook != null) {
706 outOfUi(null, new Runnable() {
707 @Override
708 public void run() {
709 reader.delete(selectedBook.getMeta().getLuid());
710 selectedBook = null;
711 }
712 });
713 }
714 }
715 });
716
717 return delete;
718 }
719
720 /**
721 * Create the open menu item for a book or a source (no LUID).
722 *
723 * @return the item
724 */
725 private JMenuItem createMenuItemOpenBook() {
726 JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O);
727 open.addActionListener(new ActionListener() {
728 @Override
729 public void actionPerformed(ActionEvent e) {
730 if (selectedBook != null) {
731 if (selectedBook.getMeta().getLuid() == null) {
732 removeBookPanes();
733 addBookPane(selectedBook.getMeta().getSource(), true);
734 refreshBooks();
735 } else {
736 openBook(selectedBook);
737 }
738 }
739 }
740 });
741
742 return open;
743 }
744
745 /**
746 * Create the SetCover menu item for a book to change the linked source
747 * cover.
748 *
749 * @return the item
750 */
751 private JMenuItem createMenuItemSetCover() {
752 JMenuItem open = new JMenuItem("Set as cover for source", KeyEvent.VK_C);
753 open.addActionListener(new ActionListener() {
754 @Override
755 public void actionPerformed(ActionEvent e) {
756 if (selectedBook != null) {
757 reader.getLibrary().setSourceCover(
758 selectedBook.getMeta().getSource(),
759 selectedBook.getMeta().getLuid());
760 }
761 }
762 });
763
764 return open;
765 }
766
767 /**
768 * Open a {@link GuiReaderBook} item.
769 *
770 * @param book
771 * the {@link GuiReaderBook} to open
772 */
773 private void openBook(final GuiReaderBook book) {
774 final Progress pg = new Progress();
775 outOfUi(pg, new Runnable() {
776 @Override
777 public void run() {
778 try {
779 reader.read(book.getMeta().getLuid(), pg);
780 SwingUtilities.invokeLater(new Runnable() {
781 @Override
782 public void run() {
783 book.setCached(true);
784 }
785 });
786 } catch (IOException e) {
787 // TODO: error message?
788 Instance.syserr(e);
789 }
790 }
791 });
792 }
793
794 /**
795 * Process the given action out of the Swing UI thread and link the given
796 * {@link ProgressBar} to the action.
797 * <p>
798 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
799 * to done when the action is done.
800 *
801 * @param progress
802 * the {@link ProgressBar} or NULL
803 * @param run
804 * the action to run
805 */
806 private void outOfUi(Progress progress, final Runnable run) {
807 final Progress pg = new Progress();
808 final Progress reload = new Progress("Reload books");
809 if (progress == null) {
810 progress = new Progress();
811 }
812
813 pg.addProgress(progress, 90);
814 pg.addProgress(reload, 10);
815
816 invalidate();
817 pgBar.setProgress(pg);
818 validate();
819 setEnabled(false);
820
821 new Thread(new Runnable() {
822 @Override
823 public void run() {
824 run.run();
825 refreshBooks();
826 reload.done();
827 if (!pg.isDone()) {
828 // will trigger pgBar ActionListener:
829 pg.done();
830 }
831 }
832 }, "outOfUi thread").start();
833 }
834
835 /**
836 * Import a {@link Story} into the main {@link LocalLibrary}.
837 * <p>
838 * Should be called inside the UI thread.
839 *
840 * @param askUrl
841 * TRUE for an {@link URL}, false for a {@link File}
842 */
843 private void imprt(boolean askUrl) {
844 JFileChooser fc = new JFileChooser();
845
846 Object url;
847 if (askUrl) {
848 String clipboard = "";
849 try {
850 clipboard = ("" + Toolkit.getDefaultToolkit()
851 .getSystemClipboard().getData(DataFlavor.stringFlavor))
852 .trim();
853 } catch (Exception e) {
854 // No data will be handled
855 }
856
857 if (clipboard == null || !clipboard.startsWith("http")) {
858 clipboard = "";
859 }
860
861 url = JOptionPane.showInputDialog(GuiReaderFrame.this,
862 "url of the story to import?", "Importing from URL",
863 JOptionPane.QUESTION_MESSAGE, null, null, clipboard);
864 } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) {
865 url = fc.getSelectedFile().getAbsolutePath();
866 } else {
867 url = null;
868 }
869
870 if (url != null && !url.toString().isEmpty()) {
871 imprt(url.toString(), null, null);
872 }
873 }
874
875 /**
876 * Actually import the {@link Story} into the main {@link LocalLibrary}.
877 * <p>
878 * Should be called inside the UI thread.
879 *
880 * @param url
881 * the {@link Story} to import by {@link URL}
882 * @param onSuccess
883 * Action to execute on success
884 */
885 private void imprt(final String url, final Runnable onSuccess,
886 String onSuccessPgName) {
887 final Progress pg = new Progress();
888 final Progress pgImprt = new Progress();
889 final Progress pgOnSuccess = new Progress(onSuccessPgName);
890 pg.addProgress(pgImprt, 95);
891 pg.addProgress(pgOnSuccess, 5);
892
893 outOfUi(pg, new Runnable() {
894 @Override
895 public void run() {
896 Exception ex = null;
897 try {
898 reader.getLibrary().imprt(BasicReader.getUrl(url), pgImprt);
899 } catch (IOException e) {
900 ex = e;
901 }
902
903 final Exception e = ex;
904
905 final boolean ok = (e == null);
906
907 pgOnSuccess.setProgress(0);
908 if (!ok) {
909 Instance.syserr(e);
910 SwingUtilities.invokeLater(new Runnable() {
911 @Override
912 public void run() {
913 JOptionPane.showMessageDialog(GuiReaderFrame.this,
914 "Cannot import: " + url, e.getMessage(),
915 JOptionPane.ERROR_MESSAGE);
916 }
917 });
918 } else {
919 if (onSuccess != null) {
920 onSuccess.run();
921 }
922 }
923 pgOnSuccess.done();
924 }
925 });
926 }
927
928 /**
929 * Enables or disables this component, depending on the value of the
930 * parameter <code>b</code>. An enabled component can respond to user input
931 * and generate events. Components are enabled initially by default.
932 * <p>
933 * Disabling this component will also affect its children.
934 *
935 * @param b
936 * If <code>true</code>, this component is enabled; otherwise
937 * this component is disabled
938 */
939 @Override
940 public void setEnabled(boolean b) {
941 if (bar != null) {
942 bar.setEnabled(b);
943 }
944
945 for (GuiReaderGroup group : booksByType.keySet()) {
946 group.setEnabled(b);
947 }
948 for (GuiReaderGroup group : booksByAuthor.keySet()) {
949 group.setEnabled(b);
950 }
951 super.setEnabled(b);
952 repaint();
953 }
954 }