1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.BorderLayout
;
6 import java
.awt
.event
.ActionEvent
;
7 import java
.awt
.event
.ActionListener
;
8 import java
.awt
.event
.KeyEvent
;
9 import java
.awt
.event
.MouseEvent
;
10 import java
.awt
.event
.WindowEvent
;
12 import java
.io
.IOException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.util
.List
;
18 import java
.util
.Map
.Entry
;
20 import javax
.swing
.JFileChooser
;
21 import javax
.swing
.JFrame
;
22 import javax
.swing
.JMenu
;
23 import javax
.swing
.JMenuBar
;
24 import javax
.swing
.JMenuItem
;
25 import javax
.swing
.JOptionPane
;
26 import javax
.swing
.JPanel
;
27 import javax
.swing
.JPopupMenu
;
28 import javax
.swing
.JScrollPane
;
29 import javax
.swing
.SwingUtilities
;
30 import javax
.swing
.filechooser
.FileFilter
;
31 import javax
.swing
.filechooser
.FileNameExtensionFilter
;
33 import be
.nikiroo
.fanfix
.Instance
;
34 import be
.nikiroo
.fanfix
.Library
;
35 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
36 import be
.nikiroo
.fanfix
.data
.MetaData
;
37 import be
.nikiroo
.fanfix
.data
.Story
;
38 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
39 import be
.nikiroo
.fanfix
.reader
.LocalReaderBook
.BookActionListener
;
40 import be
.nikiroo
.utils
.Progress
;
41 import be
.nikiroo
.utils
.Version
;
42 import be
.nikiroo
.utils
.ui
.ProgressBar
;
43 import be
.nikiroo
.utils
.ui
.WrapLayout
;
46 * A {@link Frame} that will show a {@link LocalReaderBook} item for each
47 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
48 * way to copy them to the {@link LocalReader} cache ({@link LocalReader#lib}),
49 * read them, delete them...
53 class LocalReaderFrame
extends JFrame
{
54 private static final long serialVersionUID
= 1L;
55 private LocalReader reader
;
56 private List
<MetaData
> stories
;
57 private List
<LocalReaderBook
> books
;
58 private JPanel bookPane
;
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());
83 books
= new ArrayList
<LocalReaderBook
>();
84 bookPane
= new JPanel(new WrapLayout(WrapLayout
.LEADING
, 5, 5));
86 color
= Instance
.getUiConfig().getColor(UiConfig
.BACKGROUND_COLOR
);
90 bookPane
.setBackground(color
);
93 JScrollPane scroll
= new JScrollPane(bookPane
);
94 scroll
.getVerticalScrollBar().setUnitIncrement(16);
95 add(scroll
, BorderLayout
.CENTER
);
97 pgBar
= new ProgressBar();
98 add(pgBar
, BorderLayout
.SOUTH
);
101 setJMenuBar(createMenu());
107 * Refresh the list of {@link LocalReaderBook}s from disk.
110 * the type of {@link Story} to load, or NULL for all types
112 private void refreshBooks(String type
) {
114 stories
= Instance
.getLibrary().getList(type
);
116 bookPane
.invalidate();
117 bookPane
.removeAll();
118 for (MetaData meta
: stories
) {
119 LocalReaderBook book
= new LocalReaderBook(meta
,
120 reader
.isCached(meta
.getLuid()));
122 book
.setBackground(color
);
127 book
.addActionListener(new BookActionListener() {
128 public void select(LocalReaderBook book
) {
130 for (LocalReaderBook abook
: books
) {
131 abook
.setSelected(abook
== book
);
135 public void popupRequested(LocalReaderBook book
, MouseEvent e
) {
136 JPopupMenu popup
= new JPopupMenu();
137 popup
.add(createMenuItemOpenBook());
138 popup
.addSeparator();
139 popup
.add(createMenuItemExport());
140 popup
.add(createMenuItemClearCache());
141 popup
.add(createMenuItemRedownload());
142 popup
.addSeparator();
143 popup
.add(createMenuItemDelete());
144 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
147 public void action(final LocalReaderBook book
) {
160 * Create the main menu bar.
164 private JMenuBar
createMenu() {
165 bar
= new JMenuBar();
167 JMenu file
= new JMenu("File");
168 file
.setMnemonic(KeyEvent
.VK_F
);
170 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
171 imprt
.addActionListener(new ActionListener() {
172 public void actionPerformed(ActionEvent e
) {
176 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
177 imprtF
.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e
) {
182 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
183 exit
.addActionListener(new ActionListener() {
184 public void actionPerformed(ActionEvent e
) {
185 LocalReaderFrame
.this.dispatchEvent(new WindowEvent(
186 LocalReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
190 file
.add(createMenuItemOpenBook());
191 file
.add(createMenuItemExport());
200 JMenu edit
= new JMenu("Edit");
201 edit
.setMnemonic(KeyEvent
.VK_E
);
203 edit
.add(createMenuItemClearCache());
204 edit
.add(createMenuItemRedownload());
206 edit
.add(createMenuItemDelete());
210 JMenu view
= new JMenu("View");
211 view
.setMnemonic(KeyEvent
.VK_V
);
213 List
<String
> tt
= Instance
.getLibrary().getTypes();
215 for (final String type
: tt
) {
216 JMenuItem item
= new JMenuItem(type
== null ?
"All books" : type
);
217 item
.addActionListener(new ActionListener() {
218 public void actionPerformed(ActionEvent e
) {
235 * Create the export menu item.
239 private JMenuItem
createMenuItemExport() {
240 final JFileChooser fc
= new JFileChooser();
241 fc
.setAcceptAllFileFilterUsed(false);
243 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
244 for (OutputType type
: OutputType
.values()) {
245 String ext
= type
.getDefaultExtension(false);
246 String desc
= type
.getDesc(false);
247 if (ext
== null || ext
.isEmpty()) {
248 filters
.put(createAllFilter(desc
), type
);
250 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
254 // First the "ALL" filters, then, the extension filters
255 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
256 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
257 fc
.addChoosableFileFilter(entry
.getKey());
260 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
261 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
262 fc
.addChoosableFileFilter(entry
.getKey());
267 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
268 export
.addActionListener(new ActionListener() {
269 public void actionPerformed(ActionEvent e
) {
270 if (selectedBook
!= null) {
271 fc
.showDialog(LocalReaderFrame
.this, "Save");
272 final OutputType type
= filters
.get(fc
.getFileFilter());
273 final String path
= fc
.getSelectedFile().getAbsolutePath()
274 + type
.getDefaultExtension(false);
275 final Progress pg
= new Progress();
276 outOfUi(pg
, new Runnable() {
279 Instance
.getLibrary().export(
280 selectedBook
.getMeta().getLuid(), type
,
282 } catch (IOException e
) {
295 * Create a {@link FileFilter} that accepts all files and return the given
303 private FileFilter
createAllFilter(final String desc
) {
304 return new FileFilter() {
306 public String
getDescription() {
311 public boolean accept(File f
) {
318 * Create the refresh (delete cache) menu item.
322 private JMenuItem
createMenuItemClearCache() {
323 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
324 refresh
.addActionListener(new ActionListener() {
325 public void actionPerformed(ActionEvent e
) {
326 if (selectedBook
!= null) {
327 outOfUi(null, new Runnable() {
329 reader
.refresh(selectedBook
.getMeta().getLuid());
330 selectedBook
.setCached(false);
331 SwingUtilities
.invokeLater(new Runnable() {
333 selectedBook
.repaint();
346 * Create the redownload (then delete original) menu item.
350 private JMenuItem
createMenuItemRedownload() {
351 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
352 refresh
.addActionListener(new ActionListener() {
353 public void actionPerformed(ActionEvent e
) {
354 if (selectedBook
!= null) {
355 imprt(selectedBook
.getMeta().getUrl(), new Runnable() {
357 reader
.delete(selectedBook
.getMeta().getLuid());
369 * Create the delete menu item.
373 private JMenuItem
createMenuItemDelete() {
374 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
375 delete
.addActionListener(new ActionListener() {
376 public void actionPerformed(ActionEvent e
) {
377 if (selectedBook
!= null) {
378 outOfUi(null, new Runnable() {
380 reader
.delete(selectedBook
.getMeta().getLuid());
382 SwingUtilities
.invokeLater(new Runnable() {
397 * Create the open menu item.
401 private JMenuItem
createMenuItemOpenBook() {
402 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
403 open
.addActionListener(new ActionListener() {
404 public void actionPerformed(ActionEvent e
) {
405 if (selectedBook
!= null) {
406 openBook(selectedBook
);
415 * Open a {@link LocalReaderBook} item.
418 * the {@link LocalReaderBook} to open
420 private void openBook(final LocalReaderBook book
) {
421 final Progress pg
= new Progress();
422 outOfUi(pg
, new Runnable() {
425 reader
.open(book
.getMeta().getLuid(), pg
);
426 SwingUtilities
.invokeLater(new Runnable() {
428 book
.setCached(true);
431 } catch (IOException e
) {
432 // TODO: error message?
440 * Process the given action out of the Swing UI thread and link the given
441 * {@link ProgressBar} to the action.
443 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
444 * to done when the action is done.
447 * the {@link ProgressBar} or NULL
451 private void outOfUi(final Progress pg
, final Runnable run
) {
452 pgBar
.setProgress(pg
);
455 pgBar
.addActioListener(new ActionListener() {
456 public void actionPerformed(ActionEvent e
) {
457 pgBar
.setProgress(null);
462 new Thread(new Runnable() {
466 SwingUtilities
.invokeLater(new Runnable() {
471 } else if (!pg
.isDone()) {
472 pg
.setProgress(pg
.getMax());
479 * Import a {@link Story} into the main {@link Library}.
481 * Should be called inside the UI thread.
484 * TRUE for an {@link URL}, false for a {@link File}
486 private void imprt(boolean askUrl
) {
487 JFileChooser fc
= new JFileChooser();
491 url
= JOptionPane
.showInputDialog(LocalReaderFrame
.this,
492 "url of the story to import?", "Importing from URL",
493 JOptionPane
.QUESTION_MESSAGE
);
494 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
495 url
= fc
.getSelectedFile().getAbsolutePath();
500 if (url
!= null && !url
.isEmpty()) {
506 * Actually import the {@link Story} into the main {@link Library}.
508 * Should be called inside the UI thread.
511 * the {@link Story} to import by {@link URL}
513 * Action to execute on success
515 private void imprt(final String url
, final Runnable onSuccess
) {
516 final Progress pg
= new Progress("Importing " + url
);
517 outOfUi(pg
, new Runnable() {
521 Instance
.getLibrary().imprt(BasicReader
.getUrl(url
), pg
);
522 } catch (IOException e
) {
526 final Exception e
= ex
;
528 final boolean ok
= (e
== null);
529 SwingUtilities
.invokeLater(new Runnable() {
533 JOptionPane
.showMessageDialog(
534 LocalReaderFrame
.this, "Cannot import: "
535 + url
, e
.getMessage(),
536 JOptionPane
.ERROR_MESSAGE
);
541 if (onSuccess
!= null) {
553 * Enables or disables this component, depending on the value of the
554 * parameter <code>b</code>. An enabled component can respond to user input
555 * and generate events. Components are enabled initially by default.
557 * Disabling this component will also affect its children.
560 * If <code>true</code>, this component is enabled; otherwise
561 * this component is disabled
564 public void setEnabled(boolean b
) {
565 for (LocalReaderBook book
: books
) {
571 bookPane
.setEnabled(b
);