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(createMenuItemMoveTo(true));
311 popup
.add(createMenuItemSetCover());
312 popup
.add(createMenuItemClearCache());
313 popup
.add(createMenuItemRedownload());
314 popup
.addSeparator();
315 popup
.add(createMenuItemRename(true));
316 popup
.add(createMenuItemSetAuthor(true));
317 popup
.addSeparator();
318 popup
.add(createMenuItemDelete());
319 popup
.addSeparator();
320 popup
.add(createMenuItemProperties());
321 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
325 public void action(final GuiReaderBook book
) {
331 private void removeBookPanes() {
333 booksByAuthor
.clear();
342 * Refresh the list of {@link GuiReaderBook}s from disk.
344 private void refreshBooks() {
345 for (GuiReaderGroup group
: booksByType
.keySet()) {
346 List
<MetaData
> stories
= reader
.getLibrary().getListBySource(
347 booksByType
.get(group
));
348 group
.refreshBooks(stories
, words
);
351 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
352 List
<MetaData
> stories
= reader
.getLibrary().getListByAuthor(
353 booksByAuthor
.get(group
));
354 group
.refreshBooks(stories
, words
);
362 * Create the main menu bar.
365 * the library can be queried
369 private JMenuBar
createMenu(boolean libOk
) {
370 bar
= new JMenuBar();
372 JMenu file
= new JMenu("File");
373 file
.setMnemonic(KeyEvent
.VK_F
);
375 JMenuItem imprt
= new JMenuItem("Import URL...", KeyEvent
.VK_U
);
376 imprt
.addActionListener(new ActionListener() {
378 public void actionPerformed(ActionEvent e
) {
382 JMenuItem imprtF
= new JMenuItem("Import File...", KeyEvent
.VK_F
);
383 imprtF
.addActionListener(new ActionListener() {
385 public void actionPerformed(ActionEvent e
) {
389 JMenuItem exit
= new JMenuItem("Exit", KeyEvent
.VK_X
);
390 exit
.addActionListener(new ActionListener() {
392 public void actionPerformed(ActionEvent e
) {
393 GuiReaderFrame
.this.dispatchEvent(new WindowEvent(
394 GuiReaderFrame
.this, WindowEvent
.WINDOW_CLOSING
));
398 file
.add(createMenuItemOpenBook());
399 file
.add(createMenuItemExport());
400 file
.add(createMenuItemMoveTo(libOk
));
405 file
.add(createMenuItemRename(libOk
));
406 file
.add(createMenuItemSetAuthor(libOk
));
412 JMenu edit
= new JMenu("Edit");
413 edit
.setMnemonic(KeyEvent
.VK_E
);
415 edit
.add(createMenuItemClearCache());
416 edit
.add(createMenuItemRedownload());
418 edit
.add(createMenuItemDelete());
422 JMenu view
= new JMenu("View");
423 view
.setMnemonic(KeyEvent
.VK_V
);
424 JMenuItem vauthors
= new JMenuItem("Author");
425 vauthors
.setMnemonic(KeyEvent
.VK_A
);
426 vauthors
.addActionListener(new ActionListener() {
428 public void actionPerformed(ActionEvent e
) {
434 JMenuItem vwords
= new JMenuItem("Word count");
435 vwords
.setMnemonic(KeyEvent
.VK_W
);
436 vwords
.addActionListener(new ActionListener() {
438 public void actionPerformed(ActionEvent e
) {
446 JMenu sources
= new JMenu("Sources");
447 sources
.setMnemonic(KeyEvent
.VK_S
);
449 List
<String
> tt
= new ArrayList
<String
>();
451 tt
.addAll(reader
.getLibrary().getSources());
455 for (final String type
: tt
) {
456 JMenuItem item
= new JMenuItem(type
== null ?
"All" : type
);
457 item
.addActionListener(new ActionListener() {
459 public void actionPerformed(ActionEvent e
) {
461 addBookPane(type
, true);
468 sources
.addSeparator();
474 JMenu authors
= new JMenu("Authors");
475 authors
.setMnemonic(KeyEvent
.VK_A
);
477 List
<Entry
<String
, List
<String
>>> authorGroups
= reader
.getLibrary()
478 .getAuthorsGrouped();
479 if (authorGroups
.size() > 1) {
482 // null -> "All" authors special item
483 populateMenuAuthorList(authors
, Arrays
.asList((String
) null));
485 for (Entry
<String
, List
<String
>> group
: authorGroups
) {
486 JMenu thisGroup
= new JMenu(group
.getKey());
487 populateMenuAuthorList(thisGroup
, group
.getValue());
488 authors
.add(thisGroup
);
493 // null -> "All" authors special item
494 List
<String
> authorNames
= new ArrayList
<String
>();
495 authorNames
.add(null);
496 if (authorGroups
.size() > 0) {
497 authorNames
.addAll(authorGroups
.get(0).getValue());
499 populateMenuAuthorList(authors
, authorNames
);
504 JMenu options
= new JMenu("Options");
505 options
.setMnemonic(KeyEvent
.VK_O
);
506 options
.add(createMenuItemConfig());
507 options
.add(createMenuItemUiConfig());
514 * Populate a list of authors as {@link JMenuItem}s into the given
517 * Each item will select the author when clicked.
520 * the parent {@link JMenuItem}
524 private void populateMenuAuthorList(JMenu authors
, List
<String
> names
) {
525 for (final String name
: names
) {
526 JMenuItem item
= new JMenuItem(name
== null ?
"All"
527 : name
.isEmpty() ?
"[unknown]" : name
);
528 item
.addActionListener(new ActionListener() {
530 public void actionPerformed(ActionEvent e
) {
532 addBookPane(name
, false);
538 if (name
== null || name
.isEmpty()) {
539 authors
.addSeparator();
545 * Create the Fanfix Configuration menu item.
549 private JMenuItem
createMenuItemConfig() {
550 final String title
= "Fanfix Configuration";
551 JMenuItem item
= new JMenuItem(title
);
552 item
.setMnemonic(KeyEvent
.VK_F
);
554 item
.addActionListener(new ActionListener() {
556 public void actionPerformed(ActionEvent e
) {
557 ConfigEditor
<Config
> ed
= new ConfigEditor
<Config
>(
558 Config
.class, Instance
.getConfig(),
559 "This is where you configure the options of the program.");
560 JFrame frame
= new JFrame(title
);
562 frame
.setSize(800, 600);
563 frame
.setVisible(true);
571 * Create the UI Configuration menu item.
575 private JMenuItem
createMenuItemUiConfig() {
576 final String title
= "UI Configuration";
577 JMenuItem item
= new JMenuItem(title
);
578 item
.setMnemonic(KeyEvent
.VK_U
);
580 item
.addActionListener(new ActionListener() {
582 public void actionPerformed(ActionEvent e
) {
583 ConfigEditor
<UiConfig
> ed
= new ConfigEditor
<UiConfig
>(
584 UiConfig
.class, Instance
.getUiConfig(),
585 "This is where you configure the graphical appearence of the program.");
586 JFrame frame
= new JFrame(title
);
588 frame
.setSize(800, 600);
589 frame
.setVisible(true);
597 * Create the export menu item.
601 private JMenuItem
createMenuItemExport() {
602 final JFileChooser fc
= new JFileChooser();
603 fc
.setAcceptAllFileFilterUsed(false);
605 final Map
<FileFilter
, OutputType
> filters
= new HashMap
<FileFilter
, OutputType
>();
606 for (OutputType type
: OutputType
.values()) {
607 String ext
= type
.getDefaultExtension(false);
608 String desc
= type
.getDesc(false);
610 if (ext
== null || ext
.isEmpty()) {
611 filters
.put(createAllFilter(desc
), type
);
613 filters
.put(new FileNameExtensionFilter(desc
, ext
), type
);
617 // First the "ALL" filters, then, the extension filters
618 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
619 if (!(entry
.getKey() instanceof FileNameExtensionFilter
)) {
620 fc
.addChoosableFileFilter(entry
.getKey());
623 for (Entry
<FileFilter
, OutputType
> entry
: filters
.entrySet()) {
624 if (entry
.getKey() instanceof FileNameExtensionFilter
) {
625 fc
.addChoosableFileFilter(entry
.getKey());
630 JMenuItem export
= new JMenuItem("Save as...", KeyEvent
.VK_S
);
631 export
.addActionListener(new ActionListener() {
633 public void actionPerformed(ActionEvent e
) {
634 if (selectedBook
!= null) {
635 fc
.showDialog(GuiReaderFrame
.this, "Save");
636 if (fc
.getSelectedFile() != null) {
637 final OutputType type
= filters
.get(fc
.getFileFilter());
638 final String path
= fc
.getSelectedFile()
640 + type
.getDefaultExtension(false);
641 final Progress pg
= new Progress();
642 outOfUi(pg
, new Runnable() {
646 reader
.getLibrary().export(
647 selectedBook
.getMeta().getLuid(),
649 } catch (IOException e
) {
650 Instance
.getTraceHandler().error(e
);
663 * Create a {@link FileFilter} that accepts all files and return the given
671 private FileFilter
createAllFilter(final String desc
) {
672 return new FileFilter() {
674 public String
getDescription() {
679 public boolean accept(File f
) {
686 * Create the refresh (delete cache) menu item.
690 private JMenuItem
createMenuItemClearCache() {
691 JMenuItem refresh
= new JMenuItem("Clear cache", KeyEvent
.VK_C
);
692 refresh
.addActionListener(new ActionListener() {
694 public void actionPerformed(ActionEvent e
) {
695 if (selectedBook
!= null) {
696 outOfUi(null, new Runnable() {
699 reader
.clearLocalReaderCache(selectedBook
.getMeta()
701 selectedBook
.setCached(false);
702 GuiReaderCoverImager
.clearIcon(selectedBook
704 SwingUtilities
.invokeLater(new Runnable() {
707 selectedBook
.repaint();
720 * Create the "move to" menu item.
723 * the library can be queried
727 private JMenuItem
createMenuItemMoveTo(boolean libOk
) {
728 JMenuItem changeTo
= new JMenu("Move to");
729 changeTo
.setMnemonic(KeyEvent
.VK_M
);
731 List
<String
> values
= new ArrayList
<String
>();
734 values
.addAll(reader
.getLibrary().getSources());
737 for (String value
: values
) {
738 JMenuItem item
= new JMenuItem(value
== null ?
"New type..."
741 item
.addActionListener(createMoveAction("SOURCE", value
));
745 ((JMenu
) changeTo
).addSeparator();
753 * Create the "set author" menu item.
756 * the library can be queried
760 private JMenuItem
createMenuItemSetAuthor(boolean libOk
) {
761 JMenu changeTo
= new JMenu("Set author");
762 changeTo
.setMnemonic(KeyEvent
.VK_A
);
765 JMenuItem newItem
= new JMenuItem("New author...");
766 changeTo
.add(newItem
);
767 changeTo
.addSeparator();
768 newItem
.addActionListener(createMoveAction("AUTHOR", null));
772 List
<Entry
<String
, List
<String
>>> authorGroups
= reader
773 .getLibrary().getAuthorsGrouped();
775 if (authorGroups
.size() > 1) {
776 for (Entry
<String
, List
<String
>> entry
: authorGroups
) {
777 JMenu group
= new JMenu(entry
.getKey());
778 for (String value
: entry
.getValue()) {
779 JMenuItem item
= new JMenuItem(value
);
780 item
.addActionListener(createMoveAction("AUTHOR", value
));
785 } else if (authorGroups
.size() == 1) {
786 for (String value
: authorGroups
.get(0).getValue()) {
787 JMenuItem item
= new JMenuItem(value
);
788 item
.addActionListener(createMoveAction("AUTHOR", value
));
798 * Create the "rename" menu item.
801 * the library can be queried
805 private JMenuItem
createMenuItemRename(
806 @SuppressWarnings("unused") boolean libOk
) {
807 JMenuItem changeTo
= new JMenuItem("Rename...");
808 changeTo
.setMnemonic(KeyEvent
.VK_R
);
809 changeTo
.addActionListener(createMoveAction("TITLE", null));
813 private ActionListener
createMoveAction(final String what
, final String type
) {
814 return new ActionListener() {
816 public void actionPerformed(ActionEvent e
) {
817 if (selectedBook
!= null) {
818 String changeTo
= type
;
821 if (what
.equals("SOURCE")) {
822 init
= selectedBook
.getMeta().getSource();
823 } else if (what
.equals("TITLE")) {
824 init
= selectedBook
.getMeta().getTitle();
825 } else if (what
.equals("AUTHOR")) {
826 init
= selectedBook
.getMeta().getAuthor();
829 Object rep
= JOptionPane
.showInputDialog(
830 GuiReaderFrame
.this, "Move to:",
831 "Moving story", JOptionPane
.QUESTION_MESSAGE
,
838 changeTo
= rep
.toString();
841 final String fChangeTo
= changeTo
;
842 outOfUi(null, new Runnable() {
845 if (what
.equals("SOURCE")) {
846 reader
.changeSource(selectedBook
.getMeta()
847 .getLuid(), fChangeTo
);
848 } else if (what
.equals("TITLE")) {
849 reader
.changeTitle(selectedBook
.getMeta()
850 .getLuid(), fChangeTo
);
851 } else if (what
.equals("AUTHOR")) {
852 reader
.changeAuthor(selectedBook
.getMeta()
853 .getLuid(), fChangeTo
);
858 SwingUtilities
.invokeLater(new Runnable() {
861 setJMenuBar(createMenu(true));
872 * Create the redownload (then delete original) menu item.
876 private JMenuItem
createMenuItemRedownload() {
877 JMenuItem refresh
= new JMenuItem("Redownload", KeyEvent
.VK_R
);
878 refresh
.addActionListener(new ActionListener() {
880 public void actionPerformed(ActionEvent e
) {
881 if (selectedBook
!= null) {
882 final MetaData meta
= selectedBook
.getMeta();
883 imprt(meta
.getUrl(), new StoryRunnable() {
885 public void run(Story story
) {
886 reader
.delete(meta
.getLuid());
887 GuiReaderFrame
.this.selectedBook
= null;
888 MetaData newMeta
= story
.getMeta();
889 if (!newMeta
.getSource().equals(meta
.getSource())) {
890 reader
.changeSource(newMeta
.getLuid(),
894 }, "Removing old copy");
903 * Create the delete menu item.
907 private JMenuItem
createMenuItemDelete() {
908 JMenuItem delete
= new JMenuItem("Delete", KeyEvent
.VK_D
);
909 delete
.addActionListener(new ActionListener() {
911 public void actionPerformed(ActionEvent e
) {
912 if (selectedBook
!= null) {
913 outOfUi(null, new Runnable() {
916 reader
.delete(selectedBook
.getMeta().getLuid());
928 * Create the properties menu item.
932 private JMenuItem
createMenuItemProperties() {
933 JMenuItem delete
= new JMenuItem("Properties", KeyEvent
.VK_P
);
934 delete
.addActionListener(new ActionListener() {
936 public void actionPerformed(ActionEvent e
) {
937 if (selectedBook
!= null) {
938 outOfUi(null, new Runnable() {
941 final MetaData meta
= selectedBook
.getMeta();
943 private static final long serialVersionUID
= 1L;
944 @SuppressWarnings("unused")
945 private Object init
= init();
947 private Object
init() {
953 ImageIcon img
= GuiReaderCoverImager
955 reader
.getLibrary(), meta
);
958 setTitle(meta
.getLuid() + ": "
961 setSize(800, img
.getIconHeight() + 2 * top
);
962 setLayout(new BorderLayout());
965 JPanel mainPanel
= new JPanel(
967 JPanel mainPanelKeys
= new JPanel();
968 mainPanelKeys
.setLayout(new BoxLayout(
969 mainPanelKeys
, BoxLayout
.Y_AXIS
));
970 JPanel mainPanelValues
= new JPanel();
971 mainPanelValues
.setLayout(new BoxLayout(
972 mainPanelValues
, BoxLayout
.Y_AXIS
));
974 mainPanel
.add(mainPanelKeys
,
976 mainPanel
.add(mainPanelValues
,
977 BorderLayout
.CENTER
);
979 List
<Entry
<String
, String
>> infos
= BasicReader
982 Color trans
= new Color(0, 0, 0, 1);
983 for (Entry
<String
, String
> info
: infos
) {
984 JTextArea key
= new JTextArea(info
986 key
.setFont(new Font(key
.getFont()
987 .getFontName(), Font
.BOLD
, key
988 .getFont().getSize()));
989 key
.setEditable(false);
990 key
.setLineWrap(false);
991 key
.setBackground(trans
);
992 mainPanelKeys
.add(key
);
994 JTextArea value
= new JTextArea(info
996 value
.setEditable(false);
997 value
.setLineWrap(false);
998 value
.setBackground(trans
);
999 mainPanelValues
.add(value
);
1003 JLabel imgLabel
= new JLabel(img
);
1004 imgLabel
.setVerticalAlignment(JLabel
.TOP
);
1007 mainPanelKeys
.setBorder(BorderFactory
1008 .createEmptyBorder(top
, space
, 0, 0));
1009 mainPanelValues
.setBorder(BorderFactory
1010 .createEmptyBorder(top
, space
, 0, 0));
1011 imgLabel
.setBorder(BorderFactory
1012 .createEmptyBorder(0, space
, 0, 0));
1015 add(imgLabel
, BorderLayout
.WEST
);
1016 add(mainPanel
, BorderLayout
.CENTER
);
1032 * Create the open menu item for a book or a source (no LUID).
1036 private JMenuItem
createMenuItemOpenBook() {
1037 JMenuItem open
= new JMenuItem("Open", KeyEvent
.VK_O
);
1038 open
.addActionListener(new ActionListener() {
1040 public void actionPerformed(ActionEvent e
) {
1041 if (selectedBook
!= null) {
1042 if (selectedBook
.getMeta().getLuid() == null) {
1044 addBookPane(selectedBook
.getMeta().getSource(), true);
1047 openBook(selectedBook
);
1057 * Create the SetCover menu item for a book to change the linked source
1062 private JMenuItem
createMenuItemSetCover() {
1063 JMenuItem open
= new JMenuItem("Set as cover for source", KeyEvent
.VK_C
);
1064 open
.addActionListener(new ActionListener() {
1066 public void actionPerformed(ActionEvent e
) {
1067 if (selectedBook
!= null) {
1068 reader
.getLibrary().setSourceCover(
1069 selectedBook
.getMeta().getSource(),
1070 selectedBook
.getMeta().getLuid());
1071 MetaData source
= selectedBook
.getMeta().clone();
1072 source
.setLuid(null);
1073 GuiReaderCoverImager
.clearIcon(source
);
1082 * Open a {@link GuiReaderBook} item.
1085 * the {@link GuiReaderBook} to open
1087 private void openBook(final GuiReaderBook book
) {
1088 final Progress pg
= new Progress();
1089 outOfUi(pg
, new Runnable() {
1093 reader
.read(book
.getMeta().getLuid(), false, pg
);
1094 SwingUtilities
.invokeLater(new Runnable() {
1097 book
.setCached(true);
1100 } catch (IOException e
) {
1101 // TODO: error message?
1102 Instance
.getTraceHandler().error(e
);
1109 * Process the given action out of the Swing UI thread and link the given
1110 * {@link ProgressBar} to the action.
1112 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
1113 * to done when the action is done.
1116 * the {@link ProgressBar} or NULL
1120 private void outOfUi(Progress progress
, final Runnable run
) {
1121 final Progress pg
= new Progress();
1122 final Progress reload
= new Progress("Reload books");
1123 if (progress
== null) {
1124 progress
= new Progress();
1127 pg
.addProgress(progress
, 90);
1128 pg
.addProgress(reload
, 10);
1131 pgBar
.setProgress(pg
);
1135 new Thread(new Runnable() {
1144 // will trigger pgBar ActionListener:
1149 }, "outOfUi thread").start();
1153 * Import a {@link Story} into the main {@link LocalLibrary}.
1155 * Should be called inside the UI thread.
1158 * TRUE for an {@link URL}, false for a {@link File}
1160 private void imprt(boolean askUrl
) {
1161 JFileChooser fc
= new JFileChooser();
1165 String clipboard
= "";
1167 clipboard
= ("" + Toolkit
.getDefaultToolkit()
1168 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
1170 } catch (Exception e
) {
1171 // No data will be handled
1174 if (clipboard
== null || !clipboard
.startsWith("http")) {
1178 url
= JOptionPane
.showInputDialog(GuiReaderFrame
.this,
1179 "url of the story to import?", "Importing from URL",
1180 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
1181 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
1182 url
= fc
.getSelectedFile().getAbsolutePath();
1187 if (url
!= null && !url
.toString().isEmpty()) {
1188 imprt(url
.toString(), null, null);
1193 * Actually import the {@link Story} into the main {@link LocalLibrary}.
1195 * Should be called inside the UI thread.
1198 * the {@link Story} to import by {@link URL}
1200 * Action to execute on success
1202 private void imprt(final String url
, final StoryRunnable onSuccess
,
1203 String onSuccessPgName
) {
1204 final Progress pg
= new Progress();
1205 final Progress pgImprt
= new Progress();
1206 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
1207 pg
.addProgress(pgImprt
, 95);
1208 pg
.addProgress(pgOnSuccess
, 5);
1210 outOfUi(pg
, new Runnable() {
1213 Exception ex
= null;
1216 story
= reader
.getLibrary().imprt(BasicReader
.getUrl(url
),
1218 } catch (IOException e
) {
1222 final Exception e
= ex
;
1224 final boolean ok
= (e
== null);
1226 pgOnSuccess
.setProgress(0);
1228 if (e
instanceof UnknownHostException
) {
1229 error("URL not supported: " + url
, "Cannot import URL",
1232 error("Failed to import " + url
+ ": \n"
1233 + e
.getMessage(), "Cannot import URL", e
);
1236 if (onSuccess
!= null) {
1237 onSuccess
.run(story
);
1246 * Enables or disables this component, depending on the value of the
1247 * parameter <code>b</code>. An enabled component can respond to user input
1248 * and generate events. Components are enabled initially by default.
1250 * Disabling this component will also affect its children.
1253 * If <code>true</code>, this component is enabled; otherwise
1254 * this component is disabled
1257 public void setEnabled(boolean b
) {
1262 for (GuiReaderGroup group
: booksByType
.keySet()) {
1263 group
.setEnabled(b
);
1265 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
1266 group
.setEnabled(b
);
1268 super.setEnabled(b
);
1273 * Display an error message and log the linked {@link Exception}.
1278 * the title of the error message
1280 * the exception to log if any
1282 private void error(final String message
, final String title
, Exception e
) {
1283 Instance
.getTraceHandler().error(title
+ ": " + message
);
1285 Instance
.getTraceHandler().error(e
);
1288 SwingUtilities
.invokeLater(new Runnable() {
1291 JOptionPane
.showMessageDialog(GuiReaderFrame
.this, message
,
1292 title
, JOptionPane
.ERROR_MESSAGE
);