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