1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
7 import java
.awt
.Toolkit
;
8 import java
.awt
.datatransfer
.DataFlavor
;
9 import java
.awt
.event
.ActionEvent
;
10 import java
.awt
.event
.ActionListener
;
11 import java
.awt
.event
.KeyEvent
;
12 import java
.awt
.event
.MouseEvent
;
13 import java
.awt
.event
.WindowEvent
;
15 import java
.io
.IOException
;
17 import java
.net
.UnknownHostException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.Map
.Entry
;
25 import javax
.swing
.BorderFactory
;
26 import javax
.swing
.BoxLayout
;
27 import javax
.swing
.ImageIcon
;
28 import javax
.swing
.JFileChooser
;
29 import javax
.swing
.JFrame
;
30 import javax
.swing
.JLabel
;
31 import javax
.swing
.JMenu
;
32 import javax
.swing
.JMenuBar
;
33 import javax
.swing
.JMenuItem
;
34 import javax
.swing
.JOptionPane
;
35 import javax
.swing
.JPanel
;
36 import javax
.swing
.JPopupMenu
;
37 import javax
.swing
.JScrollPane
;
38 import javax
.swing
.JTextArea
;
39 import javax
.swing
.SwingConstants
;
40 import javax
.swing
.SwingUtilities
;
41 import javax
.swing
.filechooser
.FileFilter
;
42 import javax
.swing
.filechooser
.FileNameExtensionFilter
;
44 import be
.nikiroo
.fanfix
.Instance
;
45 import be
.nikiroo
.fanfix
.bundles
.Config
;
46 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
47 import be
.nikiroo
.fanfix
.data
.MetaData
;
48 import be
.nikiroo
.fanfix
.data
.Story
;
49 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
50 import be
.nikiroo
.fanfix
.library
.BasicLibrary
.Status
;
51 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
52 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
53 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
54 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
55 import be
.nikiroo
.utils
.Progress
;
56 import be
.nikiroo
.utils
.Version
;
57 import be
.nikiroo
.utils
.ui
.ConfigEditor
;
58 import be
.nikiroo
.utils
.ui
.ProgressBar
;
61 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
62 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
63 * way to copy them to the {@link GuiReader} cache (
64 * {@link BasicReader#getLibrary()}), read them, delete them...
68 class GuiReaderFrame
extends JFrame
{
69 private static final long serialVersionUID
= 1L;
70 private GuiReader reader
;
71 private Map
<GuiReaderGroup
, String
> booksByType
;
72 private Map
<GuiReaderGroup
, String
> booksByAuthor
;
75 private ProgressBar pgBar
;
77 private GuiReaderBook selectedBook
;
78 private boolean words
; // words or authors (secondary info on books)
81 * A {@link Runnable} with a {@link Story} parameter.
85 private interface StoryRunnable
{
92 public void run(Story story
);
96 * Create a new {@link GuiReaderFrame}.
99 * the associated {@link GuiReader} to forward some commands and
100 * access its {@link LocalLibrary}
102 * the type of {@link Story} to load, or NULL for all types
104 public GuiReaderFrame(GuiReader reader
, String type
) {
105 super(String
.format("Fanfix %s Library", Version
.getCurrentVersion()));
107 this.reader
= reader
;
109 setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
111 setLayout(new BorderLayout());
114 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
116 Integer icolor
= Instance
.getUiConfig().getColor(
117 UiConfig
.BACKGROUND_COLOR
);
118 if (icolor
!= null) {
119 color
= new Color(icolor
);
120 setBackground(color
);
121 pane
.setBackground(color
);
124 JScrollPane scroll
= new JScrollPane(pane
);
125 scroll
.getVerticalScrollBar().setUnitIncrement(16);
126 add(scroll
, BorderLayout
.CENTER
);
128 String message
= reader
.getLibrary().getLibraryName();
129 if (!message
.isEmpty()) {
130 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
131 add(name
, BorderLayout
.NORTH
);
134 pgBar
= new ProgressBar();
135 add(pgBar
, BorderLayout
.SOUTH
);
137 pgBar
.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e
) {
141 pgBar
.setProgress(null);
147 pgBar
.addUpdateListener(new ActionListener() {
149 public void actionPerformed(ActionEvent e
) {
156 booksByType
= new HashMap
<GuiReaderGroup
, String
>();
157 booksByAuthor
= new HashMap
<GuiReaderGroup
, String
>();
159 pane
.setVisible(false);
160 final Progress pg
= new Progress();
161 final String typeF
= type
;
162 outOfUi(pg
, new Runnable() {
165 BasicLibrary lib
= GuiReaderFrame
.this.reader
.getLibrary();
166 Status status
= lib
.getStatus();
168 if (status
== Status
.READY
) {
171 setJMenuBar(createMenu(true));
172 addBookPane(typeF
, true);
175 pane
.setVisible(true);
178 setJMenuBar(createMenu(false));
181 String err
= lib
.getLibraryName() + "\n";
184 err
+= "Library not valid";
188 err
+= "You are not allowed to access this library";
192 err
+= "Library currently unavailable";
196 err
+= "An error occured when contacting the library";
200 error(err
, "Library error", null);
208 private void addSourcePanes() {
210 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, "Sources", color
);
212 List
<MetaData
> sources
= new ArrayList
<MetaData
>();
213 for (String source
: reader
.getLibrary().getSources()) {
214 MetaData mSource
= new MetaData();
215 mSource
.setLuid(null);
216 mSource
.setTitle(source
);
217 mSource
.setSource(source
);
218 sources
.add(mSource
);
221 bookPane
.refreshBooks(sources
, false);
229 bookPane
.setActionListener(new BookActionListener() {
231 public void select(GuiReaderBook book
) {
236 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
237 JPopupMenu popup
= new JPopupMenu();
238 popup
.add(createMenuItemOpenBook());
239 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
243 public void action(final GuiReaderBook book
) {
245 addBookPane(book
.getMeta().getSource(), true);
252 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
253 * selected type or author.
256 * the author or the type, or NULL to get all the
259 * TRUE for type, FALSE for author
261 private void addBookPane(String value
, boolean type
) {
264 if (Instance
.getUiConfig().getBoolean(UiConfig
.SOURCE_PAGE
,
268 for (String tt
: reader
.getLibrary().getSources()) {
270 addBookPane(tt
, type
);
275 for (String tt
: reader
.getLibrary().getAuthors()) {
277 addBookPane(tt
, type
);
285 GuiReaderGroup bookPane
= new GuiReaderGroup(reader
, value
, color
);
287 booksByType
.put(bookPane
, value
);
289 booksByAuthor
.put(bookPane
, value
);
298 bookPane
.setActionListener(new BookActionListener() {
300 public void select(GuiReaderBook book
) {
305 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
306 JPopupMenu popup
= new JPopupMenu();
307 popup
.add(createMenuItemOpenBook());
308 popup
.addSeparator();
309 popup
.add(createMenuItemExport());
310 popup
.add(createMenuItemMove(true));
311 popup
.add(createMenuItemSetCover());
312 popup
.add(createMenuItemClearCache());
313 popup
.add(createMenuItemRedownload());
314 popup
.addSeparator();
315 popup
.add(createMenuItemDelete());
316 popup
.addSeparator();
317 popup
.add(createMenuItemProperties());
318 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
322 public void action(final GuiReaderBook book
) {
328 private void removeBookPanes() {
330 booksByAuthor
.clear();
339 * Refresh the list of {@link GuiReaderBook}s from disk.
341 private void refreshBooks() {
342 for (GuiReaderGroup group
: booksByType
.keySet()) {
343 List
<MetaData
> stories
= reader
.getLibrary().getListBySource(
344 booksByType
.get(group
));
345 group
.refreshBooks(stories
, words
);
348 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
349 List
<MetaData
> stories
= reader
.getLibrary().getListByAuthor(
350 booksByAuthor
.get(group
));
351 group
.refreshBooks(stories
, words
);
359 * Create the main menu bar.
362 * the library can be queried
366 private JMenuBar
createMenu(boolean libOk
) {
367 bar
= new JMenuBar();
369 JMenu file
= new JMenu("File");
370 file
.setMnemonic(KeyEvent
.VK_F
);
372 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
373 imprt
.addActionListener(new ActionListener() {
375 public void actionPerformed(ActionEvent e
) {
379 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
380 imprtF
.addActionListener(new ActionListener() {
382 public void actionPerformed(ActionEvent e
) {
386 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
387 exit
.addActionListener(new ActionListener() {
389 public void actionPerformed(ActionEvent e
) {
390 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
391 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
395 file
.add(createMenuItemOpenBook());
396 file
.add(createMenuItemExport());
397 file
.add(createMenuItemMove(libOk
));
406 JMenu edit
= new JMenu("Edit");
407 edit
.setMnemonic(KeyEvent
.VK_E
);
409 edit
.add(createMenuItemClearCache());
410 edit
.add(createMenuItemRedownload());
412 edit
.add(createMenuItemDelete());
416 JMenu view
= new JMenu("View");
417 view
.setMnemonic(KeyEvent
.VK_V
);
418 JMenuItem vauthors
= new JMenuItem("Author");
419 vauthors
.setMnemonic(KeyEvent
.VK_A
);
420 vauthors
.addActionListener(new ActionListener() {
422 public void actionPerformed(ActionEvent e
) {
428 JMenuItem vwords
= new JMenuItem("Word count");
429 vwords
.setMnemonic(KeyEvent
.VK_W
);
430 vwords
.addActionListener(new ActionListener() {
432 public void actionPerformed(ActionEvent e
) {
440 JMenu sources
= new JMenu("Sources");
441 sources
.setMnemonic(KeyEvent
.VK_S
);
443 List
<String
> tt
= new ArrayList
<String
>();
445 tt
.addAll(reader
.getLibrary().getSources());
449 for (final String type
: tt
) {
450 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
451 item
.addActionListener(new ActionListener() {
453 public void actionPerformed(ActionEvent e
) {
455 addBookPane(type
, true);
462 sources
.addSeparator();
468 JMenu authors
= new JMenu("Authors");
469 authors
.setMnemonic(KeyEvent
.VK_A
);
471 List
<Entry
<String
, List
<String
>>> authorGroups
= reader
.getLibrary()
472 .getAuthorsGrouped();
473 if (authorGroups
.size() > 1) {
476 // null -> "All" authors special item
477 populateMenuAuthorList(authors
, Arrays
.asList((String
) null));
479 for (Entry
<String
, List
<String
>> group
: authorGroups
) {
480 JMenu thisGroup
= new JMenu(group
.getKey());
481 populateMenuAuthorList(thisGroup
, group
.getValue());
482 authors
.add(thisGroup
);
487 // null -> "All" authors special item
488 List
<String
> authorNames
= new ArrayList
<String
>();
489 authorNames
.add(null);
490 if (authorGroups
.size() > 0) {
491 authorNames
.addAll(authorGroups
.get(0).getValue());
493 populateMenuAuthorList(authors
, authorNames
);
498 JMenu options
= new JMenu("Options");
499 options
.setMnemonic(KeyEvent
.VK_O
);
500 options
.add(createMenuItemConfig());
501 options
.add(createMenuItemUiConfig());
508 * Populate a list of authors as {@link JMenuItem}s into the given
511 * Each item will select the author when clicked.
514 * the parent {@link JMenuItem}
518 private void populateMenuAuthorList(JMenu authors
, List
<String
> names
) {
519 for (final String name
: names
) {
520 JMenuItem item
= new JMenuItem(name
== null ?
"All"
521 : name
.isEmpty() ?
"[unknown]" : name
);
522 item
.addActionListener(new ActionListener() {
524 public void actionPerformed(ActionEvent e
) {
526 addBookPane(name
, false);
532 if (name
== null || name
.isEmpty()) {
533 authors
.addSeparator();
539 * Create the Fanfix Configuration menu item.
543 private JMenuItem
createMenuItemConfig() {
544 final String title
= "Fanfix Configuration";
545 JMenuItem item
= new JMenuItem(title
);
546 item
.setMnemonic(KeyEvent
.VK_F
);
548 item
.addActionListener(new ActionListener() {
550 public void actionPerformed(ActionEvent e
) {
551 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
552 Config
.class, Instance
.getConfig(),
553 "This is where you configure the options of the program.");
554 JFrame frame
= new JFrame(title
);
556 frame
.setSize(800, 600);
557 frame
.setVisible(true);
565 * Create the UI Configuration menu item.
569 private JMenuItem
createMenuItemUiConfig() {
570 final String title
= "UI Configuration";
571 JMenuItem item
= new JMenuItem(title
);
572 item
.setMnemonic(KeyEvent
.VK_U
);
574 item
.addActionListener(new ActionListener() {
576 public void actionPerformed(ActionEvent e
) {
577 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
578 UiConfig
.class, Instance
.getUiConfig(),
579 "This is where you configure the graphical appearence of the program.");
580 JFrame frame
= new JFrame(title
);
582 frame
.setSize(800, 600);
583 frame
.setVisible(true);
591 * Create the export menu item.
595 private JMenuItem
createMenuItemExport() {
596 final JFileChooser fc
= new JFileChooser();
597 fc
.setAcceptAllFileFilterUsed(false);
599 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
600 for (OutputType type
: OutputType
.values()) {
601 String ext
= type
.getDefaultExtension(false);
602 String desc
= type
.getDesc(false);
604 if (ext
== null || ext
.isEmpty()) {
605 filters
.put(createAllFilter(desc
), type
);
607 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
611 // First the "ALL" filters, then, the extension filters
612 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
613 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
614 fc
.addChoosableFileFilter(entry
.getKey());
617 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
618 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
619 fc
.addChoosableFileFilter(entry
.getKey());
624 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
625 export
.addActionListener(new ActionListener() {
627 public void actionPerformed(ActionEvent e
) {
628 if (selectedBook
!= null) {
629 fc
.showDialog(GuiReaderFrame
.this, "Save");
630 if (fc
.getSelectedFile() != null) {
631 final OutputType type
= filters
.get(fc
.getFileFilter());
632 final String path
= fc
.getSelectedFile()
634 + type
.getDefaultExtension(false);
635 final Progress pg
= new Progress();
636 outOfUi(pg
, new Runnable() {
640 reader
.getLibrary().export(
641 selectedBook
.getMeta().getLuid(),
643 } catch (IOException e
) {
644 Instance
.getTraceHandler().error(e
);
657 * Create a {@link FileFilter} that accepts all files and return the given
665 private FileFilter
createAllFilter(final String desc
) {
666 return new FileFilter() {
668 public String
getDescription() {
673 public boolean accept(File f
) {
680 * Create the refresh (delete cache) menu item.
684 private JMenuItem
createMenuItemClearCache() {
685 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
686 refresh
.addActionListener(new ActionListener() {
688 public void actionPerformed(ActionEvent e
) {
689 if (selectedBook
!= null) {
690 outOfUi(null, new Runnable() {
693 reader
.clearLocalReaderCache(selectedBook
.getMeta()
695 selectedBook
.setCached(false);
696 GuiReaderCoverImager
.clearIcon(selectedBook
698 SwingUtilities
.invokeLater(new Runnable() {
701 selectedBook
.repaint();
714 * Create the delete menu item.
717 * the library can be queried
721 private JMenuItem
createMenuItemMove(boolean libOk
) {
722 JMenu moveTo
= new JMenu("Move to...");
723 moveTo
.setMnemonic(KeyEvent
.VK_M
);
725 List
<String
> types
= new ArrayList
<String
>();
728 types
.addAll(reader
.getLibrary().getSources());
731 for (String type
: types
) {
732 JMenuItem item
= new JMenuItem(type
== null ?
"New type..." : type
);
736 moveTo
.addSeparator();
739 final String ftype
= type
;
740 item
.addActionListener(new ActionListener() {
742 public void actionPerformed(ActionEvent e
) {
743 if (selectedBook
!= null) {
746 Object rep
= JOptionPane
.showInputDialog(
747 GuiReaderFrame
.this, "Move to:",
749 JOptionPane
.QUESTION_MESSAGE
, null, null,
750 selectedBook
.getMeta().getSource());
756 type
= rep
.toString();
759 final String ftype
= type
;
760 outOfUi(null, new Runnable() {
763 reader
.changeSource(selectedBook
.getMeta()
768 SwingUtilities
.invokeLater(new Runnable() {
771 setJMenuBar(createMenu(true));
785 * Create the redownload (then delete original) menu item.
789 private JMenuItem
createMenuItemRedownload() {
790 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
791 refresh
.addActionListener(new ActionListener() {
793 public void actionPerformed(ActionEvent e
) {
794 if (selectedBook
!= null) {
795 final MetaData meta
= selectedBook
.getMeta();
796 imprt(meta
.getUrl(), new StoryRunnable() {
798 public void run(Story story
) {
799 reader
.delete(meta
.getLuid());
800 GuiReaderFrame
.this.selectedBook
= null;
801 MetaData newMeta
= story
.getMeta();
802 if (!newMeta
.getSource().equals(meta
.getSource())) {
803 reader
.changeSource(newMeta
.getLuid(),
807 }, "Removing old copy");
816 * Create the delete menu item.
820 private JMenuItem
createMenuItemDelete() {
821 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
822 delete
.addActionListener(new ActionListener() {
824 public void actionPerformed(ActionEvent e
) {
825 if (selectedBook
!= null) {
826 outOfUi(null, new Runnable() {
829 reader
.delete(selectedBook
.getMeta().getLuid());
841 * Create the properties menu item.
845 private JMenuItem
createMenuItemProperties() {
846 JMenuItem delete
= new JMenuItem("Properties", KeyEvent
.VK_P
);
847 delete
.addActionListener(new ActionListener() {
849 public void actionPerformed(ActionEvent e
) {
850 if (selectedBook
!= null) {
851 outOfUi(null, new Runnable() {
854 final MetaData meta
= selectedBook
.getMeta();
856 private static final long serialVersionUID
= 1L;
857 @SuppressWarnings("unused")
858 private Object init
= init();
860 private Object
init() {
866 ImageIcon img
= GuiReaderCoverImager
868 reader
.getLibrary(), meta
);
871 setTitle(meta
.getLuid() + ": "
874 setSize(800, img
.getIconHeight() + 2 * top
);
875 setLayout(new BorderLayout());
878 JPanel mainPanel
= new JPanel(
880 JPanel mainPanelKeys
= new JPanel();
881 mainPanelKeys
.setLayout(new BoxLayout(
882 mainPanelKeys
, BoxLayout
.Y_AXIS
));
883 JPanel mainPanelValues
= new JPanel();
884 mainPanelValues
.setLayout(new BoxLayout(
885 mainPanelValues
, BoxLayout
.Y_AXIS
));
887 mainPanel
.add(mainPanelKeys
,
889 mainPanel
.add(mainPanelValues
,
890 BorderLayout
.CENTER
);
892 List
<Entry
<String
, String
>> infos
= BasicReader
895 Color trans
= new Color(0, 0, 0, 1);
896 for (Entry
<String
, String
> info
: infos
) {
897 JTextArea key
= new JTextArea(info
899 key
.setFont(new Font(key
.getFont()
900 .getFontName(), Font
.BOLD
, key
901 .getFont().getSize()));
902 key
.setEditable(false);
903 key
.setLineWrap(false);
904 key
.setBackground(trans
);
905 mainPanelKeys
.add(key
);
907 JTextArea value
= new JTextArea(info
909 value
.setEditable(false);
910 value
.setLineWrap(false);
911 value
.setBackground(trans
);
912 mainPanelValues
.add(value
);
916 JLabel imgLabel
= new JLabel(img
);
917 imgLabel
.setVerticalAlignment(JLabel
.TOP
);
920 mainPanelKeys
.setBorder(BorderFactory
921 .createEmptyBorder(top
, space
, 0, 0));
922 mainPanelValues
.setBorder(BorderFactory
923 .createEmptyBorder(top
, space
, 0, 0));
924 imgLabel
.setBorder(BorderFactory
925 .createEmptyBorder(0, space
, 0, 0));
928 add(imgLabel
, BorderLayout
.WEST
);
929 add(mainPanel
, BorderLayout
.CENTER
);
945 * Create the open menu item for a book or a source (no LUID).
949 private JMenuItem
createMenuItemOpenBook() {
950 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
951 open
.addActionListener(new ActionListener() {
953 public void actionPerformed(ActionEvent e
) {
954 if (selectedBook
!= null) {
955 if (selectedBook
.getMeta().getLuid() == null) {
957 addBookPane(selectedBook
.getMeta().getSource(), true);
960 openBook(selectedBook
);
970 * Create the SetCover menu item for a book to change the linked source
975 private JMenuItem
createMenuItemSetCover() {
976 JMenuItem open
= new JMenuItem("Set as cover for source", KeyEvent
.VK_C
);
977 open
.addActionListener(new ActionListener() {
979 public void actionPerformed(ActionEvent e
) {
980 if (selectedBook
!= null) {
981 reader
.getLibrary().setSourceCover(
982 selectedBook
.getMeta().getSource(),
983 selectedBook
.getMeta().getLuid());
984 MetaData source
= selectedBook
.getMeta().clone();
985 source
.setLuid(null);
986 GuiReaderCoverImager
.clearIcon(source
);
995 * Open a {@link GuiReaderBook} item.
998 * the {@link GuiReaderBook} to open
1000 private void openBook(final GuiReaderBook book
) {
1001 final Progress pg
= new Progress();
1002 outOfUi(pg
, new Runnable() {
1006 reader
.read(book
.getMeta().getLuid(), false, pg
);
1007 SwingUtilities
.invokeLater(new Runnable() {
1010 book
.setCached(true);
1013 } catch (IOException e
) {
1014 // TODO: error message?
1015 Instance
.getTraceHandler().error(e
);
1022 * Process the given action out of the Swing UI thread and link the given
1023 * {@link ProgressBar} to the action.
1025 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
1026 * to done when the action is done.
1029 * the {@link ProgressBar} or NULL
1033 private void outOfUi(Progress progress
, final Runnable run
) {
1034 final Progress pg
= new Progress();
1035 final Progress reload
= new Progress("Reload books");
1036 if (progress
== null) {
1037 progress
= new Progress();
1040 pg
.addProgress(progress
, 90);
1041 pg
.addProgress(reload
, 10);
1044 pgBar
.setProgress(pg
);
1048 new Thread(new Runnable() {
1057 // will trigger pgBar ActionListener:
1062 }, "outOfUi thread").start();
1066 * Import a {@link Story} into the main {@link LocalLibrary}.
1068 * Should be called inside the UI thread.
1071 * TRUE for an {@link URL}, false for a {@link File}
1073 private void imprt(boolean askUrl
) {
1074 JFileChooser fc
= new JFileChooser();
1078 String clipboard
= "";
1080 clipboard
= ("" + Toolkit
.getDefaultToolkit()
1081 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
1083 } catch (Exception e
) {
1084 // No data will be handled
1087 if (clipboard
== null || !clipboard
.startsWith("http")) {
1091 url
= JOptionPane
.showInputDialog(GuiReaderFrame
.this,
1092 "url of the story to import?", "Importing from URL",
1093 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
1094 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
1095 url
= fc
.getSelectedFile().getAbsolutePath();
1100 if (url
!= null && !url
.toString().isEmpty()) {
1101 imprt(url
.toString(), null, null);
1106 * Actually import the {@link Story} into the main {@link LocalLibrary}.
1108 * Should be called inside the UI thread.
1111 * the {@link Story} to import by {@link URL}
1113 * Action to execute on success
1115 private void imprt(final String url
, final StoryRunnable onSuccess
,
1116 String onSuccessPgName
) {
1117 final Progress pg
= new Progress();
1118 final Progress pgImprt
= new Progress();
1119 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
1120 pg
.addProgress(pgImprt
, 95);
1121 pg
.addProgress(pgOnSuccess
, 5);
1123 outOfUi(pg
, new Runnable() {
1126 Exception ex
= null;
1129 story
= reader
.getLibrary().imprt(BasicReader
.getUrl(url
),
1131 } catch (IOException e
) {
1135 final Exception e
= ex
;
1137 final boolean ok
= (e
== null);
1139 pgOnSuccess
.setProgress(0);
1141 if (e
instanceof UnknownHostException
) {
1142 error("URL not supported: " + url
, "Cannot import URL",
1145 error("Failed to import " + url
+ ": \n"
1146 + e
.getMessage(), "Cannot import URL", e
);
1149 if (onSuccess
!= null) {
1150 onSuccess
.run(story
);
1159 * Enables or disables this component, depending on the value of the
1160 * parameter <code>b</code>. An enabled component can respond to user input
1161 * and generate events. Components are enabled initially by default.
1163 * Disabling this component will also affect its children.
1166 * If <code>true</code>, this component is enabled; otherwise
1167 * this component is disabled
1170 public void setEnabled(boolean b
) {
1175 for (GuiReaderGroup group
: booksByType
.keySet()) {
1176 group
.setEnabled(b
);
1178 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
1179 group
.setEnabled(b
);
1181 super.setEnabled(b
);
1186 * Display an error message and log the linked {@link Exception}.
1191 * the title of the error message
1193 * the exception to log if any
1195 private void error(final String message
, final String title
, Exception e
) {
1196 Instance
.getTraceHandler().error(title
+ ": " + message
);
1198 Instance
.getTraceHandler().error(e
);
1201 SwingUtilities
.invokeLater(new Runnable() {
1204 JOptionPane
.showMessageDialog(GuiReaderFrame
.this, message
,
1205 title
, JOptionPane
.ERROR_MESSAGE
);