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