Fix cover not deleted, add new UI option "Move to"
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderFrame.java
index a90360eed234bde1c99349844d687b02832bf70a..03df7fbdf5cf133d86ac5d11634392e7edb61632 100644 (file)
@@ -13,6 +13,7 @@ import java.awt.event.WindowEvent;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -34,6 +35,7 @@ import javax.swing.filechooser.FileNameExtensionFilter;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.Library;
+import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
@@ -41,6 +43,7 @@ import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
+import be.nikiroo.utils.ui.ConfigEditor;
 import be.nikiroo.utils.ui.ProgressBar;
 
 /**
@@ -97,13 +100,40 @@ class LocalReaderFrame extends JFrame {
                pgBar = new ProgressBar();
                add(pgBar, BorderLayout.SOUTH);
 
-               setJMenuBar(createMenu());
+               pgBar.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               invalidate();
+                               pgBar.setProgress(null);
+                               validate();
+                               setEnabled(true);
+                       }
+               });
+
+               pgBar.addUpdateListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               invalidate();
+                               validate();
+                               repaint();
+                       }
+               });
 
                booksByType = new HashMap<LocalReaderGroup, String>();
                booksByAuthor = new HashMap<LocalReaderGroup, String>();
 
-               addBookPane(type, true);
-               refreshBooks();
+               pane.setVisible(false);
+               final Progress pg = new Progress();
+               final String typeF = type;
+               outOfUi(pg, new Runnable() {
+                       public void run() {
+                               Instance.getLibrary().refresh(pg);
+                               invalidate();
+                               setJMenuBar(createMenu());
+                               addBookPane(typeF, true);
+                               refreshBooks();
+                               validate();
+                               pane.setVisible(true);
+                       }
+               });
 
                setVisible(true);
        }
@@ -113,7 +143,8 @@ class LocalReaderFrame extends JFrame {
         * the selected type or author.
         * 
         * @param value
-        *            the author or the type
+        *            the author or the type, or NULL to get all the
+        *            authors-or-types
         * @param type
         *            TRUE for type, FALSE for author
         */
@@ -159,6 +190,7 @@ class LocalReaderFrame extends JFrame {
                                popup.add(createMenuItemOpenBook());
                                popup.addSeparator();
                                popup.add(createMenuItemExport());
+                               popup.add(createMenuItemMove());
                                popup.add(createMenuItemClearCache());
                                popup.add(createMenuItemRedownload());
                                popup.addSeparator();
@@ -185,8 +217,6 @@ class LocalReaderFrame extends JFrame {
        /**
         * Refresh the list of {@link LocalReaderBook}s from disk.
         * 
-        * @param type
-        *            the type of {@link Story} to load, or NULL for all types
         */
        private void refreshBooks() {
                for (LocalReaderGroup group : booksByType.keySet()) {
@@ -238,6 +268,7 @@ class LocalReaderFrame extends JFrame {
 
                file.add(createMenuItemOpenBook());
                file.add(createMenuItemExport());
+               file.add(createMenuItemMove());
                file.addSeparator();
                file.add(imprt);
                file.add(imprtF);
@@ -325,9 +356,65 @@ class LocalReaderFrame extends JFrame {
 
                bar.add(authors);
 
+               JMenu options = new JMenu("Options");
+               options.setMnemonic(KeyEvent.VK_O);
+               options.add(createMenuItemConfig());
+               options.add(createMenuItemUiConfig());
+               bar.add(options);
+
                return bar;
        }
 
+       /**
+        * Create the Fanfix Configuration menu item.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemConfig() {
+               final String title = "Fanfix Configuration";
+               JMenuItem item = new JMenuItem(title);
+               item.setMnemonic(KeyEvent.VK_F);
+
+               item.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               ConfigEditor<Config> ed = new ConfigEditor<Config>(
+                                               Config.class, Instance.getConfig(),
+                                               "This is where you configure the options of the program.");
+                               JFrame frame = new JFrame(title);
+                               frame.add(ed);
+                               frame.setSize(800, 600);
+                               frame.setVisible(true);
+                       }
+               });
+
+               return item;
+       }
+
+       /**
+        * Create the UI Configuration menu item.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemUiConfig() {
+               final String title = "UI Configuration";
+               JMenuItem item = new JMenuItem(title);
+               item.setMnemonic(KeyEvent.VK_U);
+
+               item.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
+                                               UiConfig.class, Instance.getUiConfig(),
+                                               "This is where you configure the graphical appearence of the program.");
+                               JFrame frame = new JFrame(title);
+                               frame.add(ed);
+                               frame.setSize(800, 600);
+                               frame.setVisible(true);
+                       }
+               });
+
+               return item;
+       }
+
        /**
         * Create the export menu item.
         * 
@@ -427,7 +514,8 @@ class LocalReaderFrame extends JFrame {
                                if (selectedBook != null) {
                                        outOfUi(null, new Runnable() {
                                                public void run() {
-                                                       reader.refresh(selectedBook.getMeta().getLuid());
+                                                       reader.clearLocalReaderCache(selectedBook.getMeta()
+                                                                       .getLuid());
                                                        selectedBook.setCached(false);
                                                        SwingUtilities.invokeLater(new Runnable() {
                                                                public void run() {
@@ -443,6 +531,68 @@ class LocalReaderFrame extends JFrame {
                return refresh;
        }
 
+       /**
+        * Create the delete menu item.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemMove() {
+               JMenu moveTo = new JMenu("Move to...");
+               moveTo.setMnemonic(KeyEvent.VK_M);
+
+               List<String> types = new ArrayList<String>();
+               types.add(null);
+               types.addAll(Instance.getLibrary().getTypes());
+
+               for (String type : types) {
+                       JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
+
+                       moveTo.add(item);
+                       if (type == null) {
+                               moveTo.addSeparator();
+                       }
+
+                       final String ftype = type;
+                       item.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       if (selectedBook != null) {
+                                               String type = ftype;
+                                               if (type == null) {
+                                                       Object rep = JOptionPane.showInputDialog(
+                                                                       LocalReaderFrame.this, "Move to:",
+                                                                       "Moving story",
+                                                                       JOptionPane.QUESTION_MESSAGE, null, null,
+                                                                       selectedBook.getMeta().getSource());
+                                                       if (rep == null) {
+                                                               return;
+                                                       } else {
+                                                               type = rep.toString();
+                                                       }
+                                               }
+
+                                               final String ftype = type;
+                                               outOfUi(null, new Runnable() {
+                                                       public void run() {
+                                                               reader.changeType(selectedBook.getMeta()
+                                                                               .getLuid(), ftype);
+
+                                                               selectedBook = null;
+
+                                                               SwingUtilities.invokeLater(new Runnable() {
+                                                                       public void run() {
+                                                                               setJMenuBar(createMenu());
+                                                                       }
+                                                               });
+                                                       }
+                                               });
+                                       }
+                               }
+                       });
+               }
+
+               return moveTo;
+       }
+
        /**
         * Create the redownload (then delete original) menu item.
         * 
@@ -453,12 +603,13 @@ class LocalReaderFrame extends JFrame {
                refresh.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
-                                       imprt(selectedBook.getMeta().getUrl(), new Runnable() {
+                                       final MetaData meta = selectedBook.getMeta();
+                                       imprt(meta.getUrl(), new Runnable() {
                                                public void run() {
-                                                       reader.delete(selectedBook.getMeta().getLuid());
-                                                       selectedBook = null;
+                                                       reader.delete(meta.getLuid());
+                                                       LocalReaderFrame.this.selectedBook = null;
                                                }
-                                       });
+                                       }, "Removing old copy");
                                }
                        }
                });
@@ -480,11 +631,6 @@ class LocalReaderFrame extends JFrame {
                                                public void run() {
                                                        reader.delete(selectedBook.getMeta().getLuid());
                                                        selectedBook = null;
-                                                       SwingUtilities.invokeLater(new Runnable() {
-                                                               public void run() {
-                                                                       refreshBooks();
-                                                               }
-                                                       });
                                                }
                                        });
                                }
@@ -549,31 +695,32 @@ class LocalReaderFrame extends JFrame {
         * @param run
         *            the action to run
         */
-       private void outOfUi(final Progress pg, final Runnable run) {
-               pgBar.setProgress(pg);
+       private void outOfUi(Progress progress, final Runnable run) {
+               final Progress pg = new Progress();
+               final Progress reload = new Progress("Reload books");
+               if (progress == null) {
+                       progress = new Progress();
+               }
+
+               pg.addProgress(progress, 90);
+               pg.addProgress(reload, 10);
 
+               invalidate();
+               pgBar.setProgress(pg);
+               validate();
                setEnabled(false);
-               pgBar.addActioListener(new ActionListener() {
-                       public void actionPerformed(ActionEvent e) {
-                               pgBar.setProgress(null);
-                               setEnabled(true);
-                       }
-               });
 
                new Thread(new Runnable() {
                        public void run() {
                                run.run();
-                               if (pg == null) {
-                                       SwingUtilities.invokeLater(new Runnable() {
-                                               public void run() {
-                                                       setEnabled(true);
-                                               }
-                                       });
-                               } else if (!pg.isDone()) {
-                                       pg.setProgress(pg.getMax());
+                               refreshBooks();
+                               reload.done();
+                               if (!pg.isDone()) {
+                                       // will trigger pgBar ActionListener:
+                                       pg.done();
                                }
                        }
-               }).start();
+               }, "outOfUi thread").start();
        }
 
        /**
@@ -612,7 +759,7 @@ class LocalReaderFrame extends JFrame {
                }
 
                if (url != null && !url.toString().isEmpty()) {
-                       imprt(url.toString(), null);
+                       imprt(url.toString(), null, null);
                }
        }
 
@@ -626,13 +773,20 @@ class LocalReaderFrame extends JFrame {
         * @param onSuccess
         *            Action to execute on success
         */
-       private void imprt(final String url, final Runnable onSuccess) {
-               final Progress pg = new Progress("Importing " + url);
+       private void imprt(final String url, final Runnable onSuccess,
+                       String onSuccessPgName) {
+               final Progress pg = new Progress();
+               final Progress pgImprt = new Progress();
+               final Progress pgOnSuccess = new Progress(onSuccessPgName);
+               pg.addProgress(pgImprt, 95);
+               pg.addProgress(pgOnSuccess, 5);
+
                outOfUi(pg, new Runnable() {
                        public void run() {
                                Exception ex = null;
                                try {
-                                       Instance.getLibrary().imprt(BasicReader.getUrl(url), pg);
+                                       Instance.getLibrary().imprt(BasicReader.getUrl(url),
+                                                       pgImprt);
                                } catch (IOException e) {
                                        ex = e;
                                }
@@ -640,25 +794,24 @@ class LocalReaderFrame extends JFrame {
                                final Exception e = ex;
 
                                final boolean ok = (e == null);
-                               SwingUtilities.invokeLater(new Runnable() {
-                                       public void run() {
-                                               if (!ok) {
-                                                       Instance.syserr(e);
+
+                               pgOnSuccess.setProgress(0);
+                               if (!ok) {
+                                       Instance.syserr(e);
+                                       SwingUtilities.invokeLater(new Runnable() {
+                                               public void run() {
                                                        JOptionPane.showMessageDialog(
                                                                        LocalReaderFrame.this, "Cannot import: "
                                                                                        + url, e.getMessage(),
                                                                        JOptionPane.ERROR_MESSAGE);
-
-                                                       setEnabled(true);
-                                               } else {
-                                                       refreshBooks();
-                                                       if (onSuccess != null) {
-                                                               onSuccess.run();
-                                                               refreshBooks();
-                                                       }
                                                }
+                                       });
+                               } else {
+                                       if (onSuccess != null) {
+                                               onSuccess.run();
                                        }
-                               });
+                               }
+                               pgOnSuccess.done();
                        }
                });
        }
@@ -676,7 +829,10 @@ class LocalReaderFrame extends JFrame {
         */
        @Override
        public void setEnabled(boolean b) {
-               bar.setEnabled(b);
+               if (bar != null) {
+                       bar.setEnabled(b);
+               }
+
                for (LocalReaderGroup group : booksByType.keySet()) {
                        group.setEnabled(b);
                }