tui: fix meta/story use
authorNiki Roo <niki@nikiroo.be>
Thu, 7 Mar 2019 16:42:25 +0000 (17:42 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 7 Mar 2019 16:42:25 +0000 (17:42 +0100)
src/be/nikiroo/fanfix/reader/BasicReader.java
src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java
src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java

index 8285edb47e74266bc747662fb3bbec87b5ae5460..778b6338b108f847bea745fbe8fe04c77a0454ec 100644 (file)
@@ -91,18 +91,18 @@ public abstract class BasicReader implements Reader {
        }
 
        @Override
-       public synchronized void setMeta(URL source, Progress pg)
+       public synchronized void setMeta(URL url, Progress pg)
                        throws IOException {
-               BasicSupport support = BasicSupport.getSupport(source);
+               BasicSupport support = BasicSupport.getSupport(url);
                if (support == null) {
-                       throw new IOException("URL not supported: " + source.toString());
+                       throw new IOException("URL not supported: " + url.toString());
                }
 
                story = support.process(pg);
                if (story == null) {
                        throw new IOException(
                                        "Cannot retrieve story from external source: "
-                                                       + source.toString());
+                                                       + url.toString());
                }
 
                meta = story.getMeta();
index 56f0dc294338a0c6c1b8b0d2d63d440226df4e90..b85bb8612768a30480942ccf760d6cb5baf9d449 100644 (file)
@@ -44,10 +44,7 @@ class TuiReaderApplication extends TApplication implements Reader {
 
        private Reader reader;
        private TuiReaderMainWindow main;
-
-       private MetaData meta;
        private String source;
-       private boolean useMeta;
 
        // start reading if meta present
        public TuiReaderApplication(Reader reader, BackendType backend)
@@ -55,7 +52,10 @@ class TuiReaderApplication extends TApplication implements Reader {
                super(backend);
                init(reader);
 
-               showMain(getMeta(), null, true);
+               MetaData meta = getMeta();
+               if (meta != null) {
+                       read();
+               }
        }
 
        public TuiReaderApplication(Reader reader, String source,
@@ -63,30 +63,13 @@ class TuiReaderApplication extends TApplication implements Reader {
                super(backend);
                init(reader);
 
-               showMain(null, source, false);
+               showMain();
+               setSource(source);
        }
 
        @Override
        public void read() throws IOException {
-               MetaData meta = getMeta();
-
-               if (meta == null) {
-                       throw new IOException("No story to read");
-               }
-
-               // TODO: open in editor + external option
-               if (!meta.isImageDocument()) {
-                       TWindow window = new TuiReaderStoryWindow(this, getLibrary(), meta,
-                                       getChapter());
-                       window.maximize();
-               } else {
-                       try {
-                               openExternal(getLibrary(), meta.getLuid());
-                       } catch (IOException e) {
-                               messageBox("Error when trying to open the story",
-                                               e.getMessage(), TMessageBox.Type.OK);
-                       }
-               }
+               read(getStory(null));
        }
 
        @Override
@@ -139,6 +122,25 @@ class TuiReaderApplication extends TApplication implements Reader {
                reader.setChapter(chapter);
        }
 
+       public void read(Story story) throws IOException {
+               if (story == null) {
+                       throw new IOException("No story to read");
+               }
+
+               // TODO: open in editor + external option
+               if (!story.getMeta().isImageDocument()) {
+                       TWindow window = new TuiReaderStoryWindow(this, story, getChapter());
+                       window.maximize();
+               } else {
+                       try {
+                               openExternal(getLibrary(), story.getMeta().getLuid());
+                       } catch (IOException e) {
+                               messageBox("Error when trying to open the story",
+                                               e.getMessage(), TMessageBox.Type.OK);
+                       }
+               }
+       }
+
        /**
         * Set the default status bar when this window appear.
         * <p>
@@ -159,13 +161,7 @@ class TuiReaderApplication extends TApplication implements Reader {
 
        }
 
-       private void showMain(MetaData meta, String source, boolean useMeta)
-                       throws IOException {
-               // TODO: thread-safety
-               this.meta = meta;
-               this.source = source;
-               this.useMeta = useMeta;
-
+       private void showMain() {
                if (main != null && main.isVisible()) {
                        main.activate();
                } else {
@@ -173,18 +169,16 @@ class TuiReaderApplication extends TApplication implements Reader {
                                main.close();
                        }
                        main = new TuiReaderMainWindow(this);
-                       if (useMeta) {
-                               main.setMeta(meta);
-                               if (meta != null) {
-                                       read();
-                               }
-                       } else {
-                               main.setSource(source);
-                       }
                        main.maximize();
                }
        }
 
+       private void setSource(String source) {
+               this.source = source;
+               showMain();
+               main.setSource(source);
+       }
+
        private void init(Reader reader) {
                this.reader = reader;
 
@@ -222,6 +216,20 @@ class TuiReaderApplication extends TApplication implements Reader {
                switch (menu.getId()) {
                case MENU_EXIT:
                        close(this);
+                       return true;
+               case MENU_OPEN:
+                       String openfile = null;
+                       try {
+                               openfile = fileOpenBox(".");
+                               reader.setMeta(BasicReader.getUrl(openfile), null);
+                               read();
+                       } catch (IOException e) {
+                               // TODO: i18n
+                               error("Fail to open file"
+                                               + (openfile == null ? "" : ": " + openfile),
+                                               "Import error", e);
+                       }
+
                        return true;
                case MENU_IMPORT_URL:
                        String clipboard = "";
@@ -242,11 +250,11 @@ class TuiReaderApplication extends TApplication implements Reader {
 
                        try {
                                if (!imprt(url)) {
-                                       // TODO: i18n + error
+                                       // TODO: i18n
                                        error("URK not supported: " + url, "Import error");
                                }
                        } catch (IOException e) {
-                               // TODO: i18n + error
+                               // TODO: i18n
                                error("Fail to import URL: " + url, "Import error", e);
                        }
 
@@ -256,24 +264,19 @@ class TuiReaderApplication extends TApplication implements Reader {
                        try {
                                filename = fileOpenBox(".");
                                if (!imprt(filename)) {
-                                       // TODO: i18n + error
+                                       // TODO: i18n
                                        error("File not supported: " + filename, "Import error");
                                }
                        } catch (IOException e) {
-                               // TODO: i18n + error
+                               // TODO: i18n
                                error("Fail to import file"
                                                + (filename == null ? "" : ": " + filename),
                                                "Import error", e);
                        }
                        return true;
                case MENU_LIBRARY:
-                       try {
-                               showMain(meta, source, useMeta);
-                       } catch (IOException e) {
-                               // i18n
-                               error("Cannot show the library", "ERROR", e);
-                       }
-
+                       showMain();
+                       setSource(source);
                        return true;
                }
 
index f5a2bfa547d8804835667f5140d2bcd8f632a873..af9c472693cb6a689d0c27339e2a9ea564d68025 100644 (file)
@@ -21,7 +21,6 @@ import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.utils.StringUtils;
 
 /**
@@ -31,8 +30,6 @@ import be.nikiroo.utils.StringUtils;
  * @author niki
  */
 class TuiReaderStoryWindow extends TWindow {
-       private BasicLibrary lib;
-       private MetaData meta;
        private Story story;
        private TLabel titleField;
        private TText textField;
@@ -42,14 +39,13 @@ class TuiReaderStoryWindow extends TWindow {
        private TLabel currentChapter;
 
        // chapter: -1 for "none" (0 is desc)
-       public TuiReaderStoryWindow(TuiReaderApplication app, BasicLibrary lib,
-                       MetaData meta, int chapter) {
-               super(app, desc(meta), 0, 0, 60, 18, CENTERED | RESIZABLE);
+       public TuiReaderStoryWindow(TuiReaderApplication app, Story story,
+                       int chapter) {
+               super(app, desc(story.getMeta()), 0, 0, 60, 18, CENTERED | RESIZABLE);
 
-               this.lib = lib;
-               this.meta = meta;
+               this.story = story;
 
-               app.setStatusBar(this, desc(meta));
+               app.setStatusBar(this, desc(story.getMeta()));
 
                // last = use window background
                titleField = new TLabel(this, "    Title", 0, 1, "tlabel", false);
@@ -274,10 +270,6 @@ class TuiReaderStoryWindow extends TWindow {
        }
 
        private Story getStory() {
-               if (story == null) {
-                       // TODO: progress bar?
-                       story = lib.getStory(meta.getLuid(), null);
-               }
                return story;
        }