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() {
104 public void actionPerformed(ActionEvent e
) {
106 pgBar
.setProgress(null);
112 pgBar
.addUpdateListener(new ActionListener() {
113 public void actionPerformed(ActionEvent e
) {
120 booksByType
= new HashMap
<GuiReaderGroup
, String
>();
121 booksByAuthor
= new HashMap
<GuiReaderGroup
, String
>();
123 pane
.setVisible(false);
124 final Progress pg
= new Progress();
125 final String typeF
= type
;
126 outOfUi(pg
, new Runnable() {
128 GuiReaderFrame
.this.reader
.getLibrary().refresh(false, pg
);
130 setJMenuBar(createMenu());
131 addBookPane(typeF
, true);
134 pane
.setVisible(true);
142 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
143 * selected type or author.
146 * the author or the type, or NULL to get all the
149 * TRUE for type, FALSE for author
151 private void addBookPane(String value
, boolean type
) {
154 for (String tt
: reader
.getLibrary().getSources()) {
156 addBookPane(tt
, type
);
160 for (String tt
: reader
.getLibrary().getAuthors()) {
162 addBookPane(tt
, type
);
170 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, value
, color
);
172 booksByType
.put(bookPane
, value
);
174 booksByAuthor
.put(bookPane
, value
);
183 bookPane
.setActionListener(new BookActionListener() {
184 public void select(GuiReaderBook book
) {
188 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
189 JPopupMenu popup
= new JPopupMenu();
190 popup
.add(createMenuItemOpenBook());
191 popup
.addSeparator();
192 popup
.add(createMenuItemExport());
193 popup
.add(createMenuItemMove());
194 popup
.add(createMenuItemClearCache());
195 popup
.add(createMenuItemRedownload());
196 popup
.addSeparator();
197 popup
.add(createMenuItemDelete());
198 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
201 public void action(final GuiReaderBook book
) {
207 private void removeBookPanes() {
209 booksByAuthor
.clear();
218 * Refresh the list of {@link GuiReaderBook}s from disk.
221 private void refreshBooks() {
222 for (GuiReaderGroup group
: booksByType
.keySet()) {
223 List
<MetaData
> stories
= reader
.getLibrary().getListBySource(
224 booksByType
.get(group
));
225 group
.refreshBooks(stories
, words
);
228 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
229 List
<MetaData
> stories
= reader
.getLibrary().getListByAuthor(
230 booksByAuthor
.get(group
));
231 group
.refreshBooks(stories
, words
);
239 * Create the main menu bar.
243 private JMenuBar
createMenu() {
244 bar
= new JMenuBar();
246 JMenu file
= new JMenu("File");
247 file
.setMnemonic(KeyEvent
.VK_F
);
249 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
250 imprt
.addActionListener(new ActionListener() {
251 public void actionPerformed(ActionEvent e
) {
255 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
256 imprtF
.addActionListener(new ActionListener() {
257 public void actionPerformed(ActionEvent e
) {
261 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
262 exit
.addActionListener(new ActionListener() {
263 public void actionPerformed(ActionEvent e
) {
264 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
265 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
269 file
.add(createMenuItemOpenBook());
270 file
.add(createMenuItemExport());
271 file
.add(createMenuItemMove());
280 JMenu edit
= new JMenu("Edit");
281 edit
.setMnemonic(KeyEvent
.VK_E
);
283 edit
.add(createMenuItemClearCache());
284 edit
.add(createMenuItemRedownload());
286 edit
.add(createMenuItemDelete());
290 JMenu view
= new JMenu("View");
291 view
.setMnemonic(KeyEvent
.VK_V
);
292 JMenuItem vauthors
= new JMenuItem("Author");
293 vauthors
.setMnemonic(KeyEvent
.VK_A
);
294 vauthors
.addActionListener(new ActionListener() {
295 public void actionPerformed(ActionEvent e
) {
301 JMenuItem vwords
= new JMenuItem("Word count");
302 vwords
.setMnemonic(KeyEvent
.VK_W
);
303 vwords
.addActionListener(new ActionListener() {
304 public void actionPerformed(ActionEvent e
) {
312 JMenu sources
= new JMenu("Sources");
313 sources
.setMnemonic(KeyEvent
.VK_S
);
315 List
<String
> tt
= reader
.getLibrary().getSources();
317 for (final String type
: tt
) {
318 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
319 item
.addActionListener(new ActionListener() {
320 public void actionPerformed(ActionEvent e
) {
322 addBookPane(type
, true);
329 sources
.addSeparator();
335 JMenu authors
= new JMenu("Authors");
336 authors
.setMnemonic(KeyEvent
.VK_A
);
338 List
<String
> aa
= reader
.getLibrary().getAuthors();
340 for (final String author
: aa
) {
341 JMenuItem item
= new JMenuItem(author
== null ?
"All"
342 : author
.isEmpty() ?
"[unknown]" : author
);
343 item
.addActionListener(new ActionListener() {
344 public void actionPerformed(ActionEvent e
) {
346 addBookPane(author
, false);
352 if (author
== null || author
.isEmpty()) {
353 authors
.addSeparator();
359 JMenu options
= new JMenu("Options");
360 options
.setMnemonic(KeyEvent
.VK_O
);
361 options
.add(createMenuItemConfig());
362 options
.add(createMenuItemUiConfig());
369 * Create the Fanfix Configuration menu item.
373 private JMenuItem
createMenuItemConfig() {
374 final String title
= "Fanfix Configuration";
375 JMenuItem item
= new JMenuItem(title
);
376 item
.setMnemonic(KeyEvent
.VK_F
);
378 item
.addActionListener(new ActionListener() {
379 public void actionPerformed(ActionEvent e
) {
380 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
381 Config
.class, Instance
.getConfig(),
382 "This is where you configure the options of the program.");
383 JFrame frame
= new JFrame(title
);
385 frame
.setSize(800, 600);
386 frame
.setVisible(true);
394 * Create the UI Configuration menu item.
398 private JMenuItem
createMenuItemUiConfig() {
399 final String title
= "UI Configuration";
400 JMenuItem item
= new JMenuItem(title
);
401 item
.setMnemonic(KeyEvent
.VK_U
);
403 item
.addActionListener(new ActionListener() {
404 public void actionPerformed(ActionEvent e
) {
405 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
406 UiConfig
.class, Instance
.getUiConfig(),
407 "This is where you configure the graphical appearence of the program.");
408 JFrame frame
= new JFrame(title
);
410 frame
.setSize(800, 600);
411 frame
.setVisible(true);
419 * Create the export menu item.
423 private JMenuItem
createMenuItemExport() {
424 final JFileChooser fc
= new JFileChooser();
425 fc
.setAcceptAllFileFilterUsed(false);
427 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
428 for (OutputType type
: OutputType
.values()) {
429 String ext
= type
.getDefaultExtension(false);
430 String desc
= type
.getDesc(false);
432 if (ext
== null || ext
.isEmpty()) {
433 filters
.put(createAllFilter(desc
), type
);
435 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
439 // First the "ALL" filters, then, the extension filters
440 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
441 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
442 fc
.addChoosableFileFilter(entry
.getKey());
445 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
446 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
447 fc
.addChoosableFileFilter(entry
.getKey());
452 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
453 export
.addActionListener(new ActionListener() {
454 public void actionPerformed(ActionEvent e
) {
455 if (selectedBook
!= null) {
456 fc
.showDialog(GuiReaderFrame
.this, "Save");
457 if (fc
.getSelectedFile() != null) {
458 final OutputType type
= filters
.get(fc
.getFileFilter());
459 final String path
= fc
.getSelectedFile()
461 + type
.getDefaultExtension(false);
462 final Progress pg
= new Progress();
463 outOfUi(pg
, new Runnable() {
466 reader
.getLibrary().export(
467 selectedBook
.getMeta().getLuid(),
469 } catch (IOException e
) {
483 * Create a {@link FileFilter} that accepts all files and return the given
491 private FileFilter
createAllFilter(final String desc
) {
492 return new FileFilter() {
494 public String
getDescription() {
499 public boolean accept(File f
) {
506 * Create the refresh (delete cache) menu item.
510 private JMenuItem
createMenuItemClearCache() {
511 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
512 refresh
.addActionListener(new ActionListener() {
513 public void actionPerformed(ActionEvent e
) {
514 if (selectedBook
!= null) {
515 outOfUi(null, new Runnable() {
517 reader
.clearLocalReaderCache(selectedBook
.getMeta()
519 selectedBook
.setCached(false);
520 SwingUtilities
.invokeLater(new Runnable() {
522 selectedBook
.repaint();
535 * Create the delete menu item.
539 private JMenuItem
createMenuItemMove() {
540 JMenu moveTo
= new JMenu("Move to...");
541 moveTo
.setMnemonic(KeyEvent
.VK_M
);
543 List
<String
> types
= new ArrayList
<String
>();
545 types
.addAll(reader
.getLibrary().getSources());
547 for (String type
: types
) {
548 JMenuItem item
= new JMenuItem(type
== null ?
"New type..." : type
);
552 moveTo
.addSeparator();
555 final String ftype
= type
;
556 item
.addActionListener(new ActionListener() {
557 public void actionPerformed(ActionEvent e
) {
558 if (selectedBook
!= null) {
561 Object rep
= JOptionPane
.showInputDialog(
562 GuiReaderFrame
.this, "Move to:",
564 JOptionPane
.QUESTION_MESSAGE
, null, null,
565 selectedBook
.getMeta().getSource());
569 type
= rep
.toString();
573 final String ftype
= type
;
574 outOfUi(null, new Runnable() {
576 reader
.changeType(selectedBook
.getMeta()
581 SwingUtilities
.invokeLater(new Runnable() {
583 setJMenuBar(createMenu());
597 * Create the redownload (then delete original) menu item.
601 private JMenuItem
createMenuItemRedownload() {
602 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
603 refresh
.addActionListener(new ActionListener() {
604 public void actionPerformed(ActionEvent e
) {
605 if (selectedBook
!= null) {
606 final MetaData meta
= selectedBook
.getMeta();
607 imprt(meta
.getUrl(), new Runnable() {
609 reader
.delete(meta
.getLuid());
610 GuiReaderFrame
.this.selectedBook
= null;
612 }, "Removing old copy");
621 * Create the delete menu item.
625 private JMenuItem
createMenuItemDelete() {
626 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
627 delete
.addActionListener(new ActionListener() {
628 public void actionPerformed(ActionEvent e
) {
629 if (selectedBook
!= null) {
630 outOfUi(null, new Runnable() {
632 reader
.delete(selectedBook
.getMeta().getLuid());
644 * Create the open menu item.
648 private JMenuItem
createMenuItemOpenBook() {
649 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
650 open
.addActionListener(new ActionListener() {
651 public void actionPerformed(ActionEvent e
) {
652 if (selectedBook
!= null) {
653 openBook(selectedBook
);
662 * Open a {@link GuiReaderBook} item.
665 * the {@link GuiReaderBook} to open
667 private void openBook(final GuiReaderBook book
) {
668 final Progress pg
= new Progress();
669 outOfUi(pg
, new Runnable() {
672 reader
.open(book
.getMeta().getLuid(), pg
);
673 SwingUtilities
.invokeLater(new Runnable() {
675 book
.setCached(true);
678 } catch (IOException e
) {
679 // TODO: error message?
687 * Process the given action out of the Swing UI thread and link the given
688 * {@link ProgressBar} to the action.
690 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
691 * to done when the action is done.
694 * the {@link ProgressBar} or NULL
698 private void outOfUi(Progress progress
, final Runnable run
) {
699 final Progress pg
= new Progress();
700 final Progress reload
= new Progress("Reload books");
701 if (progress
== null) {
702 progress
= new Progress();
705 pg
.addProgress(progress
, 90);
706 pg
.addProgress(reload
, 10);
709 pgBar
.setProgress(pg
);
713 new Thread(new Runnable() {
719 // will trigger pgBar ActionListener:
723 }, "outOfUi thread").start();
727 * Import a {@link Story} into the main {@link LocalLibrary}.
729 * Should be called inside the UI thread.
732 * TRUE for an {@link URL}, false for a {@link File}
734 private void imprt(boolean askUrl
) {
735 JFileChooser fc
= new JFileChooser();
739 String clipboard
= "";
741 clipboard
= ("" + Toolkit
.getDefaultToolkit()
742 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
744 } catch (Exception e
) {
745 // No data will be handled
748 if (clipboard
== null || !clipboard
.startsWith("http")) {
752 url
= JOptionPane
.showInputDialog(GuiReaderFrame
.this,
753 "url of the story to import?", "Importing from URL",
754 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
755 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
756 url
= fc
.getSelectedFile().getAbsolutePath();
761 if (url
!= null && !url
.toString().isEmpty()) {
762 imprt(url
.toString(), null, null);
767 * Actually import the {@link Story} into the main {@link LocalLibrary}.
769 * Should be called inside the UI thread.
772 * the {@link Story} to import by {@link URL}
774 * Action to execute on success
776 private void imprt(final String url
, final Runnable onSuccess
,
777 String onSuccessPgName
) {
778 final Progress pg
= new Progress();
779 final Progress pgImprt
= new Progress();
780 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
781 pg
.addProgress(pgImprt
, 95);
782 pg
.addProgress(pgOnSuccess
, 5);
784 outOfUi(pg
, new Runnable() {
788 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), pgImprt
);
789 } catch (IOException e
) {
793 final Exception e
= ex
;
795 final boolean ok
= (e
== null);
797 pgOnSuccess
.setProgress(0);
800 SwingUtilities
.invokeLater(new Runnable() {
802 JOptionPane
.showMessageDialog(GuiReaderFrame
.this,
803 "Cannot import: " + url
, e
.getMessage(),
804 JOptionPane
.ERROR_MESSAGE
);
808 if (onSuccess
!= null) {
818 * Enables or disables this component, depending on the value of the
819 * parameter <code>b</code>. An enabled component can respond to user input
820 * and generate events. Components are enabled initially by default.
822 * Disabling this component will also affect its children.
825 * If <code>true</code>, this component is enabled; otherwise
826 * this component is disabled
829 public void setEnabled(boolean b
) {
834 for (GuiReaderGroup group
: booksByType
.keySet()) {
837 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {