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