Progress handling in BasicOutput
authorNiki Roo <niki@nikiroo.be>
Sat, 18 Feb 2017 19:10:35 +0000 (20:10 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 18 Feb 2017 19:10:35 +0000 (20:10 +0100)
src/be/nikiroo/fanfix/Library.java
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/output/BasicOutput.java
src/be/nikiroo/fanfix/reader/LocalReader.java

index 03b584cf760849ec9fcaccc84529db806318ce87..3b39743c989aab347e6ad3b659b46a8c58e2a8aa 100644 (file)
@@ -217,17 +217,25 @@ public class Library {
         */
        public File export(String luid, OutputType type, String target, Progress pg)
                        throws IOException {
+               Progress pgGetStory = new Progress();
+               Progress pgOut = new Progress();
+               if (pg != null) {
+                       pg.setMax(2);
+                       pg.addProgress(pgGetStory, 1);
+                       pg.addProgress(pgOut, 1);
+               }
+
                BasicOutput out = BasicOutput.getOutput(type, true);
                if (out == null) {
                        throw new IOException("Output type not supported: " + type);
                }
 
-               Story story = getStory(luid, pg);
+               Story story = getStory(luid, pgGetStory);
                if (story == null) {
                        throw new IOException("Cannot find story to export: " + luid);
                }
 
-               return out.process(story, target);
+               return out.process(story, target, pgOut);
        }
 
        /**
@@ -235,14 +243,16 @@ public class Library {
         * 
         * @param story
         *            the {@link Story} to save
+        * @param pg
+        *            the optional progress reporter
         * 
         * @return the same {@link Story}, whose LUID may have changed
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       public Story save(Story story) throws IOException {
-               return save(story, null);
+       public Story save(Story story, Progress pg) throws IOException {
+               return save(story, null, pg);
        }
 
        /**
@@ -253,13 +263,15 @@ public class Library {
         *            the {@link Story} to save
         * @param luid
         *            the <b>correct</b> LUID or NULL to get the next free one
+        * @param pg
+        *            the optional progress reporter
         * 
         * @return the same {@link Story}, whose LUID may have changed
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       public Story save(Story story, String luid) throws IOException {
+       public Story save(Story story, String luid, Progress pg) throws IOException {
                // Do not change the original metadata, but change the original story
                MetaData key = story.getMeta().clone();
                story.setMeta(key);
@@ -284,7 +296,7 @@ public class Library {
                }
 
                BasicOutput it = BasicOutput.getOutput(out, true);
-               File file = it.process(story, getFile(key).getPath());
+               File file = it.process(story, getFile(key).getPath(), pg);
                getStories().put(story.getMeta(), file);
 
                return story;
index e6f584f30388a29e530e4310caeef742ebd11454..09635da1e20243482401c5da545378ee97243489 100644 (file)
@@ -411,12 +411,19 @@ public class Main {
                                        BasicSupport support = BasicSupport.getSupport(source);
 
                                        if (support != null) {
-                                               Story story = support.process(source, pg);
+                                               Progress pgIn = new Progress();
+                                               Progress pgOut = new Progress();
+                                               if (pg != null) {
+                                                       pg.setMax(2);
+                                                       pg.addProgress(pgIn, 1);
+                                                       pg.addProgress(pgOut, 1);
+                                               }
 
+                                               Story story = support.process(source, pgIn);
                                                try {
                                                        target = new File(target).getAbsolutePath();
                                                        BasicOutput.getOutput(type, infoCover).process(
-                                                                       story, target);
+                                                                       story, target, pgOut);
                                                } catch (IOException e) {
                                                        Instance.syserr(new IOException(trans(
                                                                        StringId.ERR_SAVING, target), e));
index 33a42c574bed176d90556bce1c447fba61ed26fe..1342eb03016189535039fedf322f41cadcb92ff7 100644 (file)
@@ -2,13 +2,16 @@ package be.nikiroo.fanfix.output;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.StringId;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.Paragraph;
-import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.utils.ui.Progress;
 
 /**
  * This class is the base class used by the other output classes. It can be used
@@ -135,6 +138,8 @@ public abstract class BasicOutput {
        private OutputType type;
        private boolean writeCover;
        private boolean writeInfo;
+       private Progress storyPg;
+       private Progress chapPg;
 
        /**
         * Process the {@link Story} into the given target.
@@ -144,6 +149,8 @@ public abstract class BasicOutput {
         * @param target
         *            the target where to save to (will not necessary be taken as is
         *            by the processor, for instance an extension can be added)
+        * @param pg
+        *            the optional progress reporter
         * 
         * @return the actual main target saved, which can be slightly different
         *         that the input one
@@ -151,7 +158,10 @@ public abstract class BasicOutput {
         * @throws IOException
         *             in case of I/O error
         */
-       public File process(Story story, String target) throws IOException {
+       public File process(Story story, String target, Progress pg)
+                       throws IOException {
+               storyPg = pg;
+
                target = new File(target).getAbsolutePath();
                File targetDir = new File(target).getParentFile();
                String targetName = new File(target).getName();
@@ -180,6 +190,7 @@ public abstract class BasicOutput {
         *            the target filename (will not necessary be taken as is by the
         *            processor, for instance an extension can be added)
         * 
+        * 
         * @return the actual main target saved, which can be slightly different
         *         that the input one
         * 
@@ -253,6 +264,12 @@ public abstract class BasicOutput {
        }
 
        protected void writeStory(Story story) throws IOException {
+               if (storyPg == null) {
+                       storyPg = new Progress(0, story.getChapters().size() + 2);
+               } else {
+                       storyPg.setMinMax(0, story.getChapters().size() + 2);
+               }
+
                String chapterNameNum = String.format("%03d", 0);
                String paragraphNumber = String.format("%04d", 0);
                imageName = paragraphNumber + "_" + chapterNameNum + ".png";
@@ -268,11 +285,28 @@ public abstract class BasicOutput {
                        InfoCover.writeInfo(targetDir, targetName, story.getMeta());
                }
 
-               writeStoryHeader(story);
+               storyPg.setProgress(1);
+
+               List<Progress> chapPgs = new ArrayList<Progress>(story.getChapters()
+                               .size());
                for (Chapter chap : story) {
-                       writeChapter(chap);
+                       chapPg = new Progress(0, chap.getParagraphs().size());
+                       storyPg.addProgress(chapPg, 1);
+                       chapPgs.add(chapPg);
+                       chapPg = null;
+               }
+
+               writeStoryHeader(story);
+               for (int i = 0; i < story.getChapters().size(); i++) {
+                       chapPg = chapPgs.get(i);
+                       writeChapter(story.getChapters().get(i));
+                       chapPg.setProgress(chapPg.getMax());
+                       chapPg = null;
                }
                writeStoryFooter(story);
+
+               storyPg.setProgress(storyPg.getMax());
+               storyPg = null;
        }
 
        protected void writeChapter(Chapter chap) throws IOException {
@@ -289,10 +323,14 @@ public abstract class BasicOutput {
                imageName = chapterNameNum + "_" + paragraphNumber + ".png";
 
                writeChapterHeader(chap);
+               int i = 1;
                for (Paragraph para : chap) {
                        paragraphNumber = String.format("%04d", num++);
                        imageName = chapterNameNum + "_" + paragraphNumber + ".png";
                        writeParagraph(para);
+                       if (chapPg != null) {
+                               chapPg.setProgress(i++);
+                       }
                }
                writeChapterFooter(chap);
        }
index 88ffc5d42d534dbbfbf1fa7d89c8de310ec4d79a..a28d4feadeeeb4502da949661923619acc16d241 100644 (file)
@@ -59,10 +59,18 @@ class LocalReader extends BasicReader {
         *             in case of I/O error
         */
        public void imprt(String luid, Progress pg) throws IOException {
+               Progress pgGetStory = new Progress();
+               Progress pgSave = new Progress();
+               if (pg != null) {
+                       pg.setMax(2);
+                       pg.addProgress(pgGetStory, 1);
+                       pg.addProgress(pgSave, 1);
+               }
+
                try {
-                       Story story = Instance.getLibrary().getStory(luid, pg);
+                       Story story = Instance.getLibrary().getStory(luid, pgGetStory);
                        if (story != null) {
-                               story = lib.save(story, luid);
+                               story = lib.save(story, luid, pgSave);
                        } else {
                                throw new IOException("Cannot find story in Library: " + luid);
                        }