Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderFrame.java
index e207023ddf2b145bc4c4e0e69ea5836d8abfff1f..e0ef6d72f8a6b75805e3989a46591ae5e7abbbaf 100644 (file)
@@ -31,11 +31,14 @@ import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.BasicLibrary.Status;
 import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.reader.BasicReader;
 import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.FrameHelper;
-import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.StoryRunnable;
+import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.MetaDataRunnable;
+import be.nikiroo.fanfix.searchable.BasicSearchable;
+import be.nikiroo.fanfix.supported.SupportType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.ui.ConfigEditor;
@@ -78,7 +81,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
         */
        public GuiReaderFrame(GuiReader reader, String type) {
                super(getAppTitle(reader.getLibrary().getLibraryName()));
-               
+
                this.reader = reader;
 
                mainPanel = new GuiReaderMainPanel(this, type);
@@ -90,20 +93,26 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
 
        @Override
        public JPopupMenu createBookPopup() {
+               Status status = reader.getLibrary().getStatus();
                JPopupMenu popup = new JPopupMenu();
                popup.add(createMenuItemOpenBook());
                popup.addSeparator();
                popup.add(createMenuItemExport());
-               popup.add(createMenuItemMoveTo(true));
-               popup.add(createMenuItemSetCoverForSource());
-               popup.add(createMenuItemSetCoverForAuthor());
+               if (status.isWritable()) {
+                       popup.add(createMenuItemMoveTo());
+                       popup.add(createMenuItemSetCoverForSource());
+                       popup.add(createMenuItemSetCoverForAuthor());
+               }
+               popup.add(createMenuItemDownloadToCache());
                popup.add(createMenuItemClearCache());
-               popup.add(createMenuItemRedownload());
-               popup.addSeparator();
-               popup.add(createMenuItemRename(true));
-               popup.add(createMenuItemSetAuthor(true));
-               popup.addSeparator();
-               popup.add(createMenuItemDelete());
+               if (status.isWritable()) {
+                       popup.add(createMenuItemRedownload());
+                       popup.addSeparator();
+                       popup.add(createMenuItemRename());
+                       popup.add(createMenuItemSetAuthor());
+                       popup.addSeparator();
+                       popup.add(createMenuItemDelete());
+               }
                popup.addSeparator();
                popup.add(createMenuItemProperties());
                return popup;
@@ -117,7 +126,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
        }
 
        @Override
-       public void createMenu(boolean libOk) {
+       public void createMenu(Status status) {
                invalidate();
 
                JMenuBar bar = new JMenuBar();
@@ -155,13 +164,15 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
 
                file.add(createMenuItemOpenBook());
                file.add(createMenuItemExport());
-               file.add(createMenuItemMoveTo(libOk));
-               file.addSeparator();
-               file.add(imprt);
-               file.add(imprtF);
-               file.addSeparator();
-               file.add(createMenuItemRename(libOk));
-               file.add(createMenuItemSetAuthor(libOk));
+               if (status.isWritable()) {
+                       file.add(createMenuItemMoveTo());
+                       file.addSeparator();
+                       file.add(imprt);
+                       file.add(imprtF);
+                       file.addSeparator();
+                       file.add(createMenuItemRename());
+                       file.add(createMenuItemSetAuthor());
+               }
                file.addSeparator();
                file.add(createMenuItemProperties());
                file.addSeparator();
@@ -174,6 +185,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
 
                edit.add(createMenuItemSetCoverForSource());
                edit.add(createMenuItemSetCoverForAuthor());
+               edit.add(createMenuItemDownloadToCache());
                edit.add(createMenuItemClearCache());
                edit.add(createMenuItemRedownload());
                edit.addSeparator();
@@ -181,6 +193,24 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
 
                bar.add(edit);
 
+               JMenu search = new JMenu(GuiReader.trans(StringIdGui.MENU_SEARCH));
+               search.setMnemonic(KeyEvent.VK_H);
+               for (final SupportType type : SupportType.values()) {
+                       BasicSearchable searchable = BasicSearchable.getSearchable(type);
+                       if (searchable != null) {
+                               JMenuItem searchItem = new JMenuItem(type.getSourceName());
+                               searchItem.addActionListener(new ActionListener() {
+                                       @Override
+                                       public void actionPerformed(ActionEvent e) {
+                                               reader.search(type, null, 1, 0, false);
+                                       }
+                               });
+                               search.add(searchItem);
+                       }
+               }
+
+               bar.add(search);
+
                JMenu view = new JMenu(GuiReader.trans(StringIdGui.MENU_VIEW));
                view.setMnemonic(KeyEvent.VK_V);
                JMenuItem vauthors = new JMenuItem(
@@ -208,8 +238,12 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                bar.add(view);
 
                Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
-               if (libOk) {
-                       groupedSources = reader.getLibrary().getSourcesGrouped();
+               if (status.isReady()) {
+                       try {
+                               groupedSources = reader.getLibrary().getSourcesGrouped();
+                       } catch (IOException e) {
+                               error(e.getLocalizedMessage(), "IOException", e);
+                       }
                }
                JMenu sources = new JMenu(GuiReader.trans(StringIdGui.MENU_SOURCES));
                sources.setMnemonic(KeyEvent.VK_S);
@@ -217,8 +251,12 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                bar.add(sources);
 
                Map<String, List<String>> goupedAuthors = new HashMap<String, List<String>>();
-               if (libOk) {
-                       goupedAuthors = reader.getLibrary().getAuthorsGrouped();
+               if (status.isReady()) {
+                       try {
+                               goupedAuthors = reader.getLibrary().getAuthorsGrouped();
+                       } catch (IOException e) {
+                               error(e.getLocalizedMessage(), "IOException", e);
+                       }
                }
                JMenu authors = new JMenu(GuiReader.trans(StringIdGui.MENU_AUTHORS));
                authors.setMnemonic(KeyEvent.VK_A);
@@ -324,9 +362,13 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                        final boolean listMode) {
                return new ActionListener() {
                        @Override
-                       public void actionPerformed(ActionEvent e) {
+                       public void actionPerformed(ActionEvent ae) {
                                mainPanel.removeBookPanes();
-                               mainPanel.addBookPane(type, listMode);
+                               try {
+                                       mainPanel.addBookPane(type, listMode);
+                               } catch (IOException e) {
+                                       error(e.getLocalizedMessage(), "IOException", e);
+                               }
                                mainPanel.refreshBooks();
                        }
                };
@@ -346,11 +388,10 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                ConfigEditor<Config> ed = new ConfigEditor<Config>(
-                                               Config.class, Instance.getConfig(), GuiReader
-                                                               .trans(StringIdGui.SUBTITLE_CONFIG));
+                                               Config.class, Instance.getInstance().getConfig(), GuiReader.trans(StringIdGui.SUBTITLE_CONFIG));
                                JFrame frame = new JFrame(title);
                                frame.add(ed);
-                               frame.setSize(800, 600);
+                               frame.setSize(850, 600);
                                frame.setVisible(true);
                        }
                });
@@ -372,8 +413,8 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
-                                               UiConfig.class, Instance.getUiConfig(), GuiReader
-                                                               .trans(StringIdGui.SUBTITLE_CONFIG_UI));
+                                               UiConfig.class, Instance.getInstance().getUiConfig(),
+                                               GuiReader.trans(StringIdGui.SUBTITLE_CONFIG_UI));
                                JFrame frame = new JFrame(title);
                                frame.add(ed);
                                frame.setSize(800, 600);
@@ -421,7 +462,8 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                        fc.showDialog(GuiReaderFrame.this,
                                                        GuiReader.trans(StringIdGui.TITLE_SAVE));
                                        if (fc.getSelectedFile() != null) {
-                                               final OutputType type = otherFilters.get(fc.getFileFilter());
+                                               final OutputType type = otherFilters.get(fc
+                                                               .getFileFilter());
                                                final String path = fc.getSelectedFile()
                                                                .getAbsolutePath()
                                                                + type.getDefaultExtension(false);
@@ -434,7 +476,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                                                                        selectedBook.getInfo().getMeta()
                                                                                                        .getLuid(), type, path, pg);
                                                                } catch (IOException e) {
-                                                                       Instance.getTraceHandler().error(e);
+                                                                       Instance.getInstance().getTraceHandler().error(e);
                                                                }
                                                        }
                                                });
@@ -509,19 +551,18 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
        /**
         * Create the "move to" menu item.
         * 
-        * @param libOk
-        *            the library can be queried
-        * 
         * @return the item
         */
-       private JMenuItem createMenuItemMoveTo(boolean libOk) {
+       private JMenuItem createMenuItemMoveTo() {
                JMenu changeTo = new JMenu(
                                GuiReader.trans(StringIdGui.MENU_FILE_MOVE_TO));
                changeTo.setMnemonic(KeyEvent.VK_M);
 
                Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
-               if (libOk) {
+               try {
                        groupedSources = reader.getLibrary().getSourcesGrouped();
+               } catch (IOException e) {
+                       error(e.getLocalizedMessage(), "IOException", e);
                }
 
                JMenuItem item = new JMenuItem(
@@ -562,12 +603,9 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
        /**
         * Create the "set author" menu item.
         * 
-        * @param libOk
-        *            the library can be queried
-        * 
         * @return the item
         */
-       private JMenuItem createMenuItemSetAuthor(boolean libOk) {
+       private JMenuItem createMenuItemSetAuthor() {
                JMenu changeTo = new JMenu(
                                GuiReader.trans(StringIdGui.MENU_FILE_SET_AUTHOR));
                changeTo.setMnemonic(KeyEvent.VK_A);
@@ -580,34 +618,39 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                newItem.addActionListener(createMoveAction(ChangeAction.AUTHOR, null));
 
                // Existing authors
-               if (libOk) {
-                       Map<String, List<String>> groupedAuthors = reader.getLibrary()
-                                       .getAuthorsGrouped();
-
-                       if (groupedAuthors.size() > 1) {
-                               for (String key : groupedAuthors.keySet()) {
-                                       JMenu group = new JMenu(key);
-                                       for (String value : groupedAuthors.get(key)) {
-                                               JMenuItem item = new JMenuItem(
-                                                               value.isEmpty() ? GuiReader
-                                                                               .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
-                                                                               : value);
-                                               item.addActionListener(createMoveAction(
-                                                               ChangeAction.AUTHOR, value));
-                                               group.add(item);
-                                       }
-                                       changeTo.add(group);
-                               }
-                       } else if (groupedAuthors.size() == 1) {
-                               for (String value : groupedAuthors.values().iterator().next()) {
+               Map<String, List<String>> groupedAuthors;
+
+               try {
+                       groupedAuthors = reader.getLibrary().getAuthorsGrouped();
+               } catch (IOException e) {
+                       error(e.getLocalizedMessage(), "IOException", e);
+                       groupedAuthors = new HashMap<String, List<String>>();
+
+               }
+
+               if (groupedAuthors.size() > 1) {
+                       for (String key : groupedAuthors.keySet()) {
+                               JMenu group = new JMenu(key);
+                               for (String value : groupedAuthors.get(key)) {
                                        JMenuItem item = new JMenuItem(
                                                        value.isEmpty() ? GuiReader
                                                                        .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
                                                                        : value);
                                        item.addActionListener(createMoveAction(
                                                        ChangeAction.AUTHOR, value));
-                                       changeTo.add(item);
+                                       group.add(item);
                                }
+                               changeTo.add(group);
+                       }
+               } else if (groupedAuthors.size() == 1) {
+                       for (String value : groupedAuthors.values().iterator().next()) {
+                               JMenuItem item = new JMenuItem(
+                                               value.isEmpty() ? GuiReader
+                                                               .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
+                                                               : value);
+                               item.addActionListener(createMoveAction(ChangeAction.AUTHOR,
+                                               value));
+                               changeTo.add(item);
                        }
                }
 
@@ -617,13 +660,9 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
        /**
         * Create the "rename" menu item.
         * 
-        * @param libOk
-        *            the library can be queried
-        * 
         * @return the item
         */
-       private JMenuItem createMenuItemRename(
-                       @SuppressWarnings("unused") boolean libOk) {
+       private JMenuItem createMenuItemRename() {
                JMenuItem changeTo = new JMenuItem(
                                GuiReader.trans(StringIdGui.MENU_FILE_RENAME));
                changeTo.setMnemonic(KeyEvent.VK_R);
@@ -693,7 +732,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                                        SwingUtilities.invokeLater(new Runnable() {
                                                                @Override
                                                                public void run() {
-                                                                       createMenu(true);
+                                                                       createMenu(reader.getLibrary().getStatus());
                                                                }
                                                        });
                                                }
@@ -718,21 +757,37 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
                                if (selectedBook != null) {
                                        final MetaData meta = selectedBook.getInfo().getMeta();
-                                       mainPanel.imprt(
-                                                       meta.getUrl(),
-                                                       new StoryRunnable() {
-                                                               @Override
-                                                               public void run(Story story) {
-                                                                       MetaData newMeta = story.getMeta();
-                                                                       if (!newMeta.getSource().equals(
-                                                                                       meta.getSource())) {
-                                                                               reader.changeSource(newMeta.getLuid(),
-                                                                                               meta.getSource());
-                                                                       }
-                                                               }
-                                                       },
-                                                       GuiReader
-                                                                       .trans(StringIdGui.PROGRESS_CHANGE_SOURCE));
+                                       mainPanel.imprt(meta.getUrl(), new MetaDataRunnable() {
+                                               @Override
+                                               public void run(MetaData newMeta) {
+                                                       if (!newMeta.getSource().equals(meta.getSource())) {
+                                                               reader.changeSource(newMeta.getLuid(),
+                                                                               meta.getSource());
+                                                       }
+                                               }
+                                       }, GuiReader.trans(StringIdGui.PROGRESS_CHANGE_SOURCE));
+                               }
+                       }
+               });
+
+               return refresh;
+       }
+       
+       /**
+        * Create the download to cache menu item.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemDownloadToCache() {
+               JMenuItem refresh = new JMenuItem(
+                               GuiReader.trans(StringIdGui.MENU_EDIT_DOWNLOAD_TO_CACHE),
+                               KeyEvent.VK_T);
+               refresh.addActionListener(new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
+                               if (selectedBook != null) {
+                                       mainPanel.prefetchBook(selectedBook);
                                }
                        }
                });
@@ -740,6 +795,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                return refresh;
        }
 
+
        /**
         * Create the delete menu item.
         * 
@@ -848,7 +904,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                KeyEvent.VK_C);
                open.addActionListener(new ActionListener() {
                        @Override
-                       public void actionPerformed(ActionEvent e) {
+                       public void actionPerformed(ActionEvent ae) {
                                final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
                                if (selectedBook != null) {
                                        BasicLibrary lib = reader.getLibrary();
@@ -856,7 +912,11 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                        String source = selectedBook.getInfo().getMeta()
                                                        .getSource();
 
-                                       lib.setSourceCover(source, luid);
+                                       try {
+                                               lib.setSourceCover(source, luid);
+                                       } catch (IOException e) {
+                                               error(e.getLocalizedMessage(), "IOException", e);
+                                       }
 
                                        GuiReaderBookInfo sourceInfo = GuiReaderBookInfo
                                                        .fromSource(lib, source);
@@ -880,7 +940,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                KeyEvent.VK_A);
                open.addActionListener(new ActionListener() {
                        @Override
-                       public void actionPerformed(ActionEvent e) {
+                       public void actionPerformed(ActionEvent ae) {
                                final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
                                if (selectedBook != null) {
                                        BasicLibrary lib = reader.getLibrary();
@@ -888,7 +948,11 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
                                        String author = selectedBook.getInfo().getMeta()
                                                        .getAuthor();
 
-                                       lib.setAuthorCover(author, luid);
+                                       try {
+                                               lib.setAuthorCover(author, luid);
+                                       } catch (IOException e) {
+                                               error(e.getLocalizedMessage(), "IOException", e);
+                                       }
 
                                        GuiReaderBookInfo authorInfo = GuiReaderBookInfo
                                                        .fromAuthor(lib, author);
@@ -911,9 +975,9 @@ class GuiReaderFrame extends JFrame implements FrameHelper {
         *            the exception to log if any
         */
        public void error(final String message, final String title, Exception e) {
-               Instance.getTraceHandler().error(title + ": " + message);
+               Instance.getInstance().getTraceHandler().error(title + ": " + message);
                if (e != null) {
-                       Instance.getTraceHandler().error(e);
+                       Instance.getInstance().getTraceHandler().error(e);
                }
 
                SwingUtilities.invokeLater(new Runnable() {