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