gui: code cleanup
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderFrame.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Frame;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.KeyEvent;
8 import java.awt.event.WindowEvent;
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import javax.swing.JFileChooser;
17 import javax.swing.JFrame;
18 import javax.swing.JMenu;
19 import javax.swing.JMenuBar;
20 import javax.swing.JMenuItem;
21 import javax.swing.JOptionPane;
22 import javax.swing.JPopupMenu;
23 import javax.swing.SwingUtilities;
24 import javax.swing.filechooser.FileFilter;
25 import javax.swing.filechooser.FileNameExtensionFilter;
26
27 import be.nikiroo.fanfix.Instance;
28 import be.nikiroo.fanfix.bundles.Config;
29 import be.nikiroo.fanfix.bundles.UiConfig;
30 import be.nikiroo.fanfix.data.MetaData;
31 import be.nikiroo.fanfix.data.Story;
32 import be.nikiroo.fanfix.library.LocalLibrary;
33 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
34 import be.nikiroo.fanfix.reader.BasicReader;
35 import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.FrameHelper;
36 import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.StoryRunnable;
37 import be.nikiroo.utils.Progress;
38 import be.nikiroo.utils.Version;
39 import be.nikiroo.utils.ui.ConfigEditor;
40
41 /**
42 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
43 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
44 * way to copy them to the {@link GuiReader} cache (
45 * {@link BasicReader#getLibrary()}), read them, delete them...
46 *
47 * @author niki
48 */
49 class GuiReaderFrame extends JFrame implements FrameHelper {
50 private static final long serialVersionUID = 1L;
51 private GuiReader reader;
52 private GuiReaderMainPanel helpee;
53
54 private enum MoveAction {
55 SOURCE, TITLE, AUTHOR
56 }
57
58 /**
59 * Create a new {@link GuiReaderFrame}.
60 *
61 * @param reader
62 * the associated {@link GuiReader} to forward some commands and
63 * access its {@link LocalLibrary}
64 * @param type
65 * the type of {@link Story} to load, or NULL for all types
66 */
67 public GuiReaderFrame(GuiReader reader, String type) {
68 super(String.format("Fanfix %s Library", Version.getCurrentVersion()));
69
70 this.reader = reader;
71
72 // TODO: should we still have that??
73 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
74
75 helpee = new GuiReaderMainPanel(this, type);
76
77 setSize(800, 600);
78 setLayout(new BorderLayout());
79 add(helpee);
80
81 setVisible(true);
82 }
83
84 @Override
85 public JPopupMenu createBookPopup() {
86 JPopupMenu popup = new JPopupMenu();
87 popup.add(createMenuItemOpenBook());
88 popup.addSeparator();
89 popup.add(createMenuItemExport());
90 popup.add(createMenuItemMoveTo(true));
91 popup.add(createMenuItemSetCover());
92 popup.add(createMenuItemClearCache());
93 popup.add(createMenuItemRedownload());
94 popup.addSeparator();
95 popup.add(createMenuItemRename(true));
96 popup.add(createMenuItemSetAuthor(true));
97 popup.addSeparator();
98 popup.add(createMenuItemDelete());
99 popup.addSeparator();
100 popup.add(createMenuItemProperties());
101 return popup;
102 }
103
104 @Override
105 public JPopupMenu createSourcePopup() {
106 JPopupMenu popup = new JPopupMenu();
107 popup.add(createMenuItemOpenBook());
108 return popup;
109 }
110
111 @Override
112 public void createMenu(boolean libOk) {
113 JMenuBar bar = new JMenuBar();
114
115 JMenu file = new JMenu("File");
116 file.setMnemonic(KeyEvent.VK_F);
117
118 JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U);
119 imprt.addActionListener(new ActionListener() {
120 @Override
121 public void actionPerformed(ActionEvent e) {
122 helpee.imprt(true);
123 }
124 });
125 JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F);
126 imprtF.addActionListener(new ActionListener() {
127 @Override
128 public void actionPerformed(ActionEvent e) {
129 helpee.imprt(false);
130 }
131 });
132 JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X);
133 exit.addActionListener(new ActionListener() {
134 @Override
135 public void actionPerformed(ActionEvent e) {
136 GuiReaderFrame.this.dispatchEvent(new WindowEvent(
137 GuiReaderFrame.this, WindowEvent.WINDOW_CLOSING));
138 }
139 });
140
141 file.add(createMenuItemOpenBook());
142 file.add(createMenuItemExport());
143 file.add(createMenuItemMoveTo(libOk));
144 file.addSeparator();
145 file.add(imprt);
146 file.add(imprtF);
147 file.addSeparator();
148 file.add(createMenuItemRename(libOk));
149 file.add(createMenuItemSetAuthor(libOk));
150 file.addSeparator();
151 file.add(exit);
152
153 bar.add(file);
154
155 JMenu edit = new JMenu("Edit");
156 edit.setMnemonic(KeyEvent.VK_E);
157
158 edit.add(createMenuItemClearCache());
159 edit.add(createMenuItemRedownload());
160 edit.addSeparator();
161 edit.add(createMenuItemDelete());
162
163 bar.add(edit);
164
165 JMenu view = new JMenu("View");
166 view.setMnemonic(KeyEvent.VK_V);
167 JMenuItem vauthors = new JMenuItem("Author");
168 vauthors.setMnemonic(KeyEvent.VK_A);
169 vauthors.addActionListener(new ActionListener() {
170 @Override
171 public void actionPerformed(ActionEvent e) {
172 helpee.setWords(false);
173 helpee.refreshBooks();
174 }
175 });
176 view.add(vauthors);
177 JMenuItem vwords = new JMenuItem("Word count");
178 vwords.setMnemonic(KeyEvent.VK_W);
179 vwords.addActionListener(new ActionListener() {
180 @Override
181 public void actionPerformed(ActionEvent e) {
182 helpee.setWords(true);
183 helpee.refreshBooks();
184 }
185 });
186 view.add(vwords);
187 bar.add(view);
188
189 Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
190 if (libOk) {
191 groupedSources = reader.getLibrary().getSourcesGrouped();
192 }
193 JMenu sources = new JMenu("Sources");
194 sources.setMnemonic(KeyEvent.VK_S);
195 populateMenuSA(sources, groupedSources, true);
196 bar.add(sources);
197
198 Map<String, List<String>> goupedAuthors = new HashMap<String, List<String>>();
199 if (libOk) {
200 goupedAuthors = reader.getLibrary().getAuthorsGrouped();
201 }
202 JMenu authors = new JMenu("Authors");
203 authors.setMnemonic(KeyEvent.VK_A);
204 populateMenuSA(authors, goupedAuthors, false);
205 bar.add(authors);
206
207 JMenu options = new JMenu("Options");
208 options.setMnemonic(KeyEvent.VK_O);
209 options.add(createMenuItemConfig());
210 options.add(createMenuItemUiConfig());
211 bar.add(options);
212
213 setJMenuBar(bar);
214 }
215
216 // "" = [unknown]
217 private void populateMenuSA(JMenu menu,
218 Map<String, List<String>> groupedValues, boolean type) {
219
220 // "All" and "Listing" special items
221 JMenuItem item = new JMenuItem("All");
222 item.addActionListener(getActionOpenList(type, false));
223 menu.add(item);
224 item = new JMenuItem("Listing");
225 item.addActionListener(getActionOpenList(type, true));
226 menu.add(item);
227 menu.addSeparator();
228
229 for (final String value : groupedValues.keySet()) {
230 List<String> list = groupedValues.get(value);
231 if (type && list.size() == 1 && list.get(0).isEmpty()) {
232 // leaf item source/type
233 item = new JMenuItem(value.isEmpty() ? "[unknown]" : value);
234 item.addActionListener(getActionOpen(value, type));
235 menu.add(item);
236 } else {
237 JMenu dir;
238 if (!type && groupedValues.size() == 1) {
239 // only one group of authors
240 dir = menu;
241 } else {
242 dir = new JMenu(value.isEmpty() ? "[unknown]" : value);
243 }
244
245 for (String sub : list) {
246 // " " instead of "" for the visual height
247 String itemName = sub.isEmpty() ? " " : sub;
248 String actualValue = value;
249
250 if (type) {
251 if (!sub.isEmpty()) {
252 actualValue += "/" + sub;
253 }
254 } else {
255 actualValue = sub;
256 }
257
258 item = new JMenuItem(itemName);
259 item.addActionListener(getActionOpen(actualValue, type));
260 dir.add(item);
261 }
262
263 if (menu != dir) {
264 menu.add(dir);
265 }
266 }
267 }
268 }
269
270 /**
271 * Return an {@link ActionListener} that will set the given source (type) as
272 * the selected/displayed one.
273 *
274 * @param type
275 * the type (source) to select, cannot be NULL
276 *
277 * @return the {@link ActionListener}
278 */
279 private ActionListener getActionOpen(final String source, final boolean type) {
280 return new ActionListener() {
281 @Override
282 public void actionPerformed(ActionEvent e) {
283 helpee.removeBookPanes();
284 helpee.addBookPane(source, type);
285 helpee.refreshBooks();
286 }
287 };
288 }
289
290 private ActionListener getActionOpenList(final boolean type,
291 final boolean listMode) {
292 return new ActionListener() {
293 @Override
294 public void actionPerformed(ActionEvent e) {
295 helpee.removeBookPanes();
296 helpee.addBookPane(type, listMode);
297 helpee.refreshBooks();
298 }
299 };
300 }
301
302 /**
303 * Create the Fanfix Configuration menu item.
304 *
305 * @return the item
306 */
307 private JMenuItem createMenuItemConfig() {
308 final String title = "Fanfix Configuration";
309 JMenuItem item = new JMenuItem(title);
310 item.setMnemonic(KeyEvent.VK_F);
311
312 item.addActionListener(new ActionListener() {
313 @Override
314 public void actionPerformed(ActionEvent e) {
315 ConfigEditor<Config> ed = new ConfigEditor<Config>(
316 Config.class, Instance.getConfig(),
317 "This is where you configure the options of the program.");
318 JFrame frame = new JFrame(title);
319 frame.add(ed);
320 frame.setSize(800, 600);
321 frame.setVisible(true);
322 }
323 });
324
325 return item;
326 }
327
328 /**
329 * Create the UI Configuration menu item.
330 *
331 * @return the item
332 */
333 private JMenuItem createMenuItemUiConfig() {
334 final String title = "UI Configuration";
335 JMenuItem item = new JMenuItem(title);
336 item.setMnemonic(KeyEvent.VK_U);
337
338 item.addActionListener(new ActionListener() {
339 @Override
340 public void actionPerformed(ActionEvent e) {
341 ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
342 UiConfig.class, Instance.getUiConfig(),
343 "This is where you configure the graphical appearence of the program.");
344 JFrame frame = new JFrame(title);
345 frame.add(ed);
346 frame.setSize(800, 600);
347 frame.setVisible(true);
348 }
349 });
350
351 return item;
352 }
353
354 /**
355 * Create the export menu item.
356 *
357 * @return the item
358 */
359 private JMenuItem createMenuItemExport() {
360 final JFileChooser fc = new JFileChooser();
361 fc.setAcceptAllFileFilterUsed(false);
362
363 final Map<FileFilter, OutputType> filters = new HashMap<FileFilter, OutputType>();
364 for (OutputType type : OutputType.values()) {
365 String ext = type.getDefaultExtension(false);
366 String desc = type.getDesc(false);
367
368 if (ext == null || ext.isEmpty()) {
369 filters.put(createAllFilter(desc), type);
370 } else {
371 filters.put(new FileNameExtensionFilter(desc, ext), type);
372 }
373 }
374
375 // First the "ALL" filters, then, the extension filters
376 for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
377 if (!(entry.getKey() instanceof FileNameExtensionFilter)) {
378 fc.addChoosableFileFilter(entry.getKey());
379 }
380 }
381 for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
382 if (entry.getKey() instanceof FileNameExtensionFilter) {
383 fc.addChoosableFileFilter(entry.getKey());
384 }
385 }
386 //
387
388 JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S);
389 export.addActionListener(new ActionListener() {
390 @Override
391 public void actionPerformed(ActionEvent e) {
392 final GuiReaderBook selectedBook = helpee.getSelectedBook();
393 if (selectedBook != null) {
394 fc.showDialog(GuiReaderFrame.this, "Save");
395 if (fc.getSelectedFile() != null) {
396 final OutputType type = filters.get(fc.getFileFilter());
397 final String path = fc.getSelectedFile()
398 .getAbsolutePath()
399 + type.getDefaultExtension(false);
400 final Progress pg = new Progress();
401 helpee.outOfUi(pg, new Runnable() {
402 @Override
403 public void run() {
404 try {
405 reader.getLibrary().export(
406 selectedBook.getMeta().getLuid(),
407 type, path, pg);
408 } catch (IOException e) {
409 Instance.getTraceHandler().error(e);
410 }
411 }
412 });
413 }
414 }
415 }
416 });
417
418 return export;
419 }
420
421 /**
422 * Create a {@link FileFilter} that accepts all files and return the given
423 * description.
424 *
425 * @param desc
426 * the description
427 *
428 * @return the filter
429 */
430 private FileFilter createAllFilter(final String desc) {
431 return new FileFilter() {
432 @Override
433 public String getDescription() {
434 return desc;
435 }
436
437 @Override
438 public boolean accept(File f) {
439 return true;
440 }
441 };
442 }
443
444 /**
445 * Create the refresh (delete cache) menu item.
446 *
447 * @return the item
448 */
449 private JMenuItem createMenuItemClearCache() {
450 JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C);
451 refresh.addActionListener(new ActionListener() {
452 @Override
453 public void actionPerformed(ActionEvent e) {
454 final GuiReaderBook selectedBook = helpee.getSelectedBook();
455 if (selectedBook != null) {
456 helpee.outOfUi(null, new Runnable() {
457 @Override
458 public void run() {
459 reader.clearLocalReaderCache(selectedBook.getMeta()
460 .getLuid());
461 selectedBook.setCached(false);
462 GuiReaderCoverImager.clearIcon(selectedBook
463 .getMeta());
464 SwingUtilities.invokeLater(new Runnable() {
465 @Override
466 public void run() {
467 selectedBook.repaint();
468 }
469 });
470 }
471 });
472 }
473 }
474 });
475
476 return refresh;
477 }
478
479 /**
480 * Create the "move to" menu item.
481 *
482 * @param libOk
483 * the library can be queried
484 *
485 * @return the item
486 */
487 private JMenuItem createMenuItemMoveTo(boolean libOk) {
488 JMenu changeTo = new JMenu("Move to");
489 changeTo.setMnemonic(KeyEvent.VK_M);
490
491 Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
492 if (libOk) {
493 groupedSources = reader.getLibrary().getSourcesGrouped();
494 }
495
496 JMenuItem item = new JMenuItem("New type...");
497 item.addActionListener(createMoveAction(MoveAction.SOURCE, null));
498 changeTo.add(item);
499 changeTo.addSeparator();
500
501 for (final String type : groupedSources.keySet()) {
502 List<String> list = groupedSources.get(type);
503 if (list.size() == 1 && list.get(0).isEmpty()) {
504 item = new JMenuItem(type);
505 item.addActionListener(createMoveAction(MoveAction.SOURCE, type));
506 changeTo.add(item);
507 } else {
508 JMenu dir = new JMenu(type);
509 for (String sub : list) {
510 // " " instead of "" for the visual height
511 String itemName = sub.isEmpty() ? " " : sub;
512 String actualType = type;
513 if (!sub.isEmpty()) {
514 actualType += "/" + sub;
515 }
516
517 item = new JMenuItem(itemName);
518 item.addActionListener(createMoveAction(MoveAction.SOURCE,
519 actualType));
520 dir.add(item);
521 }
522 changeTo.add(dir);
523 }
524 }
525
526 return changeTo;
527 }
528
529 /**
530 * Create the "set author" menu item.
531 *
532 * @param libOk
533 * the library can be queried
534 *
535 * @return the item
536 */
537 private JMenuItem createMenuItemSetAuthor(boolean libOk) {
538 JMenu changeTo = new JMenu("Set author");
539 changeTo.setMnemonic(KeyEvent.VK_A);
540
541 // New author
542 JMenuItem newItem = new JMenuItem("New author...");
543 changeTo.add(newItem);
544 changeTo.addSeparator();
545 newItem.addActionListener(createMoveAction(MoveAction.AUTHOR, null));
546
547 // Existing authors
548 if (libOk) {
549 Map<String, List<String>> groupedAuthors = reader.getLibrary()
550 .getAuthorsGrouped();
551
552 if (groupedAuthors.size() > 1) {
553 for (String key : groupedAuthors.keySet()) {
554 JMenu group = new JMenu(key);
555 for (String value : groupedAuthors.get(key)) {
556 JMenuItem item = new JMenuItem(value);
557 item.addActionListener(createMoveAction(
558 MoveAction.AUTHOR, value));
559 group.add(item);
560 }
561 changeTo.add(group);
562 }
563 } else if (groupedAuthors.size() == 1) {
564 for (String value : groupedAuthors.values().iterator().next()) {
565 JMenuItem item = new JMenuItem(value);
566 item.addActionListener(createMoveAction(MoveAction.AUTHOR,
567 value));
568 changeTo.add(item);
569 }
570 }
571 }
572
573 return changeTo;
574 }
575
576 /**
577 * Create the "rename" menu item.
578 *
579 * @param libOk
580 * the library can be queried
581 *
582 * @return the item
583 */
584 private JMenuItem createMenuItemRename(
585 @SuppressWarnings("unused") boolean libOk) {
586 JMenuItem changeTo = new JMenuItem("Rename...");
587 changeTo.setMnemonic(KeyEvent.VK_R);
588 changeTo.addActionListener(createMoveAction(MoveAction.TITLE, null));
589 return changeTo;
590 }
591
592 private ActionListener createMoveAction(final MoveAction what,
593 final String type) {
594 return new ActionListener() {
595 @Override
596 public void actionPerformed(ActionEvent e) {
597 final GuiReaderBook selectedBook = helpee.getSelectedBook();
598 if (selectedBook != null) {
599 String changeTo = type;
600 if (type == null) {
601 String init = "";
602 if (what == MoveAction.SOURCE) {
603 init = selectedBook.getMeta().getSource();
604 } else if (what == MoveAction.TITLE) {
605 init = selectedBook.getMeta().getTitle();
606 } else if (what == MoveAction.AUTHOR) {
607 init = selectedBook.getMeta().getAuthor();
608 }
609
610 Object rep = JOptionPane.showInputDialog(
611 GuiReaderFrame.this, "Move to:",
612 "Moving story", JOptionPane.QUESTION_MESSAGE,
613 null, null, init);
614
615 if (rep == null) {
616 return;
617 }
618
619 changeTo = rep.toString();
620 }
621
622 final String fChangeTo = changeTo;
623 helpee.outOfUi(null, new Runnable() {
624 @Override
625 public void run() {
626 if (what.equals("SOURCE")) {
627 reader.changeSource(selectedBook.getMeta()
628 .getLuid(), fChangeTo);
629 } else if (what.equals("TITLE")) {
630 reader.changeTitle(selectedBook.getMeta()
631 .getLuid(), fChangeTo);
632 } else if (what.equals("AUTHOR")) {
633 reader.changeAuthor(selectedBook.getMeta()
634 .getLuid(), fChangeTo);
635 }
636
637 helpee.unsetSelectedBook();
638
639 SwingUtilities.invokeLater(new Runnable() {
640 @Override
641 public void run() {
642 createMenu(true);
643 }
644 });
645 }
646 });
647 }
648 }
649 };
650 }
651
652 /**
653 * Create the redownload (then delete original) menu item.
654 *
655 * @return the item
656 */
657 private JMenuItem createMenuItemRedownload() {
658 JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R);
659 refresh.addActionListener(new ActionListener() {
660 @Override
661 public void actionPerformed(ActionEvent e) {
662 final GuiReaderBook selectedBook = helpee.getSelectedBook();
663 if (selectedBook != null) {
664 final MetaData meta = selectedBook.getMeta();
665 helpee.imprt(meta.getUrl(), new StoryRunnable() {
666 @Override
667 public void run(Story story) {
668 reader.delete(meta.getLuid());
669 helpee.unsetSelectedBook();
670 MetaData newMeta = story.getMeta();
671 if (!newMeta.getSource().equals(meta.getSource())) {
672 reader.changeSource(newMeta.getLuid(),
673 meta.getSource());
674 }
675 }
676 }, "Removing old copy");
677 }
678 }
679 });
680
681 return refresh;
682 }
683
684 /**
685 * Create the delete menu item.
686 *
687 * @return the item
688 */
689 private JMenuItem createMenuItemDelete() {
690 JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D);
691 delete.addActionListener(new ActionListener() {
692 @Override
693 public void actionPerformed(ActionEvent e) {
694 final GuiReaderBook selectedBook = helpee.getSelectedBook();
695 if (selectedBook != null) {
696 helpee.outOfUi(null, new Runnable() {
697 @Override
698 public void run() {
699 reader.delete(selectedBook.getMeta().getLuid());
700 helpee.unsetSelectedBook();
701 }
702 });
703 }
704 }
705 });
706
707 return delete;
708 }
709
710 /**
711 * Create the properties menu item.
712 *
713 * @return the item
714 */
715 private JMenuItem createMenuItemProperties() {
716 JMenuItem delete = new JMenuItem("Properties", KeyEvent.VK_P);
717 delete.addActionListener(new ActionListener() {
718 @Override
719 public void actionPerformed(ActionEvent e) {
720 final GuiReaderBook selectedBook = helpee.getSelectedBook();
721 if (selectedBook != null) {
722 helpee.outOfUi(null, new Runnable() {
723 @Override
724 public void run() {
725 new GuiReaderPropertiesFrame(reader, selectedBook
726 .getMeta()).setVisible(true);
727 }
728 });
729 }
730 }
731 });
732
733 return delete;
734 }
735
736 /**
737 * Create the open menu item for a book or a source/type (no LUID).
738 *
739 * @return the item
740 */
741 public JMenuItem createMenuItemOpenBook() {
742 JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O);
743 open.addActionListener(new ActionListener() {
744 @Override
745 public void actionPerformed(ActionEvent e) {
746 final GuiReaderBook selectedBook = helpee.getSelectedBook();
747 if (selectedBook != null) {
748 if (selectedBook.getMeta().getLuid() == null) {
749 helpee.removeBookPanes();
750 helpee.addBookPane(selectedBook.getMeta().getSource(),
751 true);
752 helpee.refreshBooks();
753 } else {
754 helpee.openBook(selectedBook);
755 }
756 }
757 }
758 });
759
760 return open;
761 }
762
763 /**
764 * Create the SetCover menu item for a book to change the linked source
765 * cover.
766 *
767 * @return the item
768 */
769 private JMenuItem createMenuItemSetCover() {
770 JMenuItem open = new JMenuItem("Set as cover for source", KeyEvent.VK_C);
771 open.addActionListener(new ActionListener() {
772 @Override
773 public void actionPerformed(ActionEvent e) {
774 final GuiReaderBook selectedBook = helpee.getSelectedBook();
775 if (selectedBook != null) {
776 reader.getLibrary().setSourceCover(
777 selectedBook.getMeta().getSource(),
778 selectedBook.getMeta().getLuid());
779 MetaData source = selectedBook.getMeta().clone();
780 source.setLuid(null);
781 GuiReaderCoverImager.clearIcon(source);
782 }
783 }
784 });
785
786 return open;
787 }
788
789 /**
790 * Display an error message and log the linked {@link Exception}.
791 *
792 * @param message
793 * the message
794 * @param title
795 * the title of the error message
796 * @param e
797 * the exception to log if any
798 */
799 public void error(final String message, final String title, Exception e) {
800 Instance.getTraceHandler().error(title + ": " + message);
801 if (e != null) {
802 Instance.getTraceHandler().error(e);
803 }
804
805 SwingUtilities.invokeLater(new Runnable() {
806 @Override
807 public void run() {
808 JOptionPane.showMessageDialog(GuiReaderFrame.this, message,
809 title, JOptionPane.ERROR_MESSAGE);
810 }
811 });
812 }
813
814 @Override
815 public GuiReader getReader() {
816 return reader;
817 }
818 }