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
;
66 * Create a new {@link LocalReaderFrame}.
69 * the associated {@link LocalReader} to forward some commands
70 * and access its {@link Library}
72 * the type of {@link Story} to load, or NULL for all types
74 public LocalReaderFrame(LocalReader reader
, String type
) {
75 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
79 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
81 setLayout(new BorderLayout());
84 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
86 color
= Instance
.getUiConfig().getColor(UiConfig
.BACKGROUND_COLOR
);
89 pane
.setBackground(color
);
92 JScrollPane scroll
= new JScrollPane(pane
);
93 scroll
.getVerticalScrollBar().setUnitIncrement(16);
94 add(scroll
, BorderLayout
.CENTER
);
96 pgBar
= new ProgressBar();
97 add(pgBar
, BorderLayout
.SOUTH
);
99 setJMenuBar(createMenu());
101 booksByType
= new HashMap
<LocalReaderGroup
, String
>();
102 booksByAuthor
= new HashMap
<LocalReaderGroup
, String
>();
104 addBookPane(type
, true);
111 * Add a new {@link LocalReaderGroup} on the frame to display the books of
112 * the selected type or author.
115 * the author or the type
117 * TRUE for type, FALSE for author
119 private void addBookPane(String value
, boolean type
) {
122 for (String tt
: Instance
.getLibrary().getTypes()) {
124 addBookPane(tt
, type
);
128 for (String tt
: Instance
.getLibrary().getAuthors()) {
130 addBookPane(tt
, type
);
138 LocalReaderGroup bookPane
= new LocalReaderGroup(reader
, value
, color
);
140 booksByType
.put(bookPane
, value
);
142 booksByAuthor
.put(bookPane
, value
);
151 bookPane
.setActionListener(new BookActionListener() {
152 public void select(LocalReaderBook book
) {
156 public void popupRequested(LocalReaderBook book
, MouseEvent e
) {
157 JPopupMenu popup
= new JPopupMenu();
158 popup
.add(createMenuItemOpenBook());
159 popup
.addSeparator();
160 popup
.add(createMenuItemExport());
161 popup
.add(createMenuItemClearCache());
162 popup
.add(createMenuItemRedownload());
163 popup
.addSeparator();
164 popup
.add(createMenuItemDelete());
165 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
168 public void action(final LocalReaderBook book
) {
174 private void removeBookPanes() {
176 booksByAuthor
.clear();
185 * Refresh the list of {@link LocalReaderBook}s from disk.
188 * the type of {@link Story} to load, or NULL for all types
190 private void refreshBooks() {
191 for (LocalReaderGroup group
: booksByType
.keySet()) {
192 List
<MetaData
> stories
= Instance
.getLibrary().getListByType(
193 booksByType
.get(group
));
194 group
.refreshBooks(stories
);
197 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {
198 List
<MetaData
> stories
= Instance
.getLibrary().getListByAuthor(
199 booksByAuthor
.get(group
));
200 group
.refreshBooks(stories
);
208 * Create the main menu bar.
212 private JMenuBar
createMenu() {
213 bar
= new JMenuBar();
215 JMenu file
= new JMenu("File");
216 file
.setMnemonic(KeyEvent
.VK_F
);
218 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
219 imprt
.addActionListener(new ActionListener() {
220 public void actionPerformed(ActionEvent e
) {
224 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
225 imprtF
.addActionListener(new ActionListener() {
226 public void actionPerformed(ActionEvent e
) {
230 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
231 exit
.addActionListener(new ActionListener() {
232 public void actionPerformed(ActionEvent e
) {
233 LocalReaderFrame
.this.dispatchEvent(new WindowEvent(
234 LocalReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
238 file
.add(createMenuItemOpenBook());
239 file
.add(createMenuItemExport());
248 JMenu edit
= new JMenu("Edit");
249 edit
.setMnemonic(KeyEvent
.VK_E
);
251 edit
.add(createMenuItemClearCache());
252 edit
.add(createMenuItemRedownload());
254 edit
.add(createMenuItemDelete());
258 JMenu sources
= new JMenu("Sources");
259 sources
.setMnemonic(KeyEvent
.VK_S
);
261 List
<String
> tt
= Instance
.getLibrary().getTypes();
263 for (final String type
: tt
) {
264 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
265 item
.addActionListener(new ActionListener() {
266 public void actionPerformed(ActionEvent e
) {
268 addBookPane(type
, true);
275 sources
.addSeparator();
281 JMenu authors
= new JMenu("Authors");
282 authors
.setMnemonic(KeyEvent
.VK_A
);
284 List
<String
> aa
= Instance
.getLibrary().getAuthors();
286 for (final String author
: aa
) {
287 JMenuItem item
= new JMenuItem(author
== null ?
"All"
288 : author
.isEmpty() ?
"[unknown]" : author
);
289 item
.addActionListener(new ActionListener() {
290 public void actionPerformed(ActionEvent e
) {
292 addBookPane(author
, false);
298 if (author
== null || author
.isEmpty()) {
299 authors
.addSeparator();
309 * Create the export menu item.
313 private JMenuItem
createMenuItemExport() {
314 final JFileChooser fc
= new JFileChooser();
315 fc
.setAcceptAllFileFilterUsed(false);
317 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
318 for (OutputType type
: OutputType
.values()) {
319 String ext
= type
.getDefaultExtension(false);
320 String desc
= type
.getDesc(false);
321 if (ext
== null || ext
.isEmpty()) {
322 filters
.put(createAllFilter(desc
), type
);
324 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
328 // First the "ALL" filters, then, the extension filters
329 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
330 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
331 fc
.addChoosableFileFilter(entry
.getKey());
334 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
335 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
336 fc
.addChoosableFileFilter(entry
.getKey());
341 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
342 export
.addActionListener(new ActionListener() {
343 public void actionPerformed(ActionEvent e
) {
344 if (selectedBook
!= null) {
345 fc
.showDialog(LocalReaderFrame
.this, "Save");
346 final OutputType type
= filters
.get(fc
.getFileFilter());
347 final String path
= fc
.getSelectedFile().getAbsolutePath()
348 + type
.getDefaultExtension(false);
349 final Progress pg
= new Progress();
350 outOfUi(pg
, new Runnable() {
353 Instance
.getLibrary().export(
354 selectedBook
.getMeta().getLuid(), type
,
356 } catch (IOException e
) {
369 * Create a {@link FileFilter} that accepts all files and return the given
377 private FileFilter
createAllFilter(final String desc
) {
378 return new FileFilter() {
380 public String
getDescription() {
385 public boolean accept(File f
) {
392 * Create the refresh (delete cache) menu item.
396 private JMenuItem
createMenuItemClearCache() {
397 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
398 refresh
.addActionListener(new ActionListener() {
399 public void actionPerformed(ActionEvent e
) {
400 if (selectedBook
!= null) {
401 outOfUi(null, new Runnable() {
403 reader
.refresh(selectedBook
.getMeta().getLuid());
404 selectedBook
.setCached(false);
405 SwingUtilities
.invokeLater(new Runnable() {
407 selectedBook
.repaint();
420 * Create the redownload (then delete original) menu item.
424 private JMenuItem
createMenuItemRedownload() {
425 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
426 refresh
.addActionListener(new ActionListener() {
427 public void actionPerformed(ActionEvent e
) {
428 if (selectedBook
!= null) {
429 imprt(selectedBook
.getMeta().getUrl(), new Runnable() {
431 reader
.delete(selectedBook
.getMeta().getLuid());
443 * Create the delete menu item.
447 private JMenuItem
createMenuItemDelete() {
448 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
449 delete
.addActionListener(new ActionListener() {
450 public void actionPerformed(ActionEvent e
) {
451 if (selectedBook
!= null) {
452 outOfUi(null, new Runnable() {
454 reader
.delete(selectedBook
.getMeta().getLuid());
456 SwingUtilities
.invokeLater(new Runnable() {
471 * Create the open menu item.
475 private JMenuItem
createMenuItemOpenBook() {
476 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
477 open
.addActionListener(new ActionListener() {
478 public void actionPerformed(ActionEvent e
) {
479 if (selectedBook
!= null) {
480 openBook(selectedBook
);
489 * Open a {@link LocalReaderBook} item.
492 * the {@link LocalReaderBook} to open
494 private void openBook(final LocalReaderBook book
) {
495 final Progress pg
= new Progress();
496 outOfUi(pg
, new Runnable() {
499 reader
.open(book
.getMeta().getLuid(), pg
);
500 SwingUtilities
.invokeLater(new Runnable() {
502 book
.setCached(true);
505 } catch (IOException e
) {
506 // TODO: error message?
514 * Process the given action out of the Swing UI thread and link the given
515 * {@link ProgressBar} to the action.
517 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
518 * to done when the action is done.
521 * the {@link ProgressBar} or NULL
525 private void outOfUi(final Progress pg
, final Runnable run
) {
526 pgBar
.setProgress(pg
);
529 pgBar
.addActioListener(new ActionListener() {
530 public void actionPerformed(ActionEvent e
) {
531 pgBar
.setProgress(null);
536 new Thread(new Runnable() {
540 SwingUtilities
.invokeLater(new Runnable() {
545 } else if (!pg
.isDone()) {
546 pg
.setProgress(pg
.getMax());
553 * Import a {@link Story} into the main {@link Library}.
555 * Should be called inside the UI thread.
558 * TRUE for an {@link URL}, false for a {@link File}
560 private void imprt(boolean askUrl
) {
561 JFileChooser fc
= new JFileChooser();
565 String clipboard
= "";
567 clipboard
= ("" + Toolkit
.getDefaultToolkit()
568 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
570 } catch (Exception e
) {
571 // No data will be handled
574 if (clipboard
== null || !clipboard
.startsWith("http")) {
578 url
= JOptionPane
.showInputDialog(LocalReaderFrame
.this,
579 "url of the story to import?", "Importing from URL",
580 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
581 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
582 url
= fc
.getSelectedFile().getAbsolutePath();
587 if (url
!= null && !url
.toString().isEmpty()) {
588 imprt(url
.toString(), null);
593 * Actually import the {@link Story} into the main {@link Library}.
595 * Should be called inside the UI thread.
598 * the {@link Story} to import by {@link URL}
600 * Action to execute on success
602 private void imprt(final String url
, final Runnable onSuccess
) {
603 final Progress pg
= new Progress("Importing " + url
);
604 outOfUi(pg
, new Runnable() {
608 Instance
.getLibrary().imprt(BasicReader
.getUrl(url
), pg
);
609 } catch (IOException e
) {
613 final Exception e
= ex
;
615 final boolean ok
= (e
== null);
616 SwingUtilities
.invokeLater(new Runnable() {
620 JOptionPane
.showMessageDialog(
621 LocalReaderFrame
.this, "Cannot import: "
622 + url
, e
.getMessage(),
623 JOptionPane
.ERROR_MESSAGE
);
628 if (onSuccess
!= null) {
640 * Enables or disables this component, depending on the value of the
641 * parameter <code>b</code>. An enabled component can respond to user input
642 * and generate events. Components are enabled initially by default.
644 * Disabling this component will also affect its children.
647 * If <code>true</code>, this component is enabled; otherwise
648 * this component is disabled
651 public void setEnabled(boolean b
) {
653 for (LocalReaderGroup group
: booksByType
.keySet()) {
656 for (LocalReaderGroup group
: booksByAuthor
.keySet()) {