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