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
.HashMap
;
17 import java
.util
.List
;
19 import java
.util
.Map
.Entry
;
21 import javax
.swing
.BoxLayout
;
22 import javax
.swing
.JFileChooser
;
23 import javax
.swing
.JFrame
;
24 import javax
.swing
.JMenu
;
25 import javax
.swing
.JMenuBar
;
26 import javax
.swing
.JMenuItem
;
27 import javax
.swing
.JOptionPane
;
28 import javax
.swing
.JPanel
;
29 import javax
.swing
.JPopupMenu
;
30 import javax
.swing
.JScrollPane
;
31 import javax
.swing
.SwingUtilities
;
32 import javax
.swing
.filechooser
.FileFilter
;
33 import javax
.swing
.filechooser
.FileNameExtensionFilter
;
35 import be
.nikiroo
.fanfix
.Instance
;
36 import be
.nikiroo
.fanfix
.Library
;
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
.output
.BasicOutput
.OutputType
;
42 import be
.nikiroo
.fanfix
.reader
.LocalReaderBook
.BookActionListener
;
43 import be
.nikiroo
.utils
.Progress
;
44 import be
.nikiroo
.utils
.Version
;
45 import be
.nikiroo
.utils
.ui
.ConfigEditor
;
46 import be
.nikiroo
.utils
.ui
.ProgressBar
;
49 * A {@link Frame} that will show a {@link LocalReaderBook} item for each
50 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
51 * way to copy them to the {@link LocalReader} cache ({@link LocalReader#lib}),
52 * read them, delete them...
56 class LocalReaderFrame
extends JFrame
{
57 private static final long serialVersionUID
= 1L;
58 private LocalReader reader
;
59 private Map
<LocalReaderGroup
, String
> booksByType
;
60 private Map
<LocalReaderGroup
, String
> booksByAuthor
;
63 private ProgressBar pgBar
;
65 private LocalReaderBook selectedBook
;
66 private boolean words
; // words or authors (secondary info on books)
69 * Create a new {@link LocalReaderFrame}.
72 * the associated {@link LocalReader} to forward some commands
73 * and access its {@link Library}
75 * the type of {@link Story} to load, or NULL for all types
77 public LocalReaderFrame(LocalReader reader
, String type
) {
78 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
82 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
84 setLayout(new BorderLayout());
87 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
89 color
= Instance
.getUiConfig().getColor(UiConfig
.BACKGROUND_COLOR
);
92 pane
.setBackground(color
);
95 JScrollPane scroll
= new JScrollPane(pane
);
96 scroll
.getVerticalScrollBar().setUnitIncrement(16);
97 add(scroll
, BorderLayout
.CENTER
);
99 pgBar
= new ProgressBar();
100 add(pgBar
, BorderLayout
.SOUTH
);
102 pgBar
.addActionListener(new ActionListener() {
103 public void actionPerformed(ActionEvent e
) {
105 pgBar
.setProgress(null);
111 pgBar
.addUpdateListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e
) {
119 setJMenuBar(createMenu());
121 booksByType
= new HashMap
<LocalReaderGroup
, String
>();
122 booksByAuthor
= new HashMap
<LocalReaderGroup
, String
>();
124 addBookPane(type
, true);
131 * Add a new {@link LocalReaderGroup} on the frame to display the books of
132 * the selected type or author.
135 * the author or the type
137 * TRUE for type, FALSE for author
139 private void addBookPane(String value
, boolean type
) {
142 for (String tt
: Instance
.getLibrary().getTypes()) {
144 addBookPane(tt
, type
);
148 for (String tt
: Instance
.getLibrary().getAuthors()) {
150 addBookPane(tt
, type
);
158 LocalReaderGroup bookPane
= new LocalReaderGroup(reader
, value
, color
);
160 booksByType
.put(bookPane
, value
);
162 booksByAuthor
.put(bookPane
, value
);
171 bookPane
.setActionListener(new BookActionListener() {
172 public void select(LocalReaderBook book
) {
176 public void popupRequested(LocalReaderBook book
, MouseEvent e
) {
177 JPopupMenu popup
= new JPopupMenu();
178 popup
.add(createMenuItemOpenBook());
179 popup
.addSeparator();
180 popup
.add(createMenuItemExport());
181 popup
.add(createMenuItemClearCache());
182 popup
.add(createMenuItemRedownload());
183 popup
.addSeparator();
184 popup
.add(createMenuItemDelete());
185 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
188 public void action(final LocalReaderBook book
) {
194 private void removeBookPanes() {
196 booksByAuthor
.clear();
205 * Refresh the list of {@link LocalReaderBook}s from disk.
208 private void refreshBooks() {
209 for (LocalReaderGroup group
: booksByType
.keySet()) {
210 List
<MetaData
> stories
= Instance
.getLibrary().getListByType(
211 booksByType
.get(group
));
212 group
.refreshBooks(stories
, words
);
215 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {
216 List
<MetaData
> stories
= Instance
.getLibrary().getListByAuthor(
217 booksByAuthor
.get(group
));
218 group
.refreshBooks(stories
, words
);
226 * Create the main menu bar.
230 private JMenuBar
createMenu() {
231 bar
= new JMenuBar();
233 JMenu file
= new JMenu("File");
234 file
.setMnemonic(KeyEvent
.VK_F
);
236 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
237 imprt
.addActionListener(new ActionListener() {
238 public void actionPerformed(ActionEvent e
) {
242 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
243 imprtF
.addActionListener(new ActionListener() {
244 public void actionPerformed(ActionEvent e
) {
248 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
249 exit
.addActionListener(new ActionListener() {
250 public void actionPerformed(ActionEvent e
) {
251 LocalReaderFrame
.this.dispatchEvent(new WindowEvent(
252 LocalReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
256 file
.add(createMenuItemOpenBook());
257 file
.add(createMenuItemExport());
266 JMenu edit
= new JMenu("Edit");
267 edit
.setMnemonic(KeyEvent
.VK_E
);
269 edit
.add(createMenuItemClearCache());
270 edit
.add(createMenuItemRedownload());
272 edit
.add(createMenuItemDelete());
276 JMenu view
= new JMenu("View");
277 view
.setMnemonic(KeyEvent
.VK_V
);
278 JMenuItem vauthors
= new JMenuItem("Author");
279 vauthors
.setMnemonic(KeyEvent
.VK_A
);
280 vauthors
.addActionListener(new ActionListener() {
281 public void actionPerformed(ActionEvent e
) {
287 JMenuItem vwords
= new JMenuItem("Word count");
288 vwords
.setMnemonic(KeyEvent
.VK_W
);
289 vwords
.addActionListener(new ActionListener() {
290 public void actionPerformed(ActionEvent e
) {
298 JMenu sources
= new JMenu("Sources");
299 sources
.setMnemonic(KeyEvent
.VK_S
);
301 List
<String
> tt
= Instance
.getLibrary().getTypes();
303 for (final String type
: tt
) {
304 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
305 item
.addActionListener(new ActionListener() {
306 public void actionPerformed(ActionEvent e
) {
308 addBookPane(type
, true);
315 sources
.addSeparator();
321 JMenu authors
= new JMenu("Authors");
322 authors
.setMnemonic(KeyEvent
.VK_A
);
324 List
<String
> aa
= Instance
.getLibrary().getAuthors();
326 for (final String author
: aa
) {
327 JMenuItem item
= new JMenuItem(author
== null ?
"All"
328 : author
.isEmpty() ?
"[unknown]" : author
);
329 item
.addActionListener(new ActionListener() {
330 public void actionPerformed(ActionEvent e
) {
332 addBookPane(author
, false);
338 if (author
== null || author
.isEmpty()) {
339 authors
.addSeparator();
345 JMenu options
= new JMenu("Options");
346 options
.setMnemonic(KeyEvent
.VK_O
);
347 options
.add(createMenuItemConfig());
348 options
.add(createMenuItemUiConfig());
355 * Create the Fanfix Configuration menu item.
359 private JMenuItem
createMenuItemConfig() {
360 final String title
= "Fanfix Configuration";
361 JMenuItem item
= new JMenuItem(title
);
362 item
.setMnemonic(KeyEvent
.VK_F
);
364 item
.addActionListener(new ActionListener() {
365 public void actionPerformed(ActionEvent e
) {
366 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
367 Config
.class, Instance
.getConfig(),
368 "This is where you configure the options of the program.");
369 JFrame frame
= new JFrame(title
);
371 frame
.setSize(800, 600);
372 frame
.setVisible(true);
380 * Create the UI Configuration menu item.
384 private JMenuItem
createMenuItemUiConfig() {
385 final String title
= "UI Configuration";
386 JMenuItem item
= new JMenuItem(title
);
387 item
.setMnemonic(KeyEvent
.VK_U
);
389 item
.addActionListener(new ActionListener() {
390 public void actionPerformed(ActionEvent e
) {
391 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
392 UiConfig
.class, Instance
.getUiConfig(),
393 "This is where you configure the graphical appearence of the program.");
394 JFrame frame
= new JFrame(title
);
396 frame
.setSize(800, 600);
397 frame
.setVisible(true);
405 * Create the export menu item.
409 private JMenuItem
createMenuItemExport() {
410 final JFileChooser fc
= new JFileChooser();
411 fc
.setAcceptAllFileFilterUsed(false);
413 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
414 for (OutputType type
: OutputType
.values()) {
415 String ext
= type
.getDefaultExtension(false);
416 String desc
= type
.getDesc(false);
418 if (ext
== null || ext
.isEmpty()) {
419 filters
.put(createAllFilter(desc
), type
);
421 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
425 // First the "ALL" filters, then, the extension filters
426 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
427 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
428 fc
.addChoosableFileFilter(entry
.getKey());
431 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
432 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
433 fc
.addChoosableFileFilter(entry
.getKey());
438 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
439 export
.addActionListener(new ActionListener() {
440 public void actionPerformed(ActionEvent e
) {
441 if (selectedBook
!= null) {
442 fc
.showDialog(LocalReaderFrame
.this, "Save");
443 if (fc
.getSelectedFile() != null) {
444 final OutputType type
= filters
.get(fc
.getFileFilter());
445 final String path
= fc
.getSelectedFile()
447 + type
.getDefaultExtension(false);
448 final Progress pg
= new Progress();
449 outOfUi(pg
, new Runnable() {
452 Instance
.getLibrary().export(
453 selectedBook
.getMeta().getLuid(),
455 } catch (IOException e
) {
469 * Create a {@link FileFilter} that accepts all files and return the given
477 private FileFilter
createAllFilter(final String desc
) {
478 return new FileFilter() {
480 public String
getDescription() {
485 public boolean accept(File f
) {
492 * Create the refresh (delete cache) menu item.
496 private JMenuItem
createMenuItemClearCache() {
497 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
498 refresh
.addActionListener(new ActionListener() {
499 public void actionPerformed(ActionEvent e
) {
500 if (selectedBook
!= null) {
501 outOfUi(null, new Runnable() {
503 reader
.clearLocalReaderCache(selectedBook
.getMeta()
505 selectedBook
.setCached(false);
506 SwingUtilities
.invokeLater(new Runnable() {
508 selectedBook
.repaint();
521 * Create the redownload (then delete original) menu item.
525 private JMenuItem
createMenuItemRedownload() {
526 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
527 refresh
.addActionListener(new ActionListener() {
528 public void actionPerformed(ActionEvent e
) {
529 if (selectedBook
!= null) {
530 final MetaData meta
= selectedBook
.getMeta();
531 imprt(meta
.getUrl(), new Runnable() {
533 reader
.delete(meta
.getLuid());
534 LocalReaderFrame
.this.selectedBook
= null;
536 }, "Removing old copy");
545 * Create the delete menu item.
549 private JMenuItem
createMenuItemDelete() {
550 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
551 delete
.addActionListener(new ActionListener() {
552 public void actionPerformed(ActionEvent e
) {
553 if (selectedBook
!= null) {
554 outOfUi(null, new Runnable() {
556 reader
.delete(selectedBook
.getMeta().getLuid());
568 * Create the open menu item.
572 private JMenuItem
createMenuItemOpenBook() {
573 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
574 open
.addActionListener(new ActionListener() {
575 public void actionPerformed(ActionEvent e
) {
576 if (selectedBook
!= null) {
577 openBook(selectedBook
);
586 * Open a {@link LocalReaderBook} item.
589 * the {@link LocalReaderBook} to open
591 private void openBook(final LocalReaderBook book
) {
592 final Progress pg
= new Progress();
593 outOfUi(pg
, new Runnable() {
596 reader
.open(book
.getMeta().getLuid(), pg
);
597 SwingUtilities
.invokeLater(new Runnable() {
599 book
.setCached(true);
602 } catch (IOException e
) {
603 // TODO: error message?
611 * Process the given action out of the Swing UI thread and link the given
612 * {@link ProgressBar} to the action.
614 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
615 * to done when the action is done.
618 * the {@link ProgressBar} or NULL
622 private void outOfUi(Progress progress
, final Runnable run
) {
623 final Progress pg
= new Progress();
624 final Progress reload
= new Progress("Reload books");
625 if (progress
== null) {
626 progress
= new Progress();
629 pg
.addProgress(progress
, 90);
630 pg
.addProgress(reload
, 10);
633 pgBar
.setProgress(pg
);
637 new Thread(new Runnable() {
641 reload
.setProgress(100);
643 // will trigger pgBar ActionListener:
644 pg
.setProgress(pg
.getMax());
647 }, "outOfUi thread").start();
651 * Import a {@link Story} into the main {@link Library}.
653 * Should be called inside the UI thread.
656 * TRUE for an {@link URL}, false for a {@link File}
658 private void imprt(boolean askUrl
) {
659 JFileChooser fc
= new JFileChooser();
663 String clipboard
= "";
665 clipboard
= ("" + Toolkit
.getDefaultToolkit()
666 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
668 } catch (Exception e
) {
669 // No data will be handled
672 if (clipboard
== null || !clipboard
.startsWith("http")) {
676 url
= JOptionPane
.showInputDialog(LocalReaderFrame
.this,
677 "url of the story to import?", "Importing from URL",
678 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
679 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
680 url
= fc
.getSelectedFile().getAbsolutePath();
685 if (url
!= null && !url
.toString().isEmpty()) {
686 imprt(url
.toString(), null, null);
691 * Actually import the {@link Story} into the main {@link Library}.
693 * Should be called inside the UI thread.
696 * the {@link Story} to import by {@link URL}
698 * Action to execute on success
700 private void imprt(final String url
, final Runnable onSuccess
,
701 String onSuccessPgName
) {
702 final Progress pg
= new Progress();
703 final Progress pgImprt
= new Progress();
704 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
705 pg
.addProgress(pgImprt
, 95);
706 pg
.addProgress(pgOnSuccess
, 5);
708 outOfUi(pg
, new Runnable() {
712 Instance
.getLibrary().imprt(BasicReader
.getUrl(url
),
714 } catch (IOException e
) {
718 final Exception e
= ex
;
720 final boolean ok
= (e
== null);
722 pgOnSuccess
.setProgress(0);
725 SwingUtilities
.invokeLater(new Runnable() {
727 JOptionPane
.showMessageDialog(
728 LocalReaderFrame
.this, "Cannot import: "
729 + url
, e
.getMessage(),
730 JOptionPane
.ERROR_MESSAGE
);
734 if (onSuccess
!= null) {
738 pgOnSuccess
.setProgress(100);
744 * Enables or disables this component, depending on the value of the
745 * parameter <code>b</code>. An enabled component can respond to user input
746 * and generate events. Components are enabled initially by default.
748 * Disabling this component will also affect its children.
751 * If <code>true</code>, this component is enabled; otherwise
752 * this component is disabled
755 public void setEnabled(boolean b
) {
757 for (LocalReaderGroup group
: booksByType
.keySet()) {
760 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {