1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.event
.ActionEvent
;
6 import java
.awt
.event
.ActionListener
;
7 import java
.awt
.event
.KeyEvent
;
8 import java
.awt
.event
.WindowEvent
;
10 import java
.io
.IOException
;
11 import java
.util
.HashMap
;
12 import java
.util
.List
;
14 import java
.util
.Map
.Entry
;
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
;
27 import be
.nikiroo
.fanfix
.Instance
;
28 import be
.nikiroo
.fanfix
.bundles
.Config
;
29 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
30 import be
.nikiroo
.fanfix
.data
.MetaData
;
31 import be
.nikiroo
.fanfix
.data
.Story
;
32 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
33 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
34 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
35 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
36 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderMainPanel
.FrameHelper
;
37 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderMainPanel
.StoryRunnable
;
38 import be
.nikiroo
.utils
.Progress
;
39 import be
.nikiroo
.utils
.Version
;
40 import be
.nikiroo
.utils
.ui
.ConfigEditor
;
43 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
44 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
45 * way to copy them to the {@link GuiReader} cache (
46 * {@link BasicReader#getLibrary()}), read them, delete them...
50 class GuiReaderFrame
extends JFrame
implements FrameHelper
{
51 private static final long serialVersionUID
= 1L;
52 private GuiReader reader
;
53 private GuiReaderMainPanel mainPanel
;
56 * The different modification actions you can use on {@link Story} items.
60 private enum ChangeAction
{
61 /** Change the source/type, that is, move it to another source. */
63 /** Change its name. */
65 /** Change its author. */
70 * Create a new {@link GuiReaderFrame}.
73 * the associated {@link GuiReader} to forward some commands and
74 * access its {@link LocalLibrary}
76 * the type of {@link Story} to load, or NULL for all types
78 public GuiReaderFrame(GuiReader reader
, String type
) {
79 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
83 mainPanel
= new GuiReaderMainPanel(this, type
);
86 setLayout(new BorderLayout());
91 public JPopupMenu
createBookPopup() {
92 JPopupMenu popup
= new JPopupMenu();
93 popup
.add(createMenuItemOpenBook());
95 popup
.add(createMenuItemExport());
96 popup
.add(createMenuItemMoveTo(true));
97 popup
.add(createMenuItemSetCoverForSource());
98 popup
.add(createMenuItemSetCoverForAuthor());
99 popup
.add(createMenuItemClearCache());
100 popup
.add(createMenuItemRedownload());
101 popup
.addSeparator();
102 popup
.add(createMenuItemRename(true));
103 popup
.add(createMenuItemSetAuthor(true));
104 popup
.addSeparator();
105 popup
.add(createMenuItemDelete());
106 popup
.addSeparator();
107 popup
.add(createMenuItemProperties());
112 public JPopupMenu
createSourcePopup() {
113 JPopupMenu popup
= new JPopupMenu();
114 popup
.add(createMenuItemOpenBook());
119 public void createMenu(boolean libOk
) {
120 JMenuBar bar
= new JMenuBar();
122 JMenu file
= new JMenu("File");
123 file
.setMnemonic(KeyEvent
.VK_F
);
125 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
126 imprt
.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e
) {
129 mainPanel
.imprt(true);
132 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
133 imprtF
.addActionListener(new ActionListener() {
135 public void actionPerformed(ActionEvent e
) {
136 mainPanel
.imprt(false);
139 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
140 exit
.addActionListener(new ActionListener() {
142 public void actionPerformed(ActionEvent e
) {
143 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
144 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
148 file
.add(createMenuItemOpenBook());
149 file
.add(createMenuItemExport());
150 file
.add(createMenuItemMoveTo(libOk
));
155 file
.add(createMenuItemRename(libOk
));
156 file
.add(createMenuItemSetAuthor(libOk
));
162 JMenu edit
= new JMenu("Edit");
163 edit
.setMnemonic(KeyEvent
.VK_E
);
165 edit
.add(createMenuItemClearCache());
166 edit
.add(createMenuItemRedownload());
168 edit
.add(createMenuItemDelete());
172 JMenu view
= new JMenu("View");
173 view
.setMnemonic(KeyEvent
.VK_V
);
174 JMenuItem vauthors
= new JMenuItem("Author");
175 vauthors
.setMnemonic(KeyEvent
.VK_A
);
176 vauthors
.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e
) {
179 mainPanel
.setWords(false);
180 mainPanel
.refreshBooks();
184 JMenuItem vwords
= new JMenuItem("Word count");
185 vwords
.setMnemonic(KeyEvent
.VK_W
);
186 vwords
.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent e
) {
189 mainPanel
.setWords(true);
190 mainPanel
.refreshBooks();
196 Map
<String
, List
<String
>> groupedSources
= new HashMap
<String
, List
<String
>>();
198 groupedSources
= reader
.getLibrary().getSourcesGrouped();
200 JMenu sources
= new JMenu("Sources");
201 sources
.setMnemonic(KeyEvent
.VK_S
);
202 populateMenuSA(sources
, groupedSources
, true);
205 Map
<String
, List
<String
>> goupedAuthors
= new HashMap
<String
, List
<String
>>();
207 goupedAuthors
= reader
.getLibrary().getAuthorsGrouped();
209 JMenu authors
= new JMenu("Authors");
210 authors
.setMnemonic(KeyEvent
.VK_A
);
211 populateMenuSA(authors
, goupedAuthors
, false);
214 JMenu options
= new JMenu("Options");
215 options
.setMnemonic(KeyEvent
.VK_O
);
216 options
.add(createMenuItemConfig());
217 options
.add(createMenuItemUiConfig());
224 private void populateMenuSA(JMenu menu
,
225 Map
<String
, List
<String
>> groupedValues
, boolean type
) {
227 // "All" and "Listing" special items
228 JMenuItem item
= new JMenuItem("All");
229 item
.addActionListener(getActionOpenList(type
, false));
231 item
= new JMenuItem("Listing");
232 item
.addActionListener(getActionOpenList(type
, true));
236 for (final String value
: groupedValues
.keySet()) {
237 List
<String
> list
= groupedValues
.get(value
);
238 if (type
&& list
.size() == 1 && list
.get(0).isEmpty()) {
239 // leaf item source/type
240 item
= new JMenuItem(value
.isEmpty() ?
"[unknown]" : value
);
241 item
.addActionListener(getActionOpen(value
, type
));
245 if (!type
&& groupedValues
.size() == 1) {
246 // only one group of authors
249 dir
= new JMenu(value
.isEmpty() ?
"[unknown]" : value
);
252 for (String sub
: list
) {
253 // " " instead of "" for the visual height
254 String itemName
= sub
;
255 if (itemName
.isEmpty()) {
256 itemName
= type ?
" " : "[unknown]";
259 String actualValue
= value
;
261 if (!sub
.isEmpty()) {
262 actualValue
+= "/" + sub
;
268 item
= new JMenuItem(itemName
);
269 item
.addActionListener(getActionOpen(actualValue
, type
));
281 * Return an {@link ActionListener} that will set the given source (type) as
282 * the selected/displayed one.
285 * the type (source) to select, cannot be NULL
287 * @return the {@link ActionListener}
289 private ActionListener
getActionOpen(final String source
, final boolean type
) {
290 return new ActionListener() {
292 public void actionPerformed(ActionEvent e
) {
293 mainPanel
.removeBookPanes();
294 mainPanel
.addBookPane(source
, type
);
295 mainPanel
.refreshBooks();
300 private ActionListener
getActionOpenList(final boolean type
,
301 final boolean listMode
) {
302 return new ActionListener() {
304 public void actionPerformed(ActionEvent e
) {
305 mainPanel
.removeBookPanes();
306 mainPanel
.addBookPane(type
, listMode
);
307 mainPanel
.refreshBooks();
313 * Create the Fanfix Configuration menu item.
317 private JMenuItem
createMenuItemConfig() {
318 final String title
= "Fanfix Configuration";
319 JMenuItem item
= new JMenuItem(title
);
320 item
.setMnemonic(KeyEvent
.VK_F
);
322 item
.addActionListener(new ActionListener() {
324 public void actionPerformed(ActionEvent e
) {
325 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
326 Config
.class, Instance
.getConfig(),
327 "This is where you configure the options of the program.");
328 JFrame frame
= new JFrame(title
);
330 frame
.setSize(800, 600);
331 frame
.setVisible(true);
339 * Create the UI Configuration menu item.
343 private JMenuItem
createMenuItemUiConfig() {
344 final String title
= "UI Configuration";
345 JMenuItem item
= new JMenuItem(title
);
346 item
.setMnemonic(KeyEvent
.VK_U
);
348 item
.addActionListener(new ActionListener() {
350 public void actionPerformed(ActionEvent e
) {
351 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
352 UiConfig
.class, Instance
.getUiConfig(),
353 "This is where you configure the graphical appearence of the program.");
354 JFrame frame
= new JFrame(title
);
356 frame
.setSize(800, 600);
357 frame
.setVisible(true);
365 * Create the export menu item.
369 private JMenuItem
createMenuItemExport() {
370 final JFileChooser fc
= new JFileChooser();
371 fc
.setAcceptAllFileFilterUsed(false);
373 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
374 for (OutputType type
: OutputType
.values()) {
375 String ext
= type
.getDefaultExtension(false);
376 String desc
= type
.getDesc(false);
378 if (ext
== null || ext
.isEmpty()) {
379 filters
.put(createAllFilter(desc
), type
);
381 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
385 // First the "ALL" filters, then, the extension filters
386 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
387 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
388 fc
.addChoosableFileFilter(entry
.getKey());
391 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
392 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
393 fc
.addChoosableFileFilter(entry
.getKey());
398 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
399 export
.addActionListener(new ActionListener() {
401 public void actionPerformed(ActionEvent e
) {
402 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
403 if (selectedBook
!= null) {
404 fc
.showDialog(GuiReaderFrame
.this, "Save");
405 if (fc
.getSelectedFile() != null) {
406 final OutputType type
= filters
.get(fc
.getFileFilter());
407 final String path
= fc
.getSelectedFile()
409 + type
.getDefaultExtension(false);
410 final Progress pg
= new Progress();
411 mainPanel
.outOfUi(pg
, new Runnable() {
415 reader
.getLibrary().export(
416 selectedBook
.getInfo().getMeta()
417 .getLuid(), type
, path
, pg
);
418 } catch (IOException e
) {
419 Instance
.getTraceHandler().error(e
);
432 * Create a {@link FileFilter} that accepts all files and return the given
440 private FileFilter
createAllFilter(final String desc
) {
441 return new FileFilter() {
443 public String
getDescription() {
448 public boolean accept(File f
) {
455 * Create the refresh (delete cache) menu item.
459 private JMenuItem
createMenuItemClearCache() {
460 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
461 refresh
.addActionListener(new ActionListener() {
463 public void actionPerformed(ActionEvent e
) {
464 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
465 if (selectedBook
!= null) {
466 mainPanel
.outOfUi(null, new Runnable() {
469 reader
.clearLocalReaderCache(selectedBook
.getInfo()
470 .getMeta().getLuid());
471 selectedBook
.setCached(false);
472 GuiReaderCoverImager
.clearIcon(selectedBook
474 SwingUtilities
.invokeLater(new Runnable() {
477 selectedBook
.repaint();
490 * Create the "move to" menu item.
493 * the library can be queried
497 private JMenuItem
createMenuItemMoveTo(boolean libOk
) {
498 JMenu changeTo
= new JMenu("Move to");
499 changeTo
.setMnemonic(KeyEvent
.VK_M
);
501 Map
<String
, List
<String
>> groupedSources
= new HashMap
<String
, List
<String
>>();
503 groupedSources
= reader
.getLibrary().getSourcesGrouped();
506 JMenuItem item
= new JMenuItem("New type...");
507 item
.addActionListener(createMoveAction(ChangeAction
.SOURCE
, null));
509 changeTo
.addSeparator();
511 for (final String type
: groupedSources
.keySet()) {
512 List
<String
> list
= groupedSources
.get(type
);
513 if (list
.size() == 1 && list
.get(0).isEmpty()) {
514 item
= new JMenuItem(type
);
515 item
.addActionListener(createMoveAction(ChangeAction
.SOURCE
,
519 JMenu dir
= new JMenu(type
);
520 for (String sub
: list
) {
521 // " " instead of "" for the visual height
522 String itemName
= sub
.isEmpty() ?
" " : sub
;
523 String actualType
= type
;
524 if (!sub
.isEmpty()) {
525 actualType
+= "/" + sub
;
528 item
= new JMenuItem(itemName
);
529 item
.addActionListener(createMoveAction(
530 ChangeAction
.SOURCE
, actualType
));
541 * Create the "set author" menu item.
544 * the library can be queried
548 private JMenuItem
createMenuItemSetAuthor(boolean libOk
) {
549 JMenu changeTo
= new JMenu("Set author");
550 changeTo
.setMnemonic(KeyEvent
.VK_A
);
553 JMenuItem newItem
= new JMenuItem("New author...");
554 changeTo
.add(newItem
);
555 changeTo
.addSeparator();
556 newItem
.addActionListener(createMoveAction(ChangeAction
.AUTHOR
, null));
560 Map
<String
, List
<String
>> groupedAuthors
= reader
.getLibrary()
561 .getAuthorsGrouped();
563 if (groupedAuthors
.size() > 1) {
564 for (String key
: groupedAuthors
.keySet()) {
565 JMenu group
= new JMenu(key
);
566 for (String value
: groupedAuthors
.get(key
)) {
567 JMenuItem item
= new JMenuItem(
568 value
.isEmpty() ?
"[unknown]" : value
);
569 item
.addActionListener(createMoveAction(
570 ChangeAction
.AUTHOR
, value
));
575 } else if (groupedAuthors
.size() == 1) {
576 for (String value
: groupedAuthors
.values().iterator().next()) {
577 JMenuItem item
= new JMenuItem(
578 value
.isEmpty() ?
"[unknown]" : value
);
579 item
.addActionListener(createMoveAction(
580 ChangeAction
.AUTHOR
, value
));
590 * Create the "rename" menu item.
593 * the library can be queried
597 private JMenuItem
createMenuItemRename(
598 @SuppressWarnings("unused") boolean libOk
) {
599 JMenuItem changeTo
= new JMenuItem("Rename...");
600 changeTo
.setMnemonic(KeyEvent
.VK_R
);
601 changeTo
.addActionListener(createMoveAction(ChangeAction
.TITLE
, null));
605 private ActionListener
createMoveAction(final ChangeAction what
,
607 return new ActionListener() {
609 public void actionPerformed(ActionEvent e
) {
610 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
611 if (selectedBook
!= null) {
612 String changeTo
= type
;
614 MetaData meta
= selectedBook
.getInfo().getMeta();
616 if (what
== ChangeAction
.SOURCE
) {
617 init
= meta
.getSource();
618 } else if (what
== ChangeAction
.TITLE
) {
619 init
= meta
.getTitle();
620 } else if (what
== ChangeAction
.AUTHOR
) {
621 init
= meta
.getAuthor();
624 Object rep
= JOptionPane
.showInputDialog(
625 GuiReaderFrame
.this, "Move to:",
626 "Moving story", JOptionPane
.QUESTION_MESSAGE
,
633 changeTo
= rep
.toString();
636 final String fChangeTo
= changeTo
;
637 mainPanel
.outOfUi(null, new Runnable() {
640 String luid
= selectedBook
.getInfo().getMeta()
642 if (what
== ChangeAction
.SOURCE
) {
643 reader
.changeSource(luid
, fChangeTo
);
644 } else if (what
== ChangeAction
.TITLE
) {
645 reader
.changeTitle(luid
, fChangeTo
);
646 } else if (what
== ChangeAction
.AUTHOR
) {
647 reader
.changeAuthor(luid
, fChangeTo
);
650 mainPanel
.unsetSelectedBook();
652 SwingUtilities
.invokeLater(new Runnable() {
666 * Create the re-download (then delete original) menu item.
670 private JMenuItem
createMenuItemRedownload() {
671 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
672 refresh
.addActionListener(new ActionListener() {
674 public void actionPerformed(ActionEvent e
) {
675 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
676 if (selectedBook
!= null) {
677 final MetaData meta
= selectedBook
.getInfo().getMeta();
678 mainPanel
.imprt(meta
.getUrl(), new StoryRunnable() {
680 public void run(Story story
) {
681 reader
.delete(meta
.getLuid());
682 mainPanel
.unsetSelectedBook();
683 MetaData newMeta
= story
.getMeta();
684 if (!newMeta
.getSource().equals(meta
.getSource())) {
685 reader
.changeSource(newMeta
.getLuid(),
689 }, "Removing old copy");
698 * Create the delete menu item.
702 private JMenuItem
createMenuItemDelete() {
703 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
704 delete
.addActionListener(new ActionListener() {
706 public void actionPerformed(ActionEvent e
) {
707 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
708 if (selectedBook
!= null) {
709 mainPanel
.outOfUi(null, new Runnable() {
712 reader
.delete(selectedBook
.getInfo().getMeta()
714 mainPanel
.unsetSelectedBook();
725 * Create the properties menu item.
729 private JMenuItem
createMenuItemProperties() {
730 JMenuItem delete
= new JMenuItem("Properties", KeyEvent
.VK_P
);
731 delete
.addActionListener(new ActionListener() {
733 public void actionPerformed(ActionEvent e
) {
734 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
735 if (selectedBook
!= null) {
736 mainPanel
.outOfUi(null, new Runnable() {
739 new GuiReaderPropertiesFrame(reader
, selectedBook
740 .getInfo()).setVisible(true);
751 * Create the open menu item for a book or a source/type (no LUID).
755 public JMenuItem
createMenuItemOpenBook() {
756 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
757 open
.addActionListener(new ActionListener() {
759 public void actionPerformed(ActionEvent e
) {
760 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
761 if (selectedBook
!= null) {
762 if (selectedBook
.getInfo().getMeta().getLuid() == null) {
763 mainPanel
.removeBookPanes();
764 mainPanel
.addBookPane(selectedBook
.getInfo().getMeta()
766 mainPanel
.refreshBooks();
768 mainPanel
.openBook(selectedBook
);
778 * Create the SetCover menu item for a book to change the linked source
783 private JMenuItem
createMenuItemSetCoverForSource() {
784 JMenuItem open
= new JMenuItem("Set as cover for source", KeyEvent
.VK_C
);
785 open
.addActionListener(new ActionListener() {
787 public void actionPerformed(ActionEvent e
) {
788 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
789 if (selectedBook
!= null) {
790 BasicLibrary lib
= reader
.getLibrary();
791 String luid
= selectedBook
.getInfo().getMeta().getLuid();
792 String source
= selectedBook
.getInfo().getMeta()
795 lib
.setSourceCover(source
, luid
);
797 GuiReaderBookInfo sourceInfo
= GuiReaderBookInfo
798 .fromSource(lib
, source
);
799 GuiReaderCoverImager
.clearIcon(sourceInfo
);
808 * Create the SetCover menu item for a book to change the linked source
813 private JMenuItem
createMenuItemSetCoverForAuthor() {
814 JMenuItem open
= new JMenuItem("Set as cover for author", KeyEvent
.VK_A
);
815 open
.addActionListener(new ActionListener() {
817 public void actionPerformed(ActionEvent e
) {
818 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
819 if (selectedBook
!= null) {
820 BasicLibrary lib
= reader
.getLibrary();
821 String luid
= selectedBook
.getInfo().getMeta().getLuid();
822 String author
= selectedBook
.getInfo().getMeta()
825 lib
.setAuthorCover(author
, luid
);
827 GuiReaderBookInfo authorInfo
= GuiReaderBookInfo
828 .fromAuthor(lib
, author
);
829 GuiReaderCoverImager
.clearIcon(authorInfo
);
838 * Display an error message and log the linked {@link Exception}.
843 * the title of the error message
845 * the exception to log if any
847 public void error(final String message
, final String title
, Exception e
) {
848 Instance
.getTraceHandler().error(title
+ ": " + message
);
850 Instance
.getTraceHandler().error(e
);
853 SwingUtilities
.invokeLater(new Runnable() {
856 JOptionPane
.showMessageDialog(GuiReaderFrame
.this, message
,
857 title
, JOptionPane
.ERROR_MESSAGE
);
863 public GuiReader
getReader() {