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
.UiConfig
;
38 import be
.nikiroo
.fanfix
.data
.MetaData
;
39 import be
.nikiroo
.fanfix
.data
.Story
;
40 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
41 import be
.nikiroo
.fanfix
.reader
.LocalReaderBook
.BookActionListener
;
42 import be
.nikiroo
.utils
.Progress
;
43 import be
.nikiroo
.utils
.Version
;
44 import be
.nikiroo
.utils
.ui
.ProgressBar
;
47 * A {@link Frame} that will show a {@link LocalReaderBook} item for each
48 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
49 * way to copy them to the {@link LocalReader} cache ({@link LocalReader#lib}),
50 * read them, delete them...
54 class LocalReaderFrame
extends JFrame
{
55 private static final long serialVersionUID
= 1L;
56 private LocalReader reader
;
57 private Map
<LocalReaderGroup
, String
> booksByType
;
58 private Map
<LocalReaderGroup
, String
> booksByAuthor
;
61 private ProgressBar pgBar
;
63 private LocalReaderBook selectedBook
;
64 private boolean words
; // words or authors (secondary info on books)
67 * Create a new {@link LocalReaderFrame}.
70 * the associated {@link LocalReader} to forward some commands
71 * and access its {@link Library}
73 * the type of {@link Story} to load, or NULL for all types
75 public LocalReaderFrame(LocalReader reader
, String type
) {
76 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
80 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
82 setLayout(new BorderLayout());
85 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
87 color
= Instance
.getUiConfig().getColor(UiConfig
.BACKGROUND_COLOR
);
90 pane
.setBackground(color
);
93 JScrollPane scroll
= new JScrollPane(pane
);
94 scroll
.getVerticalScrollBar().setUnitIncrement(16);
95 add(scroll
, BorderLayout
.CENTER
);
97 pgBar
= new ProgressBar();
98 add(pgBar
, BorderLayout
.SOUTH
);
100 pgBar
.addActionListener(new ActionListener() {
101 public void actionPerformed(ActionEvent e
) {
103 pgBar
.setProgress(null);
109 pgBar
.addUpdateListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e
) {
116 setJMenuBar(createMenu());
118 booksByType
= new HashMap
<LocalReaderGroup
, String
>();
119 booksByAuthor
= new HashMap
<LocalReaderGroup
, String
>();
121 addBookPane(type
, true);
128 * Add a new {@link LocalReaderGroup} on the frame to display the books of
129 * the selected type or author.
132 * the author or the type
134 * TRUE for type, FALSE for author
136 private void addBookPane(String value
, boolean type
) {
139 for (String tt
: Instance
.getLibrary().getTypes()) {
141 addBookPane(tt
, type
);
145 for (String tt
: Instance
.getLibrary().getAuthors()) {
147 addBookPane(tt
, type
);
155 LocalReaderGroup bookPane
= new LocalReaderGroup(reader
, value
, color
);
157 booksByType
.put(bookPane
, value
);
159 booksByAuthor
.put(bookPane
, value
);
168 bookPane
.setActionListener(new BookActionListener() {
169 public void select(LocalReaderBook book
) {
173 public void popupRequested(LocalReaderBook book
, MouseEvent e
) {
174 JPopupMenu popup
= new JPopupMenu();
175 popup
.add(createMenuItemOpenBook());
176 popup
.addSeparator();
177 popup
.add(createMenuItemExport());
178 popup
.add(createMenuItemClearCache());
179 popup
.add(createMenuItemRedownload());
180 popup
.addSeparator();
181 popup
.add(createMenuItemDelete());
182 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
185 public void action(final LocalReaderBook book
) {
191 private void removeBookPanes() {
193 booksByAuthor
.clear();
202 * Refresh the list of {@link LocalReaderBook}s from disk.
205 private void refreshBooks() {
206 for (LocalReaderGroup group
: booksByType
.keySet()) {
207 List
<MetaData
> stories
= Instance
.getLibrary().getListByType(
208 booksByType
.get(group
));
209 group
.refreshBooks(stories
, words
);
212 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {
213 List
<MetaData
> stories
= Instance
.getLibrary().getListByAuthor(
214 booksByAuthor
.get(group
));
215 group
.refreshBooks(stories
, words
);
223 * Create the main menu bar.
227 private JMenuBar
createMenu() {
228 bar
= new JMenuBar();
230 JMenu file
= new JMenu("File");
231 file
.setMnemonic(KeyEvent
.VK_F
);
233 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
234 imprt
.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent e
) {
239 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
240 imprtF
.addActionListener(new ActionListener() {
241 public void actionPerformed(ActionEvent e
) {
245 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
246 exit
.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent e
) {
248 LocalReaderFrame
.this.dispatchEvent(new WindowEvent(
249 LocalReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
253 file
.add(createMenuItemOpenBook());
254 file
.add(createMenuItemExport());
263 JMenu edit
= new JMenu("Edit");
264 edit
.setMnemonic(KeyEvent
.VK_E
);
266 edit
.add(createMenuItemClearCache());
267 edit
.add(createMenuItemRedownload());
269 edit
.add(createMenuItemDelete());
273 JMenu view
= new JMenu("View");
274 view
.setMnemonic(KeyEvent
.VK_V
);
275 JMenuItem vauthors
= new JMenuItem("Author");
276 vauthors
.setMnemonic(KeyEvent
.VK_A
);
277 vauthors
.addActionListener(new ActionListener() {
278 public void actionPerformed(ActionEvent e
) {
284 JMenuItem vwords
= new JMenuItem("Word count");
285 vwords
.setMnemonic(KeyEvent
.VK_W
);
286 vwords
.addActionListener(new ActionListener() {
287 public void actionPerformed(ActionEvent e
) {
295 JMenu sources
= new JMenu("Sources");
296 sources
.setMnemonic(KeyEvent
.VK_S
);
298 List
<String
> tt
= Instance
.getLibrary().getTypes();
300 for (final String type
: tt
) {
301 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
302 item
.addActionListener(new ActionListener() {
303 public void actionPerformed(ActionEvent e
) {
305 addBookPane(type
, true);
312 sources
.addSeparator();
318 JMenu authors
= new JMenu("Authors");
319 authors
.setMnemonic(KeyEvent
.VK_A
);
321 List
<String
> aa
= Instance
.getLibrary().getAuthors();
323 for (final String author
: aa
) {
324 JMenuItem item
= new JMenuItem(author
== null ?
"All"
325 : author
.isEmpty() ?
"[unknown]" : author
);
326 item
.addActionListener(new ActionListener() {
327 public void actionPerformed(ActionEvent e
) {
329 addBookPane(author
, false);
335 if (author
== null || author
.isEmpty()) {
336 authors
.addSeparator();
346 * Create the export menu item.
350 private JMenuItem
createMenuItemExport() {
351 final JFileChooser fc
= new JFileChooser();
352 fc
.setAcceptAllFileFilterUsed(false);
354 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
355 for (OutputType type
: OutputType
.values()) {
356 String ext
= type
.getDefaultExtension(false);
357 String desc
= type
.getDesc(false);
359 if (ext
== null || ext
.isEmpty()) {
360 filters
.put(createAllFilter(desc
), type
);
362 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
366 // First the "ALL" filters, then, the extension filters
367 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
368 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
369 fc
.addChoosableFileFilter(entry
.getKey());
372 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
373 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
374 fc
.addChoosableFileFilter(entry
.getKey());
379 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
380 export
.addActionListener(new ActionListener() {
381 public void actionPerformed(ActionEvent e
) {
382 if (selectedBook
!= null) {
383 fc
.showDialog(LocalReaderFrame
.this, "Save");
384 if (fc
.getSelectedFile() != null) {
385 final OutputType type
= filters
.get(fc
.getFileFilter());
386 final String path
= fc
.getSelectedFile()
388 + type
.getDefaultExtension(false);
389 final Progress pg
= new Progress();
390 outOfUi(pg
, new Runnable() {
393 Instance
.getLibrary().export(
394 selectedBook
.getMeta().getLuid(),
396 } catch (IOException e
) {
410 * Create a {@link FileFilter} that accepts all files and return the given
418 private FileFilter
createAllFilter(final String desc
) {
419 return new FileFilter() {
421 public String
getDescription() {
426 public boolean accept(File f
) {
433 * Create the refresh (delete cache) menu item.
437 private JMenuItem
createMenuItemClearCache() {
438 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
439 refresh
.addActionListener(new ActionListener() {
440 public void actionPerformed(ActionEvent e
) {
441 if (selectedBook
!= null) {
442 outOfUi(null, new Runnable() {
444 reader
.clearLocalReaderCache(selectedBook
.getMeta()
446 selectedBook
.setCached(false);
447 SwingUtilities
.invokeLater(new Runnable() {
449 selectedBook
.repaint();
462 * Create the redownload (then delete original) menu item.
466 private JMenuItem
createMenuItemRedownload() {
467 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
468 refresh
.addActionListener(new ActionListener() {
469 public void actionPerformed(ActionEvent e
) {
470 if (selectedBook
!= null) {
471 final MetaData meta
= selectedBook
.getMeta();
472 imprt(meta
.getUrl(), new Runnable() {
474 reader
.delete(meta
.getLuid());
475 LocalReaderFrame
.this.selectedBook
= null;
477 }, "Removing old copy");
486 * Create the delete menu item.
490 private JMenuItem
createMenuItemDelete() {
491 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
492 delete
.addActionListener(new ActionListener() {
493 public void actionPerformed(ActionEvent e
) {
494 if (selectedBook
!= null) {
495 outOfUi(null, new Runnable() {
497 reader
.delete(selectedBook
.getMeta().getLuid());
509 * Create the open menu item.
513 private JMenuItem
createMenuItemOpenBook() {
514 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
515 open
.addActionListener(new ActionListener() {
516 public void actionPerformed(ActionEvent e
) {
517 if (selectedBook
!= null) {
518 openBook(selectedBook
);
527 * Open a {@link LocalReaderBook} item.
530 * the {@link LocalReaderBook} to open
532 private void openBook(final LocalReaderBook book
) {
533 final Progress pg
= new Progress();
534 outOfUi(pg
, new Runnable() {
537 reader
.open(book
.getMeta().getLuid(), pg
);
538 SwingUtilities
.invokeLater(new Runnable() {
540 book
.setCached(true);
543 } catch (IOException e
) {
544 // TODO: error message?
552 * Process the given action out of the Swing UI thread and link the given
553 * {@link ProgressBar} to the action.
555 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
556 * to done when the action is done.
559 * the {@link ProgressBar} or NULL
563 private void outOfUi(Progress progress
, final Runnable run
) {
564 final Progress pg
= new Progress();
565 final Progress reload
= new Progress("Reload books");
566 if (progress
== null) {
567 progress
= new Progress();
570 pg
.addProgress(progress
, 90);
571 pg
.addProgress(reload
, 10);
574 pgBar
.setProgress(pg
);
578 new Thread(new Runnable() {
582 reload
.setProgress(100);
584 // will trigger pgBar ActionListener:
585 pg
.setProgress(pg
.getMax());
588 }, "outOfUi thread").start();
592 * Import a {@link Story} into the main {@link Library}.
594 * Should be called inside the UI thread.
597 * TRUE for an {@link URL}, false for a {@link File}
599 private void imprt(boolean askUrl
) {
600 JFileChooser fc
= new JFileChooser();
604 String clipboard
= "";
606 clipboard
= ("" + Toolkit
.getDefaultToolkit()
607 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
609 } catch (Exception e
) {
610 // No data will be handled
613 if (clipboard
== null || !clipboard
.startsWith("http")) {
617 url
= JOptionPane
.showInputDialog(LocalReaderFrame
.this,
618 "url of the story to import?", "Importing from URL",
619 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
620 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
621 url
= fc
.getSelectedFile().getAbsolutePath();
626 if (url
!= null && !url
.toString().isEmpty()) {
627 imprt(url
.toString(), null, null);
632 * Actually import the {@link Story} into the main {@link Library}.
634 * Should be called inside the UI thread.
637 * the {@link Story} to import by {@link URL}
639 * Action to execute on success
641 private void imprt(final String url
, final Runnable onSuccess
,
642 String onSuccessPgName
) {
643 final Progress pg
= new Progress();
644 final Progress pgImprt
= new Progress();
645 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
646 pg
.addProgress(pgImprt
, 95);
647 pg
.addProgress(pgOnSuccess
, 5);
649 outOfUi(pg
, new Runnable() {
653 Instance
.getLibrary().imprt(BasicReader
.getUrl(url
),
655 } catch (IOException e
) {
659 final Exception e
= ex
;
661 final boolean ok
= (e
== null);
663 pgOnSuccess
.setProgress(0);
666 SwingUtilities
.invokeLater(new Runnable() {
668 JOptionPane
.showMessageDialog(
669 LocalReaderFrame
.this, "Cannot import: "
670 + url
, e
.getMessage(),
671 JOptionPane
.ERROR_MESSAGE
);
675 if (onSuccess
!= null) {
679 pgOnSuccess
.setProgress(100);
685 * Enables or disables this component, depending on the value of the
686 * parameter <code>b</code>. An enabled component can respond to user input
687 * and generate events. Components are enabled initially by default.
689 * Disabling this component will also affect its children.
692 * If <code>true</code>, this component is enabled; otherwise
693 * this component is disabled
696 public void setEnabled(boolean b
) {
698 for (LocalReaderGroup group
: booksByType
.keySet()) {
701 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {