Fix source/type reset on redownload, show num img
[fanfix.git] / src / be / nikiroo / fanfix / reader / GuiReaderFrame.java
index 88eccf7885a8ebf715fafb548d691c52a9bff476..87d7075ab0a744d13fce7faa512d4312a62de7d4 100644 (file)
@@ -66,6 +66,21 @@ class GuiReaderFrame extends JFrame {
        private GuiReaderBook selectedBook;
        private boolean words; // words or authors (secondary info on books)
 
+       /**
+        * A {@link Runnable} with a {@link Story} parameter.
+        * 
+        * @author niki
+        */
+       private interface StoryRunnable {
+               /**
+                * Run the action.
+                * 
+                * @param story
+                *            the story
+                */
+               public void run(Story story);
+       }
+
        /**
         * Create a new {@link GuiReaderFrame}.
         * 
@@ -101,6 +116,7 @@ class GuiReaderFrame extends JFrame {
                add(pgBar, BorderLayout.SOUTH);
 
                pgBar.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                invalidate();
                                pgBar.setProgress(null);
@@ -110,6 +126,7 @@ class GuiReaderFrame extends JFrame {
                });
 
                pgBar.addUpdateListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                invalidate();
                                validate();
@@ -124,6 +141,7 @@ class GuiReaderFrame extends JFrame {
                final Progress pg = new Progress();
                final String typeF = type;
                outOfUi(pg, new Runnable() {
+                       @Override
                        public void run() {
                                GuiReaderFrame.this.reader.getLibrary().refresh(false, pg);
                                invalidate();
@@ -138,6 +156,49 @@ class GuiReaderFrame extends JFrame {
                setVisible(true);
        }
 
+       private void addSourcePanes() {
+               // Sources -> i18n
+               GuiReaderGroup bookPane = new GuiReaderGroup(reader, "Sources", color);
+
+               List<MetaData> sources = new ArrayList<MetaData>();
+               for (String source : reader.getLibrary().getSources()) {
+                       MetaData mSource = new MetaData();
+                       mSource.setLuid(null);
+                       mSource.setTitle(source);
+                       mSource.setSource(source);
+                       sources.add(mSource);
+               }
+
+               bookPane.refreshBooks(sources, false);
+
+               this.invalidate();
+               pane.invalidate();
+               pane.add(bookPane);
+               pane.validate();
+               this.validate();
+
+               bookPane.setActionListener(new BookActionListener() {
+                       @Override
+                       public void select(GuiReaderBook book) {
+                               selectedBook = book;
+                       }
+
+                       @Override
+                       public void popupRequested(GuiReaderBook book, MouseEvent e) {
+                               JPopupMenu popup = new JPopupMenu();
+                               popup.add(createMenuItemOpenBook());
+                               popup.show(e.getComponent(), e.getX(), e.getY());
+                       }
+
+                       @Override
+                       public void action(final GuiReaderBook book) {
+                               removeBookPanes();
+                               addBookPane(book.getMeta().getSource(), true);
+                               refreshBooks();
+                       }
+               });
+       }
+
        /**
         * Add a new {@link GuiReaderGroup} on the frame to display the books of the
         * selected type or author.
@@ -151,9 +212,14 @@ class GuiReaderFrame extends JFrame {
        private void addBookPane(String value, boolean type) {
                if (value == null) {
                        if (type) {
-                               for (String tt : reader.getLibrary().getSources()) {
-                                       if (tt != null) {
-                                               addBookPane(tt, type);
+                               if (Instance.getUiConfig().getBoolean(UiConfig.SOURCE_PAGE,
+                                               false)) {
+                                       addSourcePanes();
+                               } else {
+                                       for (String tt : reader.getLibrary().getSources()) {
+                                               if (tt != null) {
+                                                       addBookPane(tt, type);
+                                               }
                                        }
                                }
                        } else {
@@ -181,16 +247,19 @@ class GuiReaderFrame extends JFrame {
                this.validate();
 
                bookPane.setActionListener(new BookActionListener() {
+                       @Override
                        public void select(GuiReaderBook book) {
                                selectedBook = book;
                        }
 
+                       @Override
                        public void popupRequested(GuiReaderBook book, MouseEvent e) {
                                JPopupMenu popup = new JPopupMenu();
                                popup.add(createMenuItemOpenBook());
                                popup.addSeparator();
                                popup.add(createMenuItemExport());
                                popup.add(createMenuItemMove());
+                               popup.add(createMenuItemSetCover());
                                popup.add(createMenuItemClearCache());
                                popup.add(createMenuItemRedownload());
                                popup.addSeparator();
@@ -198,6 +267,7 @@ class GuiReaderFrame extends JFrame {
                                popup.show(e.getComponent(), e.getX(), e.getY());
                        }
 
+                       @Override
                        public void action(final GuiReaderBook book) {
                                openBook(book);
                        }
@@ -248,18 +318,21 @@ class GuiReaderFrame extends JFrame {
 
                JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U);
                imprt.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                imprt(true);
                        }
                });
                JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F);
                imprtF.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                imprt(false);
                        }
                });
                JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X);
                exit.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                GuiReaderFrame.this.dispatchEvent(new WindowEvent(
                                                GuiReaderFrame.this, WindowEvent.WINDOW_CLOSING));
@@ -292,6 +365,7 @@ class GuiReaderFrame extends JFrame {
                JMenuItem vauthors = new JMenuItem("Author");
                vauthors.setMnemonic(KeyEvent.VK_A);
                vauthors.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                words = false;
                                refreshBooks();
@@ -301,6 +375,7 @@ class GuiReaderFrame extends JFrame {
                JMenuItem vwords = new JMenuItem("Word count");
                vwords.setMnemonic(KeyEvent.VK_W);
                vwords.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                words = true;
                                refreshBooks();
@@ -317,6 +392,7 @@ class GuiReaderFrame extends JFrame {
                for (final String type : tt) {
                        JMenuItem item = new JMenuItem(type == null ? "All" : type);
                        item.addActionListener(new ActionListener() {
+                               @Override
                                public void actionPerformed(ActionEvent e) {
                                        removeBookPanes();
                                        addBookPane(type, true);
@@ -341,6 +417,7 @@ class GuiReaderFrame extends JFrame {
                        JMenuItem item = new JMenuItem(author == null ? "All"
                                        : author.isEmpty() ? "[unknown]" : author);
                        item.addActionListener(new ActionListener() {
+                               @Override
                                public void actionPerformed(ActionEvent e) {
                                        removeBookPanes();
                                        addBookPane(author, false);
@@ -376,6 +453,7 @@ class GuiReaderFrame extends JFrame {
                item.setMnemonic(KeyEvent.VK_F);
 
                item.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                ConfigEditor<Config> ed = new ConfigEditor<Config>(
                                                Config.class, Instance.getConfig(),
@@ -401,6 +479,7 @@ class GuiReaderFrame extends JFrame {
                item.setMnemonic(KeyEvent.VK_U);
 
                item.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
                                                UiConfig.class, Instance.getUiConfig(),
@@ -451,6 +530,7 @@ class GuiReaderFrame extends JFrame {
 
                JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S);
                export.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
                                        fc.showDialog(GuiReaderFrame.this, "Save");
@@ -461,6 +541,7 @@ class GuiReaderFrame extends JFrame {
                                                                + type.getDefaultExtension(false);
                                                final Progress pg = new Progress();
                                                outOfUi(pg, new Runnable() {
+                                                       @Override
                                                        public void run() {
                                                                try {
                                                                        reader.getLibrary().export(
@@ -510,14 +591,17 @@ class GuiReaderFrame extends JFrame {
        private JMenuItem createMenuItemClearCache() {
                JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C);
                refresh.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
                                        outOfUi(null, new Runnable() {
+                                               @Override
                                                public void run() {
                                                        reader.clearLocalReaderCache(selectedBook.getMeta()
                                                                        .getLuid());
                                                        selectedBook.setCached(false);
                                                        SwingUtilities.invokeLater(new Runnable() {
+                                                               @Override
                                                                public void run() {
                                                                        selectedBook.repaint();
                                                                }
@@ -554,6 +638,7 @@ class GuiReaderFrame extends JFrame {
 
                        final String ftype = type;
                        item.addActionListener(new ActionListener() {
+                               @Override
                                public void actionPerformed(ActionEvent e) {
                                        if (selectedBook != null) {
                                                String type = ftype;
@@ -563,15 +648,17 @@ class GuiReaderFrame extends JFrame {
                                                                        "Moving story",
                                                                        JOptionPane.QUESTION_MESSAGE, null, null,
                                                                        selectedBook.getMeta().getSource());
+
                                                        if (rep == null) {
                                                                return;
-                                                       } else {
-                                                               type = rep.toString();
                                                        }
+
+                                                       type = rep.toString();
                                                }
 
                                                final String ftype = type;
                                                outOfUi(null, new Runnable() {
+                                                       @Override
                                                        public void run() {
                                                                reader.changeType(selectedBook.getMeta()
                                                                                .getLuid(), ftype);
@@ -579,6 +666,7 @@ class GuiReaderFrame extends JFrame {
                                                                selectedBook = null;
 
                                                                SwingUtilities.invokeLater(new Runnable() {
+                                                                       @Override
                                                                        public void run() {
                                                                                setJMenuBar(createMenu());
                                                                        }
@@ -601,13 +689,20 @@ class GuiReaderFrame extends JFrame {
        private JMenuItem createMenuItemRedownload() {
                JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R);
                refresh.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
                                        final MetaData meta = selectedBook.getMeta();
-                                       imprt(meta.getUrl(), new Runnable() {
-                                               public void run() {
+                                       imprt(meta.getUrl(), new StoryRunnable() {
+                                               @Override
+                                               public void run(Story story) {
                                                        reader.delete(meta.getLuid());
                                                        GuiReaderFrame.this.selectedBook = null;
+                                                       MetaData newMeta = story.getMeta();
+                                                       if (!newMeta.getSource().equals(meta.getSource())) {
+                                                               reader.changeType(newMeta.getLuid(),
+                                                                               meta.getSource());
+                                                       }
                                                }
                                        }, "Removing old copy");
                                }
@@ -625,9 +720,11 @@ class GuiReaderFrame extends JFrame {
        private JMenuItem createMenuItemDelete() {
                JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D);
                delete.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
                                        outOfUi(null, new Runnable() {
+                                               @Override
                                                public void run() {
                                                        reader.delete(selectedBook.getMeta().getLuid());
                                                        selectedBook = null;
@@ -641,16 +738,45 @@ class GuiReaderFrame extends JFrame {
        }
 
        /**
-        * Create the open menu item.
+        * Create the open menu item for a book or a source (no LUID).
         * 
         * @return the item
         */
        private JMenuItem createMenuItemOpenBook() {
                JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O);
                open.addActionListener(new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
-                                       openBook(selectedBook);
+                                       if (selectedBook.getMeta().getLuid() == null) {
+                                               removeBookPanes();
+                                               addBookPane(selectedBook.getMeta().getSource(), true);
+                                               refreshBooks();
+                                       } else {
+                                               openBook(selectedBook);
+                                       }
+                               }
+                       }
+               });
+
+               return open;
+       }
+
+       /**
+        * Create the SetCover menu item for a book to change the linked source
+        * cover.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemSetCover() {
+               JMenuItem open = new JMenuItem("Set as cover for source", KeyEvent.VK_C);
+               open.addActionListener(new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               if (selectedBook != null) {
+                                       reader.getLibrary().setSourceCover(
+                                                       selectedBook.getMeta().getSource(),
+                                                       selectedBook.getMeta().getLuid());
                                }
                        }
                });
@@ -667,10 +793,12 @@ class GuiReaderFrame extends JFrame {
        private void openBook(final GuiReaderBook book) {
                final Progress pg = new Progress();
                outOfUi(pg, new Runnable() {
+                       @Override
                        public void run() {
                                try {
-                                       reader.open(book.getMeta().getLuid(), pg);
+                                       reader.read(book.getMeta().getLuid(), pg);
                                        SwingUtilities.invokeLater(new Runnable() {
+                                               @Override
                                                public void run() {
                                                        book.setCached(true);
                                                }
@@ -711,6 +839,7 @@ class GuiReaderFrame extends JFrame {
                setEnabled(false);
 
                new Thread(new Runnable() {
+                       @Override
                        public void run() {
                                run.run();
                                refreshBooks();
@@ -773,7 +902,7 @@ class GuiReaderFrame extends JFrame {
         * @param onSuccess
         *            Action to execute on success
         */
-       private void imprt(final String url, final Runnable onSuccess,
+       private void imprt(final String url, final StoryRunnable onSuccess,
                        String onSuccessPgName) {
                final Progress pg = new Progress();
                final Progress pgImprt = new Progress();
@@ -782,10 +911,13 @@ class GuiReaderFrame extends JFrame {
                pg.addProgress(pgOnSuccess, 5);
 
                outOfUi(pg, new Runnable() {
+                       @Override
                        public void run() {
                                Exception ex = null;
+                               Story story = null;
                                try {
-                                       reader.getLibrary().imprt(BasicReader.getUrl(url), pgImprt);
+                                       story = reader.getLibrary().imprt(BasicReader.getUrl(url),
+                                                       pgImprt);
                                } catch (IOException e) {
                                        ex = e;
                                }
@@ -798,6 +930,7 @@ class GuiReaderFrame extends JFrame {
                                if (!ok) {
                                        Instance.syserr(e);
                                        SwingUtilities.invokeLater(new Runnable() {
+                                               @Override
                                                public void run() {
                                                        JOptionPane.showMessageDialog(GuiReaderFrame.this,
                                                                        "Cannot import: " + url, e.getMessage(),
@@ -806,7 +939,7 @@ class GuiReaderFrame extends JFrame {
                                        });
                                } else {
                                        if (onSuccess != null) {
-                                               onSuccess.run();
+                                               onSuccess.run(story);
                                        }
                                }
                                pgOnSuccess.done();