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