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