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
.JMenu
;
26 import javax
.swing
.JMenuBar
;
27 import javax
.swing
.JMenuItem
;
28 import javax
.swing
.JOptionPane
;
29 import javax
.swing
.JPanel
;
30 import javax
.swing
.JPopupMenu
;
31 import javax
.swing
.JScrollPane
;
32 import javax
.swing
.SwingUtilities
;
33 import javax
.swing
.filechooser
.FileFilter
;
34 import javax
.swing
.filechooser
.FileNameExtensionFilter
;
36 import be
.nikiroo
.fanfix
.Instance
;
37 import be
.nikiroo
.fanfix
.bundles
.Config
;
38 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
39 import be
.nikiroo
.fanfix
.data
.MetaData
;
40 import be
.nikiroo
.fanfix
.data
.Story
;
41 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
42 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
43 import be
.nikiroo
.fanfix
.reader
.GuiReaderBook
.BookActionListener
;
44 import be
.nikiroo
.utils
.Progress
;
45 import be
.nikiroo
.utils
.Version
;
46 import be
.nikiroo
.utils
.ui
.ConfigEditor
;
47 import be
.nikiroo
.utils
.ui
.ProgressBar
;
50 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
51 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
52 * way to copy them to the {@link GuiReader} cache (
53 * {@link BasicReader#getLibrary()}), read them, delete them...
57 class GuiReaderFrame
extends JFrame
{
58 private static final long serialVersionUID
= 1L;
59 private GuiReader reader
;
60 private Map
<GuiReaderGroup
, String
> booksByType
;
61 private Map
<GuiReaderGroup
, String
> booksByAuthor
;
64 private ProgressBar pgBar
;
66 private GuiReaderBook selectedBook
;
67 private boolean words
; // words or authors (secondary info on books)
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 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
85 setLayout(new BorderLayout());
88 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
90 color
= Instance
.getUiConfig().getColor(UiConfig
.BACKGROUND_COLOR
);
93 pane
.setBackground(color
);
96 JScrollPane scroll
= new JScrollPane(pane
);
97 scroll
.getVerticalScrollBar().setUnitIncrement(16);
98 add(scroll
, BorderLayout
.CENTER
);
100 pgBar
= new ProgressBar();
101 add(pgBar
, BorderLayout
.SOUTH
);
103 pgBar
.addActionListener(new ActionListener() {
105 public void actionPerformed(ActionEvent e
) {
107 pgBar
.setProgress(null);
113 pgBar
.addUpdateListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e
) {
122 booksByType
= new HashMap
<GuiReaderGroup
, String
>();
123 booksByAuthor
= new HashMap
<GuiReaderGroup
, String
>();
125 pane
.setVisible(false);
126 final Progress pg
= new Progress();
127 final String typeF
= type
;
128 outOfUi(pg
, new Runnable() {
131 GuiReaderFrame
.this.reader
.getLibrary().refresh(false, pg
);
133 setJMenuBar(createMenu());
134 addBookPane(typeF
, true);
137 pane
.setVisible(true);
144 private void addSourcePanes() {
146 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, "Sources", color
);
148 List
<MetaData
> sources
= new ArrayList
<MetaData
>();
149 for (String source
: reader
.getLibrary().getSources()) {
150 MetaData mSource
= new MetaData();
151 mSource
.setLuid(null);
152 mSource
.setTitle(source
);
153 mSource
.setSource(source
);
154 sources
.add(mSource
);
157 bookPane
.refreshBooks(sources
, false);
165 bookPane
.setActionListener(new BookActionListener() {
167 public void select(GuiReaderBook book
) {
172 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
173 JPopupMenu popup
= new JPopupMenu();
174 popup
.add(createMenuItemOpenBook());
175 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
179 public void action(final GuiReaderBook book
) {
181 addBookPane(book
.getMeta().getSource(), true);
188 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
189 * selected type or author.
192 * the author or the type, or NULL to get all the
195 * TRUE for type, FALSE for author
197 private void addBookPane(String value
, boolean type
) {
200 if (Instance
.getUiConfig().getBoolean(UiConfig
.SOURCE_PAGE
,
204 for (String tt
: reader
.getLibrary().getSources()) {
206 addBookPane(tt
, type
);
211 for (String tt
: reader
.getLibrary().getAuthors()) {
213 addBookPane(tt
, type
);
221 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, value
, color
);
223 booksByType
.put(bookPane
, value
);
225 booksByAuthor
.put(bookPane
, value
);
234 bookPane
.setActionListener(new BookActionListener() {
236 public void select(GuiReaderBook book
) {
241 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
242 JPopupMenu popup
= new JPopupMenu();
243 popup
.add(createMenuItemOpenBook());
244 popup
.addSeparator();
245 popup
.add(createMenuItemExport());
246 popup
.add(createMenuItemMove());
247 popup
.add(createMenuItemSetCover());
248 popup
.add(createMenuItemClearCache());
249 popup
.add(createMenuItemRedownload());
250 popup
.addSeparator();
251 popup
.add(createMenuItemDelete());
252 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
256 public void action(final GuiReaderBook book
) {
262 private void removeBookPanes() {
264 booksByAuthor
.clear();
273 * Refresh the list of {@link GuiReaderBook}s from disk.
276 private void refreshBooks() {
277 for (GuiReaderGroup group
: booksByType
.keySet()) {
278 List
<MetaData
> stories
= reader
.getLibrary().getListBySource(
279 booksByType
.get(group
));
280 group
.refreshBooks(stories
, words
);
283 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
284 List
<MetaData
> stories
= reader
.getLibrary().getListByAuthor(
285 booksByAuthor
.get(group
));
286 group
.refreshBooks(stories
, words
);
294 * Create the main menu bar.
298 private JMenuBar
createMenu() {
299 bar
= new JMenuBar();
301 JMenu file
= new JMenu("File");
302 file
.setMnemonic(KeyEvent
.VK_F
);
304 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
305 imprt
.addActionListener(new ActionListener() {
307 public void actionPerformed(ActionEvent e
) {
311 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
312 imprtF
.addActionListener(new ActionListener() {
314 public void actionPerformed(ActionEvent e
) {
318 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
319 exit
.addActionListener(new ActionListener() {
321 public void actionPerformed(ActionEvent e
) {
322 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
323 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
327 file
.add(createMenuItemOpenBook());
328 file
.add(createMenuItemExport());
329 file
.add(createMenuItemMove());
338 JMenu edit
= new JMenu("Edit");
339 edit
.setMnemonic(KeyEvent
.VK_E
);
341 edit
.add(createMenuItemClearCache());
342 edit
.add(createMenuItemRedownload());
344 edit
.add(createMenuItemDelete());
348 JMenu view
= new JMenu("View");
349 view
.setMnemonic(KeyEvent
.VK_V
);
350 JMenuItem vauthors
= new JMenuItem("Author");
351 vauthors
.setMnemonic(KeyEvent
.VK_A
);
352 vauthors
.addActionListener(new ActionListener() {
354 public void actionPerformed(ActionEvent e
) {
360 JMenuItem vwords
= new JMenuItem("Word count");
361 vwords
.setMnemonic(KeyEvent
.VK_W
);
362 vwords
.addActionListener(new ActionListener() {
364 public void actionPerformed(ActionEvent e
) {
372 JMenu sources
= new JMenu("Sources");
373 sources
.setMnemonic(KeyEvent
.VK_S
);
375 List
<String
> tt
= reader
.getLibrary().getSources();
377 for (final String type
: tt
) {
378 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
379 item
.addActionListener(new ActionListener() {
381 public void actionPerformed(ActionEvent e
) {
383 addBookPane(type
, true);
390 sources
.addSeparator();
396 JMenu authors
= new JMenu("Authors");
397 authors
.setMnemonic(KeyEvent
.VK_A
);
399 List
<String
> aa
= reader
.getLibrary().getAuthors();
401 for (final String author
: aa
) {
402 JMenuItem item
= new JMenuItem(author
== null ?
"All"
403 : author
.isEmpty() ?
"[unknown]" : author
);
404 item
.addActionListener(new ActionListener() {
406 public void actionPerformed(ActionEvent e
) {
408 addBookPane(author
, false);
414 if (author
== null || author
.isEmpty()) {
415 authors
.addSeparator();
421 JMenu options
= new JMenu("Options");
422 options
.setMnemonic(KeyEvent
.VK_O
);
423 options
.add(createMenuItemConfig());
424 options
.add(createMenuItemUiConfig());
431 * Create the Fanfix Configuration menu item.
435 private JMenuItem
createMenuItemConfig() {
436 final String title
= "Fanfix Configuration";
437 JMenuItem item
= new JMenuItem(title
);
438 item
.setMnemonic(KeyEvent
.VK_F
);
440 item
.addActionListener(new ActionListener() {
442 public void actionPerformed(ActionEvent e
) {
443 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
444 Config
.class, Instance
.getConfig(),
445 "This is where you configure the options of the program.");
446 JFrame frame
= new JFrame(title
);
448 frame
.setSize(800, 600);
449 frame
.setVisible(true);
457 * Create the UI Configuration menu item.
461 private JMenuItem
createMenuItemUiConfig() {
462 final String title
= "UI Configuration";
463 JMenuItem item
= new JMenuItem(title
);
464 item
.setMnemonic(KeyEvent
.VK_U
);
466 item
.addActionListener(new ActionListener() {
468 public void actionPerformed(ActionEvent e
) {
469 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
470 UiConfig
.class, Instance
.getUiConfig(),
471 "This is where you configure the graphical appearence of the program.");
472 JFrame frame
= new JFrame(title
);
474 frame
.setSize(800, 600);
475 frame
.setVisible(true);
483 * Create the export menu item.
487 private JMenuItem
createMenuItemExport() {
488 final JFileChooser fc
= new JFileChooser();
489 fc
.setAcceptAllFileFilterUsed(false);
491 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
492 for (OutputType type
: OutputType
.values()) {
493 String ext
= type
.getDefaultExtension(false);
494 String desc
= type
.getDesc(false);
496 if (ext
== null || ext
.isEmpty()) {
497 filters
.put(createAllFilter(desc
), type
);
499 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
503 // First the "ALL" filters, then, the extension filters
504 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
505 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
506 fc
.addChoosableFileFilter(entry
.getKey());
509 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
510 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
511 fc
.addChoosableFileFilter(entry
.getKey());
516 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
517 export
.addActionListener(new ActionListener() {
519 public void actionPerformed(ActionEvent e
) {
520 if (selectedBook
!= null) {
521 fc
.showDialog(GuiReaderFrame
.this, "Save");
522 if (fc
.getSelectedFile() != null) {
523 final OutputType type
= filters
.get(fc
.getFileFilter());
524 final String path
= fc
.getSelectedFile()
526 + type
.getDefaultExtension(false);
527 final Progress pg
= new Progress();
528 outOfUi(pg
, new Runnable() {
532 reader
.getLibrary().export(
533 selectedBook
.getMeta().getLuid(),
535 } catch (IOException e
) {
549 * Create a {@link FileFilter} that accepts all files and return the given
557 private FileFilter
createAllFilter(final String desc
) {
558 return new FileFilter() {
560 public String
getDescription() {
565 public boolean accept(File f
) {
572 * Create the refresh (delete cache) menu item.
576 private JMenuItem
createMenuItemClearCache() {
577 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
578 refresh
.addActionListener(new ActionListener() {
580 public void actionPerformed(ActionEvent e
) {
581 if (selectedBook
!= null) {
582 outOfUi(null, new Runnable() {
585 reader
.clearLocalReaderCache(selectedBook
.getMeta()
587 selectedBook
.setCached(false);
588 SwingUtilities
.invokeLater(new Runnable() {
591 selectedBook
.repaint();
604 * Create the delete menu item.
608 private JMenuItem
createMenuItemMove() {
609 JMenu moveTo
= new JMenu("Move to...");
610 moveTo
.setMnemonic(KeyEvent
.VK_M
);
612 List
<String
> types
= new ArrayList
<String
>();
614 types
.addAll(reader
.getLibrary().getSources());
616 for (String type
: types
) {
617 JMenuItem item
= new JMenuItem(type
== null ?
"New type..." : type
);
621 moveTo
.addSeparator();
624 final String ftype
= type
;
625 item
.addActionListener(new ActionListener() {
627 public void actionPerformed(ActionEvent e
) {
628 if (selectedBook
!= null) {
631 Object rep
= JOptionPane
.showInputDialog(
632 GuiReaderFrame
.this, "Move to:",
634 JOptionPane
.QUESTION_MESSAGE
, null, null,
635 selectedBook
.getMeta().getSource());
641 type
= rep
.toString();
644 final String ftype
= type
;
645 outOfUi(null, new Runnable() {
648 reader
.changeType(selectedBook
.getMeta()
653 SwingUtilities
.invokeLater(new Runnable() {
656 setJMenuBar(createMenu());
670 * Create the redownload (then delete original) menu item.
674 private JMenuItem
createMenuItemRedownload() {
675 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
676 refresh
.addActionListener(new ActionListener() {
678 public void actionPerformed(ActionEvent e
) {
679 if (selectedBook
!= null) {
680 final MetaData meta
= selectedBook
.getMeta();
681 imprt(meta
.getUrl(), new Runnable() {
684 reader
.delete(meta
.getLuid());
685 GuiReaderFrame
.this.selectedBook
= null;
687 }, "Removing old copy");
696 * Create the delete menu item.
700 private JMenuItem
createMenuItemDelete() {
701 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
702 delete
.addActionListener(new ActionListener() {
704 public void actionPerformed(ActionEvent e
) {
705 if (selectedBook
!= null) {
706 outOfUi(null, new Runnable() {
709 reader
.delete(selectedBook
.getMeta().getLuid());
721 * Create the open menu item for a book or a source (no LUID).
725 private JMenuItem
createMenuItemOpenBook() {
726 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
727 open
.addActionListener(new ActionListener() {
729 public void actionPerformed(ActionEvent e
) {
730 if (selectedBook
!= null) {
731 if (selectedBook
.getMeta().getLuid() == null) {
733 addBookPane(selectedBook
.getMeta().getSource(), true);
736 openBook(selectedBook
);
746 * Create the SetCover menu item for a book to change the linked source
751 private JMenuItem
createMenuItemSetCover() {
752 JMenuItem open
= new JMenuItem("Set as cover for source", KeyEvent
.VK_C
);
753 open
.addActionListener(new ActionListener() {
755 public void actionPerformed(ActionEvent e
) {
756 if (selectedBook
!= null) {
757 reader
.getLibrary().setSourceCover(
758 selectedBook
.getMeta().getSource(),
759 selectedBook
.getMeta().getLuid());
768 * Open a {@link GuiReaderBook} item.
771 * the {@link GuiReaderBook} to open
773 private void openBook(final GuiReaderBook book
) {
774 final Progress pg
= new Progress();
775 outOfUi(pg
, new Runnable() {
779 reader
.read(book
.getMeta().getLuid(), pg
);
780 SwingUtilities
.invokeLater(new Runnable() {
783 book
.setCached(true);
786 } catch (IOException e
) {
787 // TODO: error message?
795 * Process the given action out of the Swing UI thread and link the given
796 * {@link ProgressBar} to the action.
798 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
799 * to done when the action is done.
802 * the {@link ProgressBar} or NULL
806 private void outOfUi(Progress progress
, final Runnable run
) {
807 final Progress pg
= new Progress();
808 final Progress reload
= new Progress("Reload books");
809 if (progress
== null) {
810 progress
= new Progress();
813 pg
.addProgress(progress
, 90);
814 pg
.addProgress(reload
, 10);
817 pgBar
.setProgress(pg
);
821 new Thread(new Runnable() {
828 // will trigger pgBar ActionListener:
832 }, "outOfUi thread").start();
836 * Import a {@link Story} into the main {@link LocalLibrary}.
838 * Should be called inside the UI thread.
841 * TRUE for an {@link URL}, false for a {@link File}
843 private void imprt(boolean askUrl
) {
844 JFileChooser fc
= new JFileChooser();
848 String clipboard
= "";
850 clipboard
= ("" + Toolkit
.getDefaultToolkit()
851 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
853 } catch (Exception e
) {
854 // No data will be handled
857 if (clipboard
== null || !clipboard
.startsWith("http")) {
861 url
= JOptionPane
.showInputDialog(GuiReaderFrame
.this,
862 "url of the story to import?", "Importing from URL",
863 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
864 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
865 url
= fc
.getSelectedFile().getAbsolutePath();
870 if (url
!= null && !url
.toString().isEmpty()) {
871 imprt(url
.toString(), null, null);
876 * Actually import the {@link Story} into the main {@link LocalLibrary}.
878 * Should be called inside the UI thread.
881 * the {@link Story} to import by {@link URL}
883 * Action to execute on success
885 private void imprt(final String url
, final Runnable onSuccess
,
886 String onSuccessPgName
) {
887 final Progress pg
= new Progress();
888 final Progress pgImprt
= new Progress();
889 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
890 pg
.addProgress(pgImprt
, 95);
891 pg
.addProgress(pgOnSuccess
, 5);
893 outOfUi(pg
, new Runnable() {
898 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), pgImprt
);
899 } catch (IOException e
) {
903 final Exception e
= ex
;
905 final boolean ok
= (e
== null);
907 pgOnSuccess
.setProgress(0);
910 SwingUtilities
.invokeLater(new Runnable() {
913 JOptionPane
.showMessageDialog(GuiReaderFrame
.this,
914 "Cannot import: " + url
, e
.getMessage(),
915 JOptionPane
.ERROR_MESSAGE
);
919 if (onSuccess
!= null) {
929 * Enables or disables this component, depending on the value of the
930 * parameter <code>b</code>. An enabled component can respond to user input
931 * and generate events. Components are enabled initially by default.
933 * Disabling this component will also affect its children.
936 * If <code>true</code>, this component is enabled; otherwise
937 * this component is disabled
940 public void setEnabled(boolean b
) {
945 for (GuiReaderGroup group
: booksByType
.keySet()) {
948 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {