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
.LocalLibrary
;
33 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
34 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
35 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderMainPanel
.FrameHelper
;
36 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderMainPanel
.StoryRunnable
;
37 import be
.nikiroo
.utils
.Progress
;
38 import be
.nikiroo
.utils
.Version
;
39 import be
.nikiroo
.utils
.ui
.ConfigEditor
;
42 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
43 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
44 * way to copy them to the {@link GuiReader} cache (
45 * {@link BasicReader#getLibrary()}), read them, delete them...
49 class GuiReaderFrame
extends JFrame
implements FrameHelper
{
50 private static final long serialVersionUID
= 1L;
51 private GuiReader reader
;
52 private GuiReaderMainPanel mainPanel
;
54 private enum MoveAction
{
59 * Create a new {@link GuiReaderFrame}.
62 * the associated {@link GuiReader} to forward some commands and
63 * access its {@link LocalLibrary}
65 * the type of {@link Story} to load, or NULL for all types
67 public GuiReaderFrame(GuiReader reader
, String type
) {
68 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
72 mainPanel
= new GuiReaderMainPanel(this, type
);
75 setLayout(new BorderLayout());
80 public JPopupMenu
createBookPopup() {
81 JPopupMenu popup
= new JPopupMenu();
82 popup
.add(createMenuItemOpenBook());
84 popup
.add(createMenuItemExport());
85 popup
.add(createMenuItemMoveTo(true));
86 popup
.add(createMenuItemSetCover());
87 popup
.add(createMenuItemClearCache());
88 popup
.add(createMenuItemRedownload());
90 popup
.add(createMenuItemRename(true));
91 popup
.add(createMenuItemSetAuthor(true));
93 popup
.add(createMenuItemDelete());
95 popup
.add(createMenuItemProperties());
100 public JPopupMenu
createSourcePopup() {
101 JPopupMenu popup
= new JPopupMenu();
102 popup
.add(createMenuItemOpenBook());
107 public void createMenu(boolean libOk
) {
108 JMenuBar bar
= new JMenuBar();
110 JMenu file
= new JMenu("File");
111 file
.setMnemonic(KeyEvent
.VK_F
);
113 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
114 imprt
.addActionListener(new ActionListener() {
116 public void actionPerformed(ActionEvent e
) {
117 mainPanel
.imprt(true);
120 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
121 imprtF
.addActionListener(new ActionListener() {
123 public void actionPerformed(ActionEvent e
) {
124 mainPanel
.imprt(false);
127 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
128 exit
.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent e
) {
131 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
132 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
136 file
.add(createMenuItemOpenBook());
137 file
.add(createMenuItemExport());
138 file
.add(createMenuItemMoveTo(libOk
));
143 file
.add(createMenuItemRename(libOk
));
144 file
.add(createMenuItemSetAuthor(libOk
));
150 JMenu edit
= new JMenu("Edit");
151 edit
.setMnemonic(KeyEvent
.VK_E
);
153 edit
.add(createMenuItemClearCache());
154 edit
.add(createMenuItemRedownload());
156 edit
.add(createMenuItemDelete());
160 JMenu view
= new JMenu("View");
161 view
.setMnemonic(KeyEvent
.VK_V
);
162 JMenuItem vauthors
= new JMenuItem("Author");
163 vauthors
.setMnemonic(KeyEvent
.VK_A
);
164 vauthors
.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e
) {
167 mainPanel
.setWords(false);
168 mainPanel
.refreshBooks();
172 JMenuItem vwords
= new JMenuItem("Word count");
173 vwords
.setMnemonic(KeyEvent
.VK_W
);
174 vwords
.addActionListener(new ActionListener() {
176 public void actionPerformed(ActionEvent e
) {
177 mainPanel
.setWords(true);
178 mainPanel
.refreshBooks();
184 Map
<String
, List
<String
>> groupedSources
= new HashMap
<String
, List
<String
>>();
186 groupedSources
= reader
.getLibrary().getSourcesGrouped();
188 JMenu sources
= new JMenu("Sources");
189 sources
.setMnemonic(KeyEvent
.VK_S
);
190 populateMenuSA(sources
, groupedSources
, true);
193 Map
<String
, List
<String
>> goupedAuthors
= new HashMap
<String
, List
<String
>>();
195 goupedAuthors
= reader
.getLibrary().getAuthorsGrouped();
197 JMenu authors
= new JMenu("Authors");
198 authors
.setMnemonic(KeyEvent
.VK_A
);
199 populateMenuSA(authors
, goupedAuthors
, false);
202 JMenu options
= new JMenu("Options");
203 options
.setMnemonic(KeyEvent
.VK_O
);
204 options
.add(createMenuItemConfig());
205 options
.add(createMenuItemUiConfig());
212 private void populateMenuSA(JMenu menu
,
213 Map
<String
, List
<String
>> groupedValues
, boolean type
) {
215 // "All" and "Listing" special items
216 JMenuItem item
= new JMenuItem("All");
217 item
.addActionListener(getActionOpenList(type
, false));
219 item
= new JMenuItem("Listing");
220 item
.addActionListener(getActionOpenList(type
, true));
224 for (final String value
: groupedValues
.keySet()) {
225 List
<String
> list
= groupedValues
.get(value
);
226 if (type
&& list
.size() == 1 && list
.get(0).isEmpty()) {
227 // leaf item source/type
228 item
= new JMenuItem(value
.isEmpty() ?
"[unknown]" : value
);
229 item
.addActionListener(getActionOpen(value
, type
));
233 if (!type
&& groupedValues
.size() == 1) {
234 // only one group of authors
237 dir
= new JMenu(value
.isEmpty() ?
"[unknown]" : value
);
240 for (String sub
: list
) {
241 // " " instead of "" for the visual height
242 String itemName
= sub
;
243 if (itemName
.isEmpty()) {
244 itemName
= type ?
" " : "[unknown]";
247 String actualValue
= value
;
249 if (!sub
.isEmpty()) {
250 actualValue
+= "/" + sub
;
256 item
= new JMenuItem(itemName
);
257 item
.addActionListener(getActionOpen(actualValue
, type
));
269 * Return an {@link ActionListener} that will set the given source (type) as
270 * the selected/displayed one.
273 * the type (source) to select, cannot be NULL
275 * @return the {@link ActionListener}
277 private ActionListener
getActionOpen(final String source
, final boolean type
) {
278 return new ActionListener() {
280 public void actionPerformed(ActionEvent e
) {
281 mainPanel
.removeBookPanes();
282 mainPanel
.addBookPane(source
, type
);
283 mainPanel
.refreshBooks();
288 private ActionListener
getActionOpenList(final boolean type
,
289 final boolean listMode
) {
290 return new ActionListener() {
292 public void actionPerformed(ActionEvent e
) {
293 mainPanel
.removeBookPanes();
294 mainPanel
.addBookPane(type
, listMode
);
295 mainPanel
.refreshBooks();
301 * Create the Fanfix Configuration menu item.
305 private JMenuItem
createMenuItemConfig() {
306 final String title
= "Fanfix Configuration";
307 JMenuItem item
= new JMenuItem(title
);
308 item
.setMnemonic(KeyEvent
.VK_F
);
310 item
.addActionListener(new ActionListener() {
312 public void actionPerformed(ActionEvent e
) {
313 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
314 Config
.class, Instance
.getConfig(),
315 "This is where you configure the options of the program.");
316 JFrame frame
= new JFrame(title
);
318 frame
.setSize(800, 600);
319 frame
.setVisible(true);
327 * Create the UI Configuration menu item.
331 private JMenuItem
createMenuItemUiConfig() {
332 final String title
= "UI Configuration";
333 JMenuItem item
= new JMenuItem(title
);
334 item
.setMnemonic(KeyEvent
.VK_U
);
336 item
.addActionListener(new ActionListener() {
338 public void actionPerformed(ActionEvent e
) {
339 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
340 UiConfig
.class, Instance
.getUiConfig(),
341 "This is where you configure the graphical appearence of the program.");
342 JFrame frame
= new JFrame(title
);
344 frame
.setSize(800, 600);
345 frame
.setVisible(true);
353 * Create the export menu item.
357 private JMenuItem
createMenuItemExport() {
358 final JFileChooser fc
= new JFileChooser();
359 fc
.setAcceptAllFileFilterUsed(false);
361 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
362 for (OutputType type
: OutputType
.values()) {
363 String ext
= type
.getDefaultExtension(false);
364 String desc
= type
.getDesc(false);
366 if (ext
== null || ext
.isEmpty()) {
367 filters
.put(createAllFilter(desc
), type
);
369 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
373 // First the "ALL" filters, then, the extension filters
374 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
375 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
376 fc
.addChoosableFileFilter(entry
.getKey());
379 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
380 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
381 fc
.addChoosableFileFilter(entry
.getKey());
386 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
387 export
.addActionListener(new ActionListener() {
389 public void actionPerformed(ActionEvent e
) {
390 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
391 if (selectedBook
!= null) {
392 fc
.showDialog(GuiReaderFrame
.this, "Save");
393 if (fc
.getSelectedFile() != null) {
394 final OutputType type
= filters
.get(fc
.getFileFilter());
395 final String path
= fc
.getSelectedFile()
397 + type
.getDefaultExtension(false);
398 final Progress pg
= new Progress();
399 mainPanel
.outOfUi(pg
, new Runnable() {
403 reader
.getLibrary().export(
404 selectedBook
.getMeta().getLuid(),
406 } catch (IOException e
) {
407 Instance
.getTraceHandler().error(e
);
420 * Create a {@link FileFilter} that accepts all files and return the given
428 private FileFilter
createAllFilter(final String desc
) {
429 return new FileFilter() {
431 public String
getDescription() {
436 public boolean accept(File f
) {
443 * Create the refresh (delete cache) menu item.
447 private JMenuItem
createMenuItemClearCache() {
448 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
449 refresh
.addActionListener(new ActionListener() {
451 public void actionPerformed(ActionEvent e
) {
452 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
453 if (selectedBook
!= null) {
454 mainPanel
.outOfUi(null, new Runnable() {
457 reader
.clearLocalReaderCache(selectedBook
.getMeta()
459 selectedBook
.setCached(false);
460 GuiReaderCoverImager
.clearIcon(selectedBook
462 SwingUtilities
.invokeLater(new Runnable() {
465 selectedBook
.repaint();
478 * Create the "move to" menu item.
481 * the library can be queried
485 private JMenuItem
createMenuItemMoveTo(boolean libOk
) {
486 JMenu changeTo
= new JMenu("Move to");
487 changeTo
.setMnemonic(KeyEvent
.VK_M
);
489 Map
<String
, List
<String
>> groupedSources
= new HashMap
<String
, List
<String
>>();
491 groupedSources
= reader
.getLibrary().getSourcesGrouped();
494 JMenuItem item
= new JMenuItem("New type...");
495 item
.addActionListener(createMoveAction(MoveAction
.SOURCE
, null));
497 changeTo
.addSeparator();
499 for (final String type
: groupedSources
.keySet()) {
500 List
<String
> list
= groupedSources
.get(type
);
501 if (list
.size() == 1 && list
.get(0).isEmpty()) {
502 item
= new JMenuItem(type
);
503 item
.addActionListener(createMoveAction(MoveAction
.SOURCE
, type
));
506 JMenu dir
= new JMenu(type
);
507 for (String sub
: list
) {
508 // " " instead of "" for the visual height
509 String itemName
= sub
.isEmpty() ?
" " : sub
;
510 String actualType
= type
;
511 if (!sub
.isEmpty()) {
512 actualType
+= "/" + sub
;
515 item
= new JMenuItem(itemName
);
516 item
.addActionListener(createMoveAction(MoveAction
.SOURCE
,
528 * Create the "set author" menu item.
531 * the library can be queried
535 private JMenuItem
createMenuItemSetAuthor(boolean libOk
) {
536 JMenu changeTo
= new JMenu("Set author");
537 changeTo
.setMnemonic(KeyEvent
.VK_A
);
540 JMenuItem newItem
= new JMenuItem("New author...");
541 changeTo
.add(newItem
);
542 changeTo
.addSeparator();
543 newItem
.addActionListener(createMoveAction(MoveAction
.AUTHOR
, null));
547 Map
<String
, List
<String
>> groupedAuthors
= reader
.getLibrary()
548 .getAuthorsGrouped();
550 if (groupedAuthors
.size() > 1) {
551 for (String key
: groupedAuthors
.keySet()) {
552 JMenu group
= new JMenu(key
);
553 for (String value
: groupedAuthors
.get(key
)) {
554 JMenuItem item
= new JMenuItem(
555 value
.isEmpty() ?
"[unknown]" : value
);
556 item
.addActionListener(createMoveAction(
557 MoveAction
.AUTHOR
, value
));
562 } else if (groupedAuthors
.size() == 1) {
563 for (String value
: groupedAuthors
.values().iterator().next()) {
564 JMenuItem item
= new JMenuItem(
565 value
.isEmpty() ?
"[unknown]" : value
);
566 item
.addActionListener(createMoveAction(MoveAction
.AUTHOR
,
577 * Create the "rename" menu item.
580 * the library can be queried
584 private JMenuItem
createMenuItemRename(
585 @SuppressWarnings("unused") boolean libOk
) {
586 JMenuItem changeTo
= new JMenuItem("Rename...");
587 changeTo
.setMnemonic(KeyEvent
.VK_R
);
588 changeTo
.addActionListener(createMoveAction(MoveAction
.TITLE
, null));
592 private ActionListener
createMoveAction(final MoveAction what
,
594 return new ActionListener() {
596 public void actionPerformed(ActionEvent e
) {
597 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
598 if (selectedBook
!= null) {
599 String changeTo
= type
;
602 if (what
== MoveAction
.SOURCE
) {
603 init
= selectedBook
.getMeta().getSource();
604 } else if (what
== MoveAction
.TITLE
) {
605 init
= selectedBook
.getMeta().getTitle();
606 } else if (what
== MoveAction
.AUTHOR
) {
607 init
= selectedBook
.getMeta().getAuthor();
610 Object rep
= JOptionPane
.showInputDialog(
611 GuiReaderFrame
.this, "Move to:",
612 "Moving story", JOptionPane
.QUESTION_MESSAGE
,
619 changeTo
= rep
.toString();
622 final String fChangeTo
= changeTo
;
623 mainPanel
.outOfUi(null, new Runnable() {
626 if (what
== MoveAction
.SOURCE
) {
627 reader
.changeSource(selectedBook
.getMeta()
628 .getLuid(), fChangeTo
);
629 } else if (what
== MoveAction
.TITLE
) {
630 reader
.changeTitle(selectedBook
.getMeta()
631 .getLuid(), fChangeTo
);
632 } else if (what
== MoveAction
.AUTHOR
) {
633 reader
.changeAuthor(selectedBook
.getMeta()
634 .getLuid(), fChangeTo
);
637 mainPanel
.unsetSelectedBook();
639 SwingUtilities
.invokeLater(new Runnable() {
653 * Create the redownload (then delete original) menu item.
657 private JMenuItem
createMenuItemRedownload() {
658 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
659 refresh
.addActionListener(new ActionListener() {
661 public void actionPerformed(ActionEvent e
) {
662 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
663 if (selectedBook
!= null) {
664 final MetaData meta
= selectedBook
.getMeta();
665 mainPanel
.imprt(meta
.getUrl(), new StoryRunnable() {
667 public void run(Story story
) {
668 reader
.delete(meta
.getLuid());
669 mainPanel
.unsetSelectedBook();
670 MetaData newMeta
= story
.getMeta();
671 if (!newMeta
.getSource().equals(meta
.getSource())) {
672 reader
.changeSource(newMeta
.getLuid(),
676 }, "Removing old copy");
685 * Create the delete menu item.
689 private JMenuItem
createMenuItemDelete() {
690 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
691 delete
.addActionListener(new ActionListener() {
693 public void actionPerformed(ActionEvent e
) {
694 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
695 if (selectedBook
!= null) {
696 mainPanel
.outOfUi(null, new Runnable() {
699 reader
.delete(selectedBook
.getMeta().getLuid());
700 mainPanel
.unsetSelectedBook();
711 * Create the properties menu item.
715 private JMenuItem
createMenuItemProperties() {
716 JMenuItem delete
= new JMenuItem("Properties", KeyEvent
.VK_P
);
717 delete
.addActionListener(new ActionListener() {
719 public void actionPerformed(ActionEvent e
) {
720 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
721 if (selectedBook
!= null) {
722 mainPanel
.outOfUi(null, new Runnable() {
725 new GuiReaderPropertiesFrame(reader
, selectedBook
726 .getMeta()).setVisible(true);
737 * Create the open menu item for a book or a source/type (no LUID).
741 public JMenuItem
createMenuItemOpenBook() {
742 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
743 open
.addActionListener(new ActionListener() {
745 public void actionPerformed(ActionEvent e
) {
746 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
747 if (selectedBook
!= null) {
748 if (selectedBook
.getMeta().getLuid() == null) {
749 mainPanel
.removeBookPanes();
750 mainPanel
.addBookPane(selectedBook
.getMeta()
752 mainPanel
.refreshBooks();
754 mainPanel
.openBook(selectedBook
);
764 * Create the SetCover menu item for a book to change the linked source
769 private JMenuItem
createMenuItemSetCover() {
770 JMenuItem open
= new JMenuItem("Set as cover for source", KeyEvent
.VK_C
);
771 open
.addActionListener(new ActionListener() {
773 public void actionPerformed(ActionEvent e
) {
774 final GuiReaderBook selectedBook
= mainPanel
.getSelectedBook();
775 if (selectedBook
!= null) {
776 reader
.getLibrary().setSourceCover(
777 selectedBook
.getMeta().getSource(),
778 selectedBook
.getMeta().getLuid());
779 MetaData source
= selectedBook
.getMeta().clone();
780 source
.setLuid(null);
781 GuiReaderCoverImager
.clearIcon(source
);
790 * Display an error message and log the linked {@link Exception}.
795 * the title of the error message
797 * the exception to log if any
799 public void error(final String message
, final String title
, Exception e
) {
800 Instance
.getTraceHandler().error(title
+ ": " + message
);
802 Instance
.getTraceHandler().error(e
);
805 SwingUtilities
.invokeLater(new Runnable() {
808 JOptionPane
.showMessageDialog(GuiReaderFrame
.this, message
,
809 title
, JOptionPane
.ERROR_MESSAGE
);
815 public GuiReader
getReader() {