1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.BorderLayout
;
6 import java
.awt
.Toolkit
;
7 import java
.awt
.datatransfer
.DataFlavor
;
8 import java
.awt
.event
.ActionEvent
;
9 import java
.awt
.event
.ActionListener
;
10 import java
.awt
.event
.KeyEvent
;
11 import java
.awt
.event
.MouseEvent
;
12 import java
.awt
.event
.WindowEvent
;
14 import java
.io
.IOException
;
16 import java
.util
.ArrayList
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
20 import java
.util
.Map
.Entry
;
22 import javax
.swing
.BoxLayout
;
23 import javax
.swing
.JFileChooser
;
24 import javax
.swing
.JFrame
;
25 import javax
.swing
.JLabel
;
26 import javax
.swing
.JMenu
;
27 import javax
.swing
.JMenuBar
;
28 import javax
.swing
.JMenuItem
;
29 import javax
.swing
.JOptionPane
;
30 import javax
.swing
.JPanel
;
31 import javax
.swing
.JPopupMenu
;
32 import javax
.swing
.JScrollPane
;
33 import javax
.swing
.SwingConstants
;
34 import javax
.swing
.SwingUtilities
;
35 import javax
.swing
.filechooser
.FileFilter
;
36 import javax
.swing
.filechooser
.FileNameExtensionFilter
;
38 import be
.nikiroo
.fanfix
.Instance
;
39 import be
.nikiroo
.fanfix
.bundles
.Config
;
40 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
41 import be
.nikiroo
.fanfix
.data
.MetaData
;
42 import be
.nikiroo
.fanfix
.data
.Story
;
43 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
44 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
45 import be
.nikiroo
.fanfix
.reader
.GuiReaderBook
.BookActionListener
;
46 import be
.nikiroo
.utils
.Progress
;
47 import be
.nikiroo
.utils
.Version
;
48 import be
.nikiroo
.utils
.ui
.ConfigEditor
;
49 import be
.nikiroo
.utils
.ui
.ProgressBar
;
52 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
53 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
54 * way to copy them to the {@link GuiReader} cache (
55 * {@link BasicReader#getLibrary()}), read them, delete them...
59 class GuiReaderFrame
extends JFrame
{
60 private static final long serialVersionUID
= 1L;
61 private GuiReader reader
;
62 private Map
<GuiReaderGroup
, String
> booksByType
;
63 private Map
<GuiReaderGroup
, String
> booksByAuthor
;
66 private ProgressBar pgBar
;
68 private GuiReaderBook selectedBook
;
69 private boolean words
; // words or authors (secondary info on books)
72 * A {@link Runnable} with a {@link Story} parameter.
76 private interface StoryRunnable
{
83 public void run(Story story
);
87 * Create a new {@link GuiReaderFrame}.
90 * the associated {@link GuiReader} to forward some commands and
91 * access its {@link LocalLibrary}
93 * the type of {@link Story} to load, or NULL for all types
95 public GuiReaderFrame(GuiReader reader
, String type
) {
96 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
100 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
102 setLayout(new BorderLayout());
105 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
107 color
= Instance
.getUiConfig().getColor(UiConfig
.BACKGROUND_COLOR
);
109 setBackground(color
);
110 pane
.setBackground(color
);
113 JScrollPane scroll
= new JScrollPane(pane
);
114 scroll
.getVerticalScrollBar().setUnitIncrement(16);
115 add(scroll
, BorderLayout
.CENTER
);
117 String message
= reader
.getLibrary().getLibraryName();
118 if (!message
.isEmpty()) {
119 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
120 add(name
, BorderLayout
.NORTH
);
123 pgBar
= new ProgressBar();
124 add(pgBar
, BorderLayout
.SOUTH
);
126 pgBar
.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e
) {
130 pgBar
.setProgress(null);
136 pgBar
.addUpdateListener(new ActionListener() {
138 public void actionPerformed(ActionEvent e
) {
145 booksByType
= new HashMap
<GuiReaderGroup
, String
>();
146 booksByAuthor
= new HashMap
<GuiReaderGroup
, String
>();
148 pane
.setVisible(false);
149 final Progress pg
= new Progress();
150 final String typeF
= type
;
151 outOfUi(pg
, new Runnable() {
154 GuiReaderFrame
.this.reader
.getLibrary().refresh(false, pg
);
156 setJMenuBar(createMenu());
157 addBookPane(typeF
, true);
160 pane
.setVisible(true);
167 private void addSourcePanes() {
169 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, "Sources", color
);
171 List
<MetaData
> sources
= new ArrayList
<MetaData
>();
172 for (String source
: reader
.getLibrary().getSources()) {
173 MetaData mSource
= new MetaData();
174 mSource
.setLuid(null);
175 mSource
.setTitle(source
);
176 mSource
.setSource(source
);
177 sources
.add(mSource
);
180 bookPane
.refreshBooks(sources
, false);
188 bookPane
.setActionListener(new BookActionListener() {
190 public void select(GuiReaderBook book
) {
195 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
196 JPopupMenu popup
= new JPopupMenu();
197 popup
.add(createMenuItemOpenBook());
198 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
202 public void action(final GuiReaderBook book
) {
204 addBookPane(book
.getMeta().getSource(), true);
211 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
212 * selected type or author.
215 * the author or the type, or NULL to get all the
218 * TRUE for type, FALSE for author
220 private void addBookPane(String value
, boolean type
) {
223 if (Instance
.getUiConfig().getBoolean(UiConfig
.SOURCE_PAGE
,
227 for (String tt
: reader
.getLibrary().getSources()) {
229 addBookPane(tt
, type
);
234 for (String tt
: reader
.getLibrary().getAuthors()) {
236 addBookPane(tt
, type
);
244 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, value
, color
);
246 booksByType
.put(bookPane
, value
);
248 booksByAuthor
.put(bookPane
, value
);
257 bookPane
.setActionListener(new BookActionListener() {
259 public void select(GuiReaderBook book
) {
264 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
265 JPopupMenu popup
= new JPopupMenu();
266 popup
.add(createMenuItemOpenBook());
267 popup
.addSeparator();
268 popup
.add(createMenuItemExport());
269 popup
.add(createMenuItemMove());
270 popup
.add(createMenuItemSetCover());
271 popup
.add(createMenuItemClearCache());
272 popup
.add(createMenuItemRedownload());
273 popup
.addSeparator();
274 popup
.add(createMenuItemDelete());
275 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
279 public void action(final GuiReaderBook book
) {
285 private void removeBookPanes() {
287 booksByAuthor
.clear();
296 * Refresh the list of {@link GuiReaderBook}s from disk.
299 private void refreshBooks() {
300 for (GuiReaderGroup group
: booksByType
.keySet()) {
301 List
<MetaData
> stories
= reader
.getLibrary().getListBySource(
302 booksByType
.get(group
));
303 group
.refreshBooks(stories
, words
);
306 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
307 List
<MetaData
> stories
= reader
.getLibrary().getListByAuthor(
308 booksByAuthor
.get(group
));
309 group
.refreshBooks(stories
, words
);
317 * Create the main menu bar.
321 private JMenuBar
createMenu() {
322 bar
= new JMenuBar();
324 JMenu file
= new JMenu("File");
325 file
.setMnemonic(KeyEvent
.VK_F
);
327 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
328 imprt
.addActionListener(new ActionListener() {
330 public void actionPerformed(ActionEvent e
) {
334 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
335 imprtF
.addActionListener(new ActionListener() {
337 public void actionPerformed(ActionEvent e
) {
341 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
342 exit
.addActionListener(new ActionListener() {
344 public void actionPerformed(ActionEvent e
) {
345 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
346 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
350 file
.add(createMenuItemOpenBook());
351 file
.add(createMenuItemExport());
352 file
.add(createMenuItemMove());
361 JMenu edit
= new JMenu("Edit");
362 edit
.setMnemonic(KeyEvent
.VK_E
);
364 edit
.add(createMenuItemClearCache());
365 edit
.add(createMenuItemRedownload());
367 edit
.add(createMenuItemDelete());
371 JMenu view
= new JMenu("View");
372 view
.setMnemonic(KeyEvent
.VK_V
);
373 JMenuItem vauthors
= new JMenuItem("Author");
374 vauthors
.setMnemonic(KeyEvent
.VK_A
);
375 vauthors
.addActionListener(new ActionListener() {
377 public void actionPerformed(ActionEvent e
) {
383 JMenuItem vwords
= new JMenuItem("Word count");
384 vwords
.setMnemonic(KeyEvent
.VK_W
);
385 vwords
.addActionListener(new ActionListener() {
387 public void actionPerformed(ActionEvent e
) {
395 JMenu sources
= new JMenu("Sources");
396 sources
.setMnemonic(KeyEvent
.VK_S
);
398 List
<String
> tt
= reader
.getLibrary().getSources();
400 for (final String type
: tt
) {
401 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
402 item
.addActionListener(new ActionListener() {
404 public void actionPerformed(ActionEvent e
) {
406 addBookPane(type
, true);
413 sources
.addSeparator();
419 JMenu authors
= new JMenu("Authors");
420 authors
.setMnemonic(KeyEvent
.VK_A
);
422 List
<String
> aa
= reader
.getLibrary().getAuthors();
424 for (final String author
: aa
) {
425 JMenuItem item
= new JMenuItem(author
== null ?
"All"
426 : author
.isEmpty() ?
"[unknown]" : author
);
427 item
.addActionListener(new ActionListener() {
429 public void actionPerformed(ActionEvent e
) {
431 addBookPane(author
, false);
437 if (author
== null || author
.isEmpty()) {
438 authors
.addSeparator();
444 JMenu options
= new JMenu("Options");
445 options
.setMnemonic(KeyEvent
.VK_O
);
446 options
.add(createMenuItemConfig());
447 options
.add(createMenuItemUiConfig());
454 * Create the Fanfix Configuration menu item.
458 private JMenuItem
createMenuItemConfig() {
459 final String title
= "Fanfix Configuration";
460 JMenuItem item
= new JMenuItem(title
);
461 item
.setMnemonic(KeyEvent
.VK_F
);
463 item
.addActionListener(new ActionListener() {
465 public void actionPerformed(ActionEvent e
) {
466 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
467 Config
.class, Instance
.getConfig(),
468 "This is where you configure the options of the program.");
469 JFrame frame
= new JFrame(title
);
471 frame
.setSize(800, 600);
472 frame
.setVisible(true);
480 * Create the UI Configuration menu item.
484 private JMenuItem
createMenuItemUiConfig() {
485 final String title
= "UI Configuration";
486 JMenuItem item
= new JMenuItem(title
);
487 item
.setMnemonic(KeyEvent
.VK_U
);
489 item
.addActionListener(new ActionListener() {
491 public void actionPerformed(ActionEvent e
) {
492 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
493 UiConfig
.class, Instance
.getUiConfig(),
494 "This is where you configure the graphical appearence of the program.");
495 JFrame frame
= new JFrame(title
);
497 frame
.setSize(800, 600);
498 frame
.setVisible(true);
506 * Create the export menu item.
510 private JMenuItem
createMenuItemExport() {
511 final JFileChooser fc
= new JFileChooser();
512 fc
.setAcceptAllFileFilterUsed(false);
514 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
515 for (OutputType type
: OutputType
.values()) {
516 String ext
= type
.getDefaultExtension(false);
517 String desc
= type
.getDesc(false);
519 if (ext
== null || ext
.isEmpty()) {
520 filters
.put(createAllFilter(desc
), type
);
522 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
526 // First the "ALL" filters, then, the extension filters
527 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
528 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
529 fc
.addChoosableFileFilter(entry
.getKey());
532 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
533 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
534 fc
.addChoosableFileFilter(entry
.getKey());
539 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
540 export
.addActionListener(new ActionListener() {
542 public void actionPerformed(ActionEvent e
) {
543 if (selectedBook
!= null) {
544 fc
.showDialog(GuiReaderFrame
.this, "Save");
545 if (fc
.getSelectedFile() != null) {
546 final OutputType type
= filters
.get(fc
.getFileFilter());
547 final String path
= fc
.getSelectedFile()
549 + type
.getDefaultExtension(false);
550 final Progress pg
= new Progress();
551 outOfUi(pg
, new Runnable() {
555 reader
.getLibrary().export(
556 selectedBook
.getMeta().getLuid(),
558 } catch (IOException e
) {
572 * Create a {@link FileFilter} that accepts all files and return the given
580 private FileFilter
createAllFilter(final String desc
) {
581 return new FileFilter() {
583 public String
getDescription() {
588 public boolean accept(File f
) {
595 * Create the refresh (delete cache) menu item.
599 private JMenuItem
createMenuItemClearCache() {
600 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
601 refresh
.addActionListener(new ActionListener() {
603 public void actionPerformed(ActionEvent e
) {
604 if (selectedBook
!= null) {
605 outOfUi(null, new Runnable() {
608 reader
.clearLocalReaderCache(selectedBook
.getMeta()
610 selectedBook
.setCached(false);
611 SwingUtilities
.invokeLater(new Runnable() {
614 selectedBook
.repaint();
627 * Create the delete menu item.
631 private JMenuItem
createMenuItemMove() {
632 JMenu moveTo
= new JMenu("Move to...");
633 moveTo
.setMnemonic(KeyEvent
.VK_M
);
635 List
<String
> types
= new ArrayList
<String
>();
637 types
.addAll(reader
.getLibrary().getSources());
639 for (String type
: types
) {
640 JMenuItem item
= new JMenuItem(type
== null ?
"New type..." : type
);
644 moveTo
.addSeparator();
647 final String ftype
= type
;
648 item
.addActionListener(new ActionListener() {
650 public void actionPerformed(ActionEvent e
) {
651 if (selectedBook
!= null) {
654 Object rep
= JOptionPane
.showInputDialog(
655 GuiReaderFrame
.this, "Move to:",
657 JOptionPane
.QUESTION_MESSAGE
, null, null,
658 selectedBook
.getMeta().getSource());
664 type
= rep
.toString();
667 final String ftype
= type
;
668 outOfUi(null, new Runnable() {
671 reader
.changeType(selectedBook
.getMeta()
676 SwingUtilities
.invokeLater(new Runnable() {
679 setJMenuBar(createMenu());
693 * Create the redownload (then delete original) menu item.
697 private JMenuItem
createMenuItemRedownload() {
698 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
699 refresh
.addActionListener(new ActionListener() {
701 public void actionPerformed(ActionEvent e
) {
702 if (selectedBook
!= null) {
703 final MetaData meta
= selectedBook
.getMeta();
704 imprt(meta
.getUrl(), new StoryRunnable() {
706 public void run(Story story
) {
707 reader
.delete(meta
.getLuid());
708 GuiReaderFrame
.this.selectedBook
= null;
709 MetaData newMeta
= story
.getMeta();
710 if (!newMeta
.getSource().equals(meta
.getSource())) {
711 reader
.changeType(newMeta
.getLuid(),
715 }, "Removing old copy");
724 * Create the delete menu item.
728 private JMenuItem
createMenuItemDelete() {
729 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
730 delete
.addActionListener(new ActionListener() {
732 public void actionPerformed(ActionEvent e
) {
733 if (selectedBook
!= null) {
734 outOfUi(null, new Runnable() {
737 reader
.delete(selectedBook
.getMeta().getLuid());
749 * Create the open menu item for a book or a source (no LUID).
753 private JMenuItem
createMenuItemOpenBook() {
754 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
755 open
.addActionListener(new ActionListener() {
757 public void actionPerformed(ActionEvent e
) {
758 if (selectedBook
!= null) {
759 if (selectedBook
.getMeta().getLuid() == null) {
761 addBookPane(selectedBook
.getMeta().getSource(), true);
764 openBook(selectedBook
);
774 * Create the SetCover menu item for a book to change the linked source
779 private JMenuItem
createMenuItemSetCover() {
780 JMenuItem open
= new JMenuItem("Set as cover for source", KeyEvent
.VK_C
);
781 open
.addActionListener(new ActionListener() {
783 public void actionPerformed(ActionEvent e
) {
784 if (selectedBook
!= null) {
785 reader
.getLibrary().setSourceCover(
786 selectedBook
.getMeta().getSource(),
787 selectedBook
.getMeta().getLuid());
796 * Open a {@link GuiReaderBook} item.
799 * the {@link GuiReaderBook} to open
801 private void openBook(final GuiReaderBook book
) {
802 final Progress pg
= new Progress();
803 outOfUi(pg
, new Runnable() {
807 reader
.read(book
.getMeta().getLuid(), pg
);
808 SwingUtilities
.invokeLater(new Runnable() {
811 book
.setCached(true);
814 } catch (IOException e
) {
815 // TODO: error message?
823 * Process the given action out of the Swing UI thread and link the given
824 * {@link ProgressBar} to the action.
826 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
827 * to done when the action is done.
830 * the {@link ProgressBar} or NULL
834 private void outOfUi(Progress progress
, final Runnable run
) {
835 final Progress pg
= new Progress();
836 final Progress reload
= new Progress("Reload books");
837 if (progress
== null) {
838 progress
= new Progress();
841 pg
.addProgress(progress
, 90);
842 pg
.addProgress(reload
, 10);
845 pgBar
.setProgress(pg
);
849 new Thread(new Runnable() {
856 // will trigger pgBar ActionListener:
860 }, "outOfUi thread").start();
864 * Import a {@link Story} into the main {@link LocalLibrary}.
866 * Should be called inside the UI thread.
869 * TRUE for an {@link URL}, false for a {@link File}
871 private void imprt(boolean askUrl
) {
872 JFileChooser fc
= new JFileChooser();
876 String clipboard
= "";
878 clipboard
= ("" + Toolkit
.getDefaultToolkit()
879 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
881 } catch (Exception e
) {
882 // No data will be handled
885 if (clipboard
== null || !clipboard
.startsWith("http")) {
889 url
= JOptionPane
.showInputDialog(GuiReaderFrame
.this,
890 "url of the story to import?", "Importing from URL",
891 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
892 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
893 url
= fc
.getSelectedFile().getAbsolutePath();
898 if (url
!= null && !url
.toString().isEmpty()) {
899 imprt(url
.toString(), null, null);
904 * Actually import the {@link Story} into the main {@link LocalLibrary}.
906 * Should be called inside the UI thread.
909 * the {@link Story} to import by {@link URL}
911 * Action to execute on success
913 private void imprt(final String url
, final StoryRunnable onSuccess
,
914 String onSuccessPgName
) {
915 final Progress pg
= new Progress();
916 final Progress pgImprt
= new Progress();
917 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
918 pg
.addProgress(pgImprt
, 95);
919 pg
.addProgress(pgOnSuccess
, 5);
921 outOfUi(pg
, new Runnable() {
927 story
= reader
.getLibrary().imprt(BasicReader
.getUrl(url
),
929 } catch (IOException e
) {
933 final Exception e
= ex
;
935 final boolean ok
= (e
== null);
937 pgOnSuccess
.setProgress(0);
940 SwingUtilities
.invokeLater(new Runnable() {
943 JOptionPane
.showMessageDialog(GuiReaderFrame
.this,
944 "Cannot import: " + url
, e
.getMessage(),
945 JOptionPane
.ERROR_MESSAGE
);
949 if (onSuccess
!= null) {
950 onSuccess
.run(story
);
959 * Enables or disables this component, depending on the value of the
960 * parameter <code>b</code>. An enabled component can respond to user input
961 * and generate events. Components are enabled initially by default.
963 * Disabling this component will also affect its children.
966 * If <code>true</code>, this component is enabled; otherwise
967 * this component is disabled
970 public void setEnabled(boolean b
) {
975 for (GuiReaderGroup group
: booksByType
.keySet()) {
978 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {