Add progress reporting on GUI
authorNiki Roo <niki@nikiroo.be>
Sun, 19 Feb 2017 00:46:02 +0000 (01:46 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 19 Feb 2017 00:46:02 +0000 (01:46 +0100)
libs/nikiroo-utils-1.1.1-sources.jar [moved from libs/nikiroo-utils-1.1.0-sources.jar with 74% similarity]
src/be/nikiroo/fanfix/Library.java
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/output/BasicOutput.java
src/be/nikiroo/fanfix/reader/BasicReader.java
src/be/nikiroo/fanfix/reader/LocalReader.java
src/be/nikiroo/fanfix/reader/LocalReaderBook.java
src/be/nikiroo/fanfix/reader/LocalReaderFrame.java
src/be/nikiroo/fanfix/supported/BasicSupport.java
src/be/nikiroo/fanfix/supported/Cbz.java
src/be/nikiroo/fanfix/supported/E621.java

similarity index 74%
rename from libs/nikiroo-utils-1.1.0-sources.jar
rename to libs/nikiroo-utils-1.1.1-sources.jar
index f716a6cb2d90b1b9993b8c7a36f8852f8b3a2155..2a6164af1ee44985975a4a5432173c01e82ec388 100644 (file)
Binary files a/libs/nikiroo-utils-1.1.0-sources.jar and b/libs/nikiroo-utils-1.1.1-sources.jar differ
index 3b39743c989aab347e6ad3b659b46a8c58e2a8aa..88228497d8e1b6722e42ca35490006d331fadc9b 100644 (file)
@@ -14,9 +14,9 @@ import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.supported.BasicSupport;
-import be.nikiroo.utils.ui.Progress;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 import be.nikiroo.fanfix.supported.InfoReader;
+import be.nikiroo.utils.Progress;
 
 /**
  * Manage a library of Stories: import, export, list.
index 09635da1e20243482401c5da545378ee97243489..db4316e8c499d22fad8417bddde2fe2cb41f9c9e 100644 (file)
@@ -14,8 +14,8 @@ import be.nikiroo.fanfix.reader.BasicReader;
 import be.nikiroo.fanfix.reader.BasicReader.ReaderType;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
+import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.ui.UIUtils;
-import be.nikiroo.utils.ui.Progress;
 
 /**
  * Main program entry point.
@@ -229,34 +229,6 @@ public class Main {
                }
        }
 
-       /**
-        * Return an {@link URL} from this {@link String}, be it a file path or an
-        * actual {@link URL}.
-        * 
-        * @param sourceString
-        *            the source
-        * 
-        * @return the corresponding {@link URL}
-        * 
-        * @throws MalformedURLException
-        *             if this is neither a file nor a conventional {@link URL}
-        */
-       private static URL getUrl(String sourceString) throws MalformedURLException {
-               if (sourceString == null || sourceString.isEmpty()) {
-                       throw new MalformedURLException("Empty url");
-               }
-
-               URL source = null;
-               try {
-                       source = new URL(sourceString);
-               } catch (MalformedURLException e) {
-                       File sourceFile = new File(sourceString);
-                       source = sourceFile.toURI().toURL();
-               }
-
-               return source;
-       }
-
        /**
         * Import the given resource into the {@link Library}.
         * 
@@ -269,7 +241,8 @@ public class Main {
         */
        public static int imprt(String urlString, Progress pg) {
                try {
-                       Story story = Instance.getLibrary().imprt(getUrl(urlString), pg);
+                       Story story = Instance.getLibrary().imprt(
+                                       BasicReader.getUrl(urlString), pg);
                        System.out.println(story.getMeta().getLuid() + ": \""
                                        + story.getMeta().getTitle() + "\" imported.");
                } catch (IOException e) {
@@ -349,7 +322,7 @@ public class Main {
                        if (library) {
                                reader.setStory(story, null);
                        } else {
-                               reader.setStory(getUrl(story), null);
+                               reader.setStory(BasicReader.getUrl(story), null);
                        }
 
                        if (chapString != null) {
@@ -394,7 +367,7 @@ public class Main {
 
                String sourceName = urlString;
                try {
-                       URL source = getUrl(urlString);
+                       URL source = BasicReader.getUrl(urlString);
                        sourceName = source.toString();
                        if (source.toString().startsWith("file://")) {
                                sourceName = sourceName.substring("file://".length());
index 1342eb03016189535039fedf322f41cadcb92ff7..5e82f253c37e47cd535faea739d69f83c4940cd2 100644 (file)
@@ -11,7 +11,7 @@ import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.utils.ui.Progress;
+import be.nikiroo.utils.Progress;
 
 /**
  * This class is the base class used by the other output classes. It can be used
index bc3bf72f2ab7f3c576daddac33b39d4a2bfa8052..0bcfd931e1b505da7e28fb43897a46a839f17ab4 100644 (file)
@@ -1,6 +1,8 @@
 package be.nikiroo.fanfix.reader;
 
+import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 
 import be.nikiroo.fanfix.Instance;
@@ -8,7 +10,7 @@ import be.nikiroo.fanfix.Library;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.supported.BasicSupport;
-import be.nikiroo.utils.ui.Progress;
+import be.nikiroo.utils.Progress;
 
 /**
  * The class that handles the different {@link Story} readers you can use.
@@ -190,4 +192,32 @@ public abstract class BasicReader {
        public static void setDefaultReaderType(ReaderType defaultType) {
                BasicReader.defaultType = defaultType;
        }
+
+       /**
+        * Return an {@link URL} from this {@link String}, be it a file path or an
+        * actual {@link URL}.
+        * 
+        * @param sourceString
+        *            the source
+        * 
+        * @return the corresponding {@link URL}
+        * 
+        * @throws MalformedURLException
+        *             if this is neither a file nor a conventional {@link URL}
+        */
+       public static URL getUrl(String sourceString) throws MalformedURLException {
+               if (sourceString == null || sourceString.isEmpty()) {
+                       throw new MalformedURLException("Empty url");
+               }
+
+               URL source = null;
+               try {
+                       source = new URL(sourceString);
+               } catch (MalformedURLException e) {
+                       File sourceFile = new File(sourceString);
+                       source = sourceFile.toURI().toURL();
+               }
+
+               return source;
+       }
 }
index a28d4feadeeeb4502da949661923619acc16d241..7c50b033444d8c740b4a973dc1512b4354ab3ab1 100644 (file)
@@ -9,7 +9,7 @@ import be.nikiroo.fanfix.Library;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
-import be.nikiroo.utils.ui.Progress;
+import be.nikiroo.utils.Progress;
 
 class LocalReader extends BasicReader {
        private Library lib;
index e7bea6aa5c80736661cc7a852000c57743030b8a..a9b24a2019f7c2902c40c97be63ca672430e7c46 100644 (file)
@@ -59,10 +59,10 @@ class LocalReaderBook extends JPanel {
        private static final int TEXT_WIDTH = COVER_WIDTH + 40;
        private static final int TEXT_HEIGHT = 50;
        private static final String AUTHOR_COLOR = "#888888";
-
        private static final long serialVersionUID = 1L;
+
        private JLabel icon;
-       private JLabel tt;
+       private JLabel title;
        private boolean selected;
        private boolean hovered;
        private Date lastClick;
@@ -91,7 +91,7 @@ class LocalReaderBook extends JPanel {
                if (optAuthor != null && !optAuthor.isEmpty()) {
                        optAuthor = "(" + optAuthor + ")";
                }
-               tt = new JLabel(
+               title = new JLabel(
                                String.format(
                                                "<html>"
                                                                + "<body style='width: %d px; height: %d px; text-align: center'>"
@@ -102,7 +102,7 @@ class LocalReaderBook extends JPanel {
 
                this.setLayout(new BorderLayout(10, 10));
                this.add(icon, BorderLayout.CENTER);
-               this.add(tt, BorderLayout.SOUTH);
+               this.add(title, BorderLayout.SOUTH);
 
                setupListeners();
                setSelected(false);
@@ -151,14 +151,16 @@ class LocalReaderBook extends JPanel {
                        }
 
                        public void mouseClicked(MouseEvent e) {
-                               Date now = new Date();
-                               if (lastClick != null
-                                               && now.getTime() - lastClick.getTime() < doubleClickDelay) {
-                                       click(true);
-                               } else {
-                                       click(false);
+                               if (isEnabled()) {
+                                       Date now = new Date();
+                                       if (lastClick != null
+                                                       && now.getTime() - lastClick.getTime() < doubleClickDelay) {
+                                               click(true);
+                                       } else {
+                                               click(false);
+                                       }
+                                       lastClick = now;
                                }
-                               lastClick = now;
                        }
                });
        }
@@ -199,7 +201,8 @@ class LocalReaderBook extends JPanel {
                g.fillPolygon(new Polygon(xs, ys, xs.length));
 
                Color color = new Color(255, 255, 255, 0);
-               if (selected && !hovered) {
+               if (!isEnabled()) {
+               } else if (selected && !hovered) {
                        color = new Color(80, 80, 100, 40);
                } else if (!selected && hovered) {
                        color = new Color(230, 230, 255, 100);
index 670ab679807ae80ce4de6f682fe0b0098a1a1f92..966869f502c8f9a65d9fe2ef6bd55ee78d698bad 100644 (file)
@@ -19,12 +19,14 @@ import javax.swing.JMenuItem;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
+import javax.swing.SwingUtilities;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.Main;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener;
+import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.ui.ProgressBar;
 import be.nikiroo.utils.ui.WrapLayout;
 
 class LocalReaderFrame extends JFrame {
@@ -35,6 +37,8 @@ class LocalReaderFrame extends JFrame {
        private JPanel bookPane;
        private String type;
        private Color color;
+       private ProgressBar pgBar;
+       private JMenuBar bar;
 
        public LocalReaderFrame(LocalReader reader, String type) {
                super("Fanfix Library");
@@ -48,18 +52,7 @@ class LocalReaderFrame extends JFrame {
                books = new ArrayList<LocalReaderBook>();
                bookPane = new JPanel(new WrapLayout(WrapLayout.LEADING, 5, 5));
 
-               color = null;
-               String bg = Instance.getUiConfig().getString(UiConfig.BACKGROUND_COLOR);
-               if (bg.startsWith("#") && bg.length() == 7) {
-                       try {
-                               color = new Color(Integer.parseInt(bg.substring(1, 3), 16),
-                                               Integer.parseInt(bg.substring(3, 5), 16),
-                                               Integer.parseInt(bg.substring(5, 7), 16));
-                       } catch (NumberFormatException e) {
-                               color = null; // no changes
-                               e.printStackTrace();
-                       }
-               }
+               color = Instance.getUiConfig().getColor(UiConfig.BACKGROUND_COLOR);
 
                if (color != null) {
                        setBackground(color);
@@ -70,6 +63,9 @@ class LocalReaderFrame extends JFrame {
                scroll.getVerticalScrollBar().setUnitIncrement(16);
                add(scroll, BorderLayout.CENTER);
 
+               pgBar = new ProgressBar();
+               add(pgBar, BorderLayout.SOUTH);
+
                refreshBooks(type);
                setJMenuBar(createMenu());
 
@@ -97,13 +93,18 @@ class LocalReaderFrame extends JFrame {
                                }
 
                                public void action(LocalReaderBook book) {
-                                       try {
-                                               File target = LocalReaderFrame.this.reader.getTarget(
-                                                               luid, null);
-                                               Desktop.getDesktop().browse(target.toURI());
-                                       } catch (IOException e) {
-                                               Instance.syserr(e);
-                                       }
+                                       final Progress pg = new Progress();
+                                       outOfUi(pg, new Runnable() {
+                                               public void run() {
+                                                       try {
+                                                               File target = LocalReaderFrame.this.reader
+                                                                               .getTarget(luid, pg);
+                                                               Desktop.getDesktop().browse(target.toURI());
+                                                       } catch (IOException e) {
+                                                               Instance.syserr(e);
+                                                       }
+                                               }
+                                       });
                                }
                        });
 
@@ -115,26 +116,48 @@ class LocalReaderFrame extends JFrame {
        }
 
        private JMenuBar createMenu() {
-               JMenuBar bar = new JMenuBar();
+               bar = new JMenuBar();
 
                JMenu file = new JMenu("File");
 
                JMenuItem imprt = new JMenuItem("Import", KeyEvent.VK_I);
                imprt.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
-                               String url = JOptionPane.showInputDialog(LocalReaderFrame.this,
-                                               "url of the story to import?\n" + "\n"
-                                                               + "Note: it will currently make the UI \n"
-                                                               + "unresponsive until it is downloaded...",
+                               final String url = JOptionPane.showInputDialog(
+                                               LocalReaderFrame.this, "url of the story to import?",
                                                "Importing from URL", JOptionPane.QUESTION_MESSAGE);
                                if (url != null && !url.isEmpty()) {
-                                       if (Main.imprt(url, null) != 0) {
-                                               JOptionPane.showMessageDialog(LocalReaderFrame.this,
-                                                               "Cannot import: " + url, "Imort error",
-                                                               JOptionPane.ERROR_MESSAGE);
-                                       } else {
-                                               refreshBooks(type);
-                                       }
+                                       final Progress pg = new Progress("Importing " + url);
+                                       outOfUi(pg, new Runnable() {
+                                               public void run() {
+                                                       Exception ex = null;
+                                                       try {
+                                                               Instance.getLibrary().imprt(
+                                                                               BasicReader.getUrl(url), pg);
+                                                       } catch (IOException e) {
+                                                               ex = e;
+                                                       }
+
+                                                       final Exception e = ex;
+
+                                                       final boolean ok = (e == null);
+                                                       SwingUtilities.invokeLater(new Runnable() {
+                                                               public void run() {
+                                                                       if (!ok) {
+                                                                               JOptionPane.showMessageDialog(
+                                                                                               LocalReaderFrame.this,
+                                                                                               "Cannot import: " + url,
+                                                                                               e.getMessage(),
+                                                                                               JOptionPane.ERROR_MESSAGE);
+
+                                                                               setAllEnabled(true);
+                                                                       } else {
+                                                                               refreshBooks(type);
+                                                                       }
+                                                               }
+                                                       });
+                                               }
+                                       });
                                }
                        }
                });
@@ -167,4 +190,44 @@ class LocalReaderFrame extends JFrame {
 
                return bar;
        }
+
+       private void outOfUi(final Progress pg, final Runnable run) {
+               pgBar.setProgress(pg);
+
+               SwingUtilities.invokeLater(new Runnable() {
+                       public void run() {
+                               setAllEnabled(false);
+                               pgBar.addActioListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               pgBar.setProgress(null);
+                                               setAllEnabled(true);
+                                       }
+                               });
+                       }
+               });
+
+               new Thread(new Runnable() {
+                       public void run() {
+                               run.run();
+                               if (!pg.isDone()) {
+                                       pg.setProgress(pg.getMax());
+                               }
+                       }
+               }).start();
+       }
+
+       public void setAllEnabled(boolean enabled) {
+               for (LocalReaderBook book : books) {
+                       book.setEnabled(enabled);
+                       book.validate();
+                       book.repaint();
+               }
+               bar.setEnabled(enabled);
+               bookPane.setEnabled(enabled);
+               bookPane.validate();
+               bookPane.repaint();
+               setEnabled(enabled);
+               validate();
+               repaint();
+       }
 }
index c47d05e608dd43de574aa69cc96ee677168eccee..be198416cb2459166c66bf84222164606c097b36 100644 (file)
@@ -23,8 +23,8 @@ import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.StringUtils;
-import be.nikiroo.utils.ui.Progress;
 
 /**
  * This class is the base class used by the other support classes. It can be
@@ -377,8 +377,7 @@ public abstract class BasicSupport {
                                                chapIn.close();
                                        }
 
-                                       pgChaps.setProgress(i);
-                                       i++;
+                                       pgChaps.setProgress(i++);
                                }
                        } else {
                                pg.setProgress(100);
index 295dc15a92ddc699792fdd4914c7a05f14056918..b63ec5d45395c8a8529e1e20bcdfef8f267626e4 100644 (file)
@@ -14,7 +14,7 @@ import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.utils.ui.Progress;
+import be.nikiroo.utils.Progress;
 
 /**
  * Support class for CBZ files (works better with CBZ created with this program,
index 5ebd6b317661cf0ff2b1ff0cf357559df469b380..cde7f0a45045db5239e6155f44a265c12a59e9b1 100644 (file)
@@ -13,8 +13,8 @@ import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.StringUtils;
-import be.nikiroo.utils.ui.Progress;
 
 /**
  * Support class for <a href="http://e621.net/">e621.net</a> and <a