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
) {
117 setJMenuBar(createMenu());
119 booksByType
= new HashMap
<LocalReaderGroup
, String
>();
120 booksByAuthor
= new HashMap
<LocalReaderGroup
, String
>();
122 addBookPane(type
, true);
129 * Add a new {@link LocalReaderGroup} on the frame to display the books of
130 * the selected type or author.
133 * the author or the type
135 * TRUE for type, FALSE for author
137 private void addBookPane(String value
, boolean type
) {
140 for (String tt
: Instance
.getLibrary().getTypes()) {
142 addBookPane(tt
, type
);
146 for (String tt
: Instance
.getLibrary().getAuthors()) {
148 addBookPane(tt
, type
);
156 LocalReaderGroup bookPane
= new LocalReaderGroup(reader
, value
, color
);
158 booksByType
.put(bookPane
, value
);
160 booksByAuthor
.put(bookPane
, value
);
169 bookPane
.setActionListener(new BookActionListener() {
170 public void select(LocalReaderBook book
) {
174 public void popupRequested(LocalReaderBook book
, MouseEvent e
) {
175 JPopupMenu popup
= new JPopupMenu();
176 popup
.add(createMenuItemOpenBook());
177 popup
.addSeparator();
178 popup
.add(createMenuItemExport());
179 popup
.add(createMenuItemClearCache());
180 popup
.add(createMenuItemRedownload());
181 popup
.addSeparator();
182 popup
.add(createMenuItemDelete());
183 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
186 public void action(final LocalReaderBook book
) {
192 private void removeBookPanes() {
194 booksByAuthor
.clear();
203 * Refresh the list of {@link LocalReaderBook}s from disk.
206 private void refreshBooks() {
207 for (LocalReaderGroup group
: booksByType
.keySet()) {
208 List
<MetaData
> stories
= Instance
.getLibrary().getListByType(
209 booksByType
.get(group
));
210 group
.refreshBooks(stories
, words
);
213 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {
214 List
<MetaData
> stories
= Instance
.getLibrary().getListByAuthor(
215 booksByAuthor
.get(group
));
216 group
.refreshBooks(stories
, words
);
224 * Create the main menu bar.
228 private JMenuBar
createMenu() {
229 bar
= new JMenuBar();
231 JMenu file
= new JMenu("File");
232 file
.setMnemonic(KeyEvent
.VK_F
);
234 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
235 imprt
.addActionListener(new ActionListener() {
236 public void actionPerformed(ActionEvent e
) {
240 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
241 imprtF
.addActionListener(new ActionListener() {
242 public void actionPerformed(ActionEvent e
) {
246 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
247 exit
.addActionListener(new ActionListener() {
248 public void actionPerformed(ActionEvent e
) {
249 LocalReaderFrame
.this.dispatchEvent(new WindowEvent(
250 LocalReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
254 file
.add(createMenuItemOpenBook());
255 file
.add(createMenuItemExport());
264 JMenu edit
= new JMenu("Edit");
265 edit
.setMnemonic(KeyEvent
.VK_E
);
267 edit
.add(createMenuItemClearCache());
268 edit
.add(createMenuItemRedownload());
270 edit
.add(createMenuItemDelete());
274 JMenu view
= new JMenu("View");
275 view
.setMnemonic(KeyEvent
.VK_V
);
276 JMenuItem vauthors
= new JMenuItem("Author");
277 vauthors
.setMnemonic(KeyEvent
.VK_A
);
278 vauthors
.addActionListener(new ActionListener() {
279 public void actionPerformed(ActionEvent e
) {
285 JMenuItem vwords
= new JMenuItem("Word count");
286 vwords
.setMnemonic(KeyEvent
.VK_W
);
287 vwords
.addActionListener(new ActionListener() {
288 public void actionPerformed(ActionEvent e
) {
296 JMenu sources
= new JMenu("Sources");
297 sources
.setMnemonic(KeyEvent
.VK_S
);
299 List
<String
> tt
= Instance
.getLibrary().getTypes();
301 for (final String type
: tt
) {
302 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
303 item
.addActionListener(new ActionListener() {
304 public void actionPerformed(ActionEvent e
) {
306 addBookPane(type
, true);
313 sources
.addSeparator();
319 JMenu authors
= new JMenu("Authors");
320 authors
.setMnemonic(KeyEvent
.VK_A
);
322 List
<String
> aa
= Instance
.getLibrary().getAuthors();
324 for (final String author
: aa
) {
325 JMenuItem item
= new JMenuItem(author
== null ?
"All"
326 : author
.isEmpty() ?
"[unknown]" : author
);
327 item
.addActionListener(new ActionListener() {
328 public void actionPerformed(ActionEvent e
) {
330 addBookPane(author
, false);
336 if (author
== null || author
.isEmpty()) {
337 authors
.addSeparator();
347 * Create the export menu item.
351 private JMenuItem
createMenuItemExport() {
352 final JFileChooser fc
= new JFileChooser();
353 fc
.setAcceptAllFileFilterUsed(false);
355 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
356 for (OutputType type
: OutputType
.values()) {
357 String ext
= type
.getDefaultExtension(false);
358 String desc
= type
.getDesc(false);
360 if (ext
== null || ext
.isEmpty()) {
361 filters
.put(createAllFilter(desc
), type
);
363 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
367 // First the "ALL" filters, then, the extension filters
368 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
369 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
370 fc
.addChoosableFileFilter(entry
.getKey());
373 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
374 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
375 fc
.addChoosableFileFilter(entry
.getKey());
380 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
381 export
.addActionListener(new ActionListener() {
382 public void actionPerformed(ActionEvent e
) {
383 if (selectedBook
!= null) {
384 fc
.showDialog(LocalReaderFrame
.this, "Save");
385 if (fc
.getSelectedFile() != null) {
386 final OutputType type
= filters
.get(fc
.getFileFilter());
387 final String path
= fc
.getSelectedFile()
389 + type
.getDefaultExtension(false);
390 final Progress pg
= new Progress();
391 outOfUi(pg
, new Runnable() {
394 Instance
.getLibrary().export(
395 selectedBook
.getMeta().getLuid(),
397 } catch (IOException e
) {
411 * Create a {@link FileFilter} that accepts all files and return the given
419 private FileFilter
createAllFilter(final String desc
) {
420 return new FileFilter() {
422 public String
getDescription() {
427 public boolean accept(File f
) {
434 * Create the refresh (delete cache) menu item.
438 private JMenuItem
createMenuItemClearCache() {
439 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
440 refresh
.addActionListener(new ActionListener() {
441 public void actionPerformed(ActionEvent e
) {
442 if (selectedBook
!= null) {
443 outOfUi(null, new Runnable() {
445 reader
.clearLocalReaderCache(selectedBook
.getMeta()
447 selectedBook
.setCached(false);
448 SwingUtilities
.invokeLater(new Runnable() {
450 selectedBook
.repaint();
463 * Create the redownload (then delete original) menu item.
467 private JMenuItem
createMenuItemRedownload() {
468 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
469 refresh
.addActionListener(new ActionListener() {
470 public void actionPerformed(ActionEvent e
) {
471 if (selectedBook
!= null) {
472 final MetaData meta
= selectedBook
.getMeta();
473 imprt(meta
.getUrl(), new Runnable() {
475 reader
.delete(meta
.getLuid());
476 LocalReaderFrame
.this.selectedBook
= null;
478 }, "Removing old copy");
487 * Create the delete menu item.
491 private JMenuItem
createMenuItemDelete() {
492 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
493 delete
.addActionListener(new ActionListener() {
494 public void actionPerformed(ActionEvent e
) {
495 if (selectedBook
!= null) {
496 outOfUi(null, new Runnable() {
498 reader
.delete(selectedBook
.getMeta().getLuid());
510 * Create the open menu item.
514 private JMenuItem
createMenuItemOpenBook() {
515 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
516 open
.addActionListener(new ActionListener() {
517 public void actionPerformed(ActionEvent e
) {
518 if (selectedBook
!= null) {
519 openBook(selectedBook
);
528 * Open a {@link LocalReaderBook} item.
531 * the {@link LocalReaderBook} to open
533 private void openBook(final LocalReaderBook book
) {
534 final Progress pg
= new Progress();
535 outOfUi(pg
, new Runnable() {
538 reader
.open(book
.getMeta().getLuid(), pg
);
539 SwingUtilities
.invokeLater(new Runnable() {
541 book
.setCached(true);
544 } catch (IOException e
) {
545 // TODO: error message?
553 * Process the given action out of the Swing UI thread and link the given
554 * {@link ProgressBar} to the action.
556 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
557 * to done when the action is done.
560 * the {@link ProgressBar} or NULL
564 private void outOfUi(Progress progress
, final Runnable run
) {
565 final Progress pg
= new Progress();
566 final Progress reload
= new Progress("Reload books");
567 if (progress
== null) {
568 progress
= new Progress();
571 pg
.addProgress(progress
, 90);
572 pg
.addProgress(reload
, 10);
575 pgBar
.setProgress(pg
);
579 new Thread(new Runnable() {
583 reload
.setProgress(100);
585 // will trigger pgBar ActionListener:
586 pg
.setProgress(pg
.getMax());
589 }, "outOfUi thread").start();
593 * Import a {@link Story} into the main {@link Library}.
595 * Should be called inside the UI thread.
598 * TRUE for an {@link URL}, false for a {@link File}
600 private void imprt(boolean askUrl
) {
601 JFileChooser fc
= new JFileChooser();
605 String clipboard
= "";
607 clipboard
= ("" + Toolkit
.getDefaultToolkit()
608 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
610 } catch (Exception e
) {
611 // No data will be handled
614 if (clipboard
== null || !clipboard
.startsWith("http")) {
618 url
= JOptionPane
.showInputDialog(LocalReaderFrame
.this,
619 "url of the story to import?", "Importing from URL",
620 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
621 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
622 url
= fc
.getSelectedFile().getAbsolutePath();
627 if (url
!= null && !url
.toString().isEmpty()) {
628 imprt(url
.toString(), null, null);
633 * Actually import the {@link Story} into the main {@link Library}.
635 * Should be called inside the UI thread.
638 * the {@link Story} to import by {@link URL}
640 * Action to execute on success
642 private void imprt(final String url
, final Runnable onSuccess
,
643 String onSuccessPgName
) {
644 final Progress pg
= new Progress();
645 final Progress pgImprt
= new Progress();
646 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
647 pg
.addProgress(pgImprt
, 95);
648 pg
.addProgress(pgOnSuccess
, 5);
650 outOfUi(pg
, new Runnable() {
654 Instance
.getLibrary().imprt(BasicReader
.getUrl(url
),
656 } catch (IOException e
) {
660 final Exception e
= ex
;
662 final boolean ok
= (e
== null);
664 pgOnSuccess
.setProgress(0);
667 SwingUtilities
.invokeLater(new Runnable() {
669 JOptionPane
.showMessageDialog(
670 LocalReaderFrame
.this, "Cannot import: "
671 + url
, e
.getMessage(),
672 JOptionPane
.ERROR_MESSAGE
);
676 if (onSuccess
!= null) {
680 pgOnSuccess
.setProgress(100);
686 * Enables or disables this component, depending on the value of the
687 * parameter <code>b</code>. An enabled component can respond to user input
688 * and generate events. Components are enabled initially by default.
690 * Disabling this component will also affect its children.
693 * If <code>true</code>, this component is enabled; otherwise
694 * this component is disabled
697 public void setEnabled(boolean b
) {
699 for (LocalReaderGroup group
: booksByType
.keySet()) {
702 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {