Code cleanup: Libraries/Readers
authorNiki Roo <niki@nikiroo.be>
Mon, 3 Jul 2017 17:54:47 +0000 (19:54 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 3 Jul 2017 17:54:47 +0000 (19:54 +0200)
- Move libraries into a new package
- Update readers to have a BasicReader and an interface

17 files changed:
src/be/nikiroo/fanfix/Instance.java
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/library/BasicLibrary.java [moved from src/be/nikiroo/fanfix/BasicLibrary.java with 99% similarity]
src/be/nikiroo/fanfix/library/LocalLibrary.java [moved from src/be/nikiroo/fanfix/LocalLibrary.java with 99% similarity]
src/be/nikiroo/fanfix/library/RemoteLibrary.java [moved from src/be/nikiroo/fanfix/RemoteLibrary.java with 98% similarity]
src/be/nikiroo/fanfix/library/RemoteLibraryServer.java [moved from src/be/nikiroo/fanfix/RemoteLibraryServer.java with 94% similarity]
src/be/nikiroo/fanfix/library/package-info.java [new file with mode: 0644]
src/be/nikiroo/fanfix/reader/BasicReader.java
src/be/nikiroo/fanfix/reader/CliReader.java
src/be/nikiroo/fanfix/reader/GuiReader.java
src/be/nikiroo/fanfix/reader/GuiReaderBook.java
src/be/nikiroo/fanfix/reader/GuiReaderFrame.java
src/be/nikiroo/fanfix/reader/GuiReaderGroup.java
src/be/nikiroo/fanfix/reader/Reader.java [new file with mode: 0644]
src/be/nikiroo/fanfix/reader/TuiReader.java
src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java
src/be/nikiroo/fanfix/test/LibraryTest.java

index f9060f440b5e2cc9fecd95090508bfd45d692291..2231414c9b53f310838cbdbfb230779cc2703fbb 100644 (file)
@@ -9,6 +9,8 @@ import be.nikiroo.fanfix.bundles.ConfigBundle;
 import be.nikiroo.fanfix.bundles.StringIdBundle;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.bundles.UiConfigBundle;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.resources.Bundles;
index a05df434fb81fdf8edbc57ce391ec78de940678b..ee122bd508349e461544055fa818a037da9e3fed 100644 (file)
@@ -8,10 +8,14 @@ import java.net.URL;
 import be.nikiroo.fanfix.bundles.StringId;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.LocalLibrary;
+import be.nikiroo.fanfix.library.RemoteLibrary;
+import be.nikiroo.fanfix.library.RemoteLibraryServer;
 import be.nikiroo.fanfix.output.BasicOutput;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.reader.BasicReader;
-import be.nikiroo.fanfix.reader.BasicReader.ReaderType;
+import be.nikiroo.fanfix.reader.Reader;
+import be.nikiroo.fanfix.reader.Reader.ReaderType;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 import be.nikiroo.utils.Progress;
@@ -392,7 +396,7 @@ public class Main {
         */
        private static int read(String story, String chapString, boolean library) {
                try {
-                       BasicReader reader = BasicReader.getReader();
+                       Reader reader = BasicReader.getReader();
                        if (library) {
                                reader.setStory(story, null);
                        } else {
similarity index 99%
rename from src/be/nikiroo/fanfix/BasicLibrary.java
rename to src/be/nikiroo/fanfix/library/BasicLibrary.java
index 75cc8a6f156c051e123a4e7386c0e83fe00006ef..9ac16ea2a45ba16e058d81f6d950b85e54bc8ea6 100644 (file)
@@ -1,4 +1,4 @@
-package be.nikiroo.fanfix;
+package be.nikiroo.fanfix.library;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
@@ -8,6 +8,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput;
similarity index 99%
rename from src/be/nikiroo/fanfix/LocalLibrary.java
rename to src/be/nikiroo/fanfix/library/LocalLibrary.java
index 35f13f05f0437546c6f42ba99181c221095fd4b7..e4dc5e98bce53547c4c7d5ad2cee833e0e95bee0 100644 (file)
@@ -1,4 +1,4 @@
-package be.nikiroo.fanfix;
+package be.nikiroo.fanfix.library;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
@@ -9,6 +9,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
similarity index 98%
rename from src/be/nikiroo/fanfix/RemoteLibrary.java
rename to src/be/nikiroo/fanfix/library/RemoteLibrary.java
index 5c111ca1a24cf718833503015c7b83e0ae7cba47..6c1e56f5d22f4ceee61ab260b722352efe583225 100644 (file)
@@ -1,4 +1,4 @@
-package be.nikiroo.fanfix;
+package be.nikiroo.fanfix.library;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
@@ -6,6 +6,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
similarity index 94%
rename from src/be/nikiroo/fanfix/RemoteLibraryServer.java
rename to src/be/nikiroo/fanfix/library/RemoteLibraryServer.java
index e47a5912c9d4f6ebad55a6fba727ee38134c52c0..566c70fa69a572043ea6749ae5361b05b6ac4b73 100644 (file)
@@ -1,10 +1,9 @@
-package be.nikiroo.fanfix;
+package be.nikiroo.fanfix.library;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.List;
-import java.util.Map;
 
+import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.ConnectActionServer;
diff --git a/src/be/nikiroo/fanfix/library/package-info.java b/src/be/nikiroo/fanfix/library/package-info.java
new file mode 100644 (file)
index 0000000..1bb63ea
--- /dev/null
@@ -0,0 +1,9 @@
+/**
+ * This package offer a Libraries to store stories into.
+ * <p>
+ * It currently has a local library and a remote one, as well as the required 
+ * remote server. 
+ * 
+ * @author niki
+ */
+package be.nikiroo.fanfix.library;
\ No newline at end of file
index baf3c7dede9ebab876d7a4b3158d369e8224331d..cbd4e0308a4584895b805d7ed3ac8bbdaf2c3732 100644 (file)
@@ -6,13 +6,13 @@ import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 
-import be.nikiroo.fanfix.BasicLibrary;
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.LocalLibrary;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.serial.SerialUtils;
@@ -24,49 +24,12 @@ import be.nikiroo.utils.serial.SerialUtils;
  * 
  * @author niki
  */
-public abstract class BasicReader {
-       /**
-        * A type of {@link BasicReader}.
-        * 
-        * @author niki
-        */
-       public enum ReaderType {
-               /** Simple reader that outputs everything on the console */
-               CLI,
-               /** Reader that starts local programs to handle the stories */
-               GUI,
-               /** A text (UTF-8) reader with menu and text windows */
-               TUI,
-
-               ;
-
-               /**
-                * Return the full class name of a type that implements said
-                * {@link ReaderType}.
-                * 
-                * @return the class name
-                */
-               public String getTypeName() {
-                       String pkg = "be.nikiroo.fanfix.reader.";
-                       switch (this) {
-                       case CLI:
-                               return pkg + "CliReader";
-                       case TUI:
-                               return pkg + "TuiReader";
-                       case GUI:
-                               return pkg + "GuiReader";
-                       }
-
-                       return null;
-               }
-       }
-
+public abstract class BasicReader implements Reader {
        private static BasicLibrary defaultLibrary = Instance.getLibrary();
        private static ReaderType defaultType = ReaderType.GUI;
 
        private BasicLibrary lib;
        private Story story;
-       private ReaderType type;
 
        /**
         * Take the default reader type configuration from the config file.
@@ -83,43 +46,10 @@ public abstract class BasicReader {
                }
        }
 
-       /**
-        * The type of this reader.
-        * 
-        * @return the type
-        */
-       public ReaderType getType() {
-               return type;
-       }
-
-       /**
-        * The type of this reader.
-        * 
-        * @param type
-        *            the new type
-        * 
-        * @return the type
-        */
-       protected BasicReader setType(ReaderType type) {
-               this.type = type;
-               return this;
-       }
-
-       /**
-        * Return the current {@link Story}.
-        * 
-        * @return the {@link Story}
-        */
        public Story getStory() {
                return story;
        }
 
-       /**
-        * The {@link LocalLibrary} to load the stories from (by default, takes the
-        * default {@link LocalLibrary}).
-        * 
-        * @return the {@link LocalLibrary}
-        */
        public BasicLibrary getLibrary() {
                if (lib == null) {
                        lib = defaultLibrary;
@@ -128,29 +58,10 @@ public abstract class BasicReader {
                return lib;
        }
 
-       /**
-        * Change the {@link LocalLibrary} that will be managed by this
-        * {@link BasicReader}.
-        * 
-        * @param lib
-        *            the new {@link LocalLibrary}
-        */
        public void setLibrary(LocalLibrary lib) {
                this.lib = lib;
        }
 
-       /**
-        * Create a new {@link BasicReader} for a {@link Story} in the
-        * {@link LocalLibrary}.
-        * 
-        * @param luid
-        *            the {@link Story} ID
-        * @param pg
-        *            the optional progress reporter
-        * 
-        * @throws IOException
-        *             in case of I/O error
-        */
        public void setStory(String luid, Progress pg) throws IOException {
                story = lib.getStory(luid, pg);
                if (story == null) {
@@ -158,17 +69,6 @@ public abstract class BasicReader {
                }
        }
 
-       /**
-        * Create a new {@link BasicReader} for an external {@link Story}.
-        * 
-        * @param source
-        *            the {@link Story} {@link URL}
-        * @param pg
-        *            the optional progress reporter
-        * 
-        * @throws IOException
-        *             in case of I/O error
-        */
        public void setStory(URL source, Progress pg) throws IOException {
                BasicSupport support = BasicSupport.getSupport(source);
                if (support == null) {
@@ -184,37 +84,6 @@ public abstract class BasicReader {
                }
        }
 
-       /**
-        * Start the {@link Story} Reading.
-        * 
-        * @throws IOException
-        *             in case of I/O error or if the {@link Story} was not
-        *             previously set
-        */
-       public abstract void read() throws IOException;
-
-       /**
-        * Read the selected chapter (starting at 1).
-        * 
-        * @param chapter
-        *            the chapter
-        * 
-        * @throws IOException
-        *             in case of I/O error or if the {@link Story} was not
-        *             previously set
-        */
-       public abstract void read(int chapter) throws IOException;
-
-       /**
-        * Start the reader in browse mode for the given source (or pass NULL for
-        * all sources).
-        * 
-        * @param source
-        *            the type of {@link Story} to take into account, or NULL for
-        *            all
-        */
-       public abstract void browse(String source);
-
        /**
         * Return a new {@link BasicReader} ready for use if one is configured.
         * <p>
@@ -222,11 +91,11 @@ public abstract class BasicReader {
         * 
         * @return a {@link BasicReader}, or NULL if none configured
         */
-       public static BasicReader getReader() {
+       public static Reader getReader() {
                try {
                        if (defaultType != null) {
-                               return ((BasicReader) SerialUtils.createObject(defaultType
-                                               .getTypeName())).setType(defaultType);
+                               return (Reader) SerialUtils.createObject(defaultType
+                                               .getTypeName());
                        }
                } catch (Exception e) {
                        Instance.syserr(new Exception("Cannot create a reader of type: "
index 7ac69e85d7ce0e14d8b0c045569f7c7fbeeb0a52..de55bf9b06a8a033662037c14cb412f1615844e1 100644 (file)
@@ -18,7 +18,6 @@ import be.nikiroo.fanfix.data.Story;
  * @author niki
  */
 class CliReader extends BasicReader {
-       @Override
        public void read() throws IOException {
                if (getStory() == null) {
                        throw new IOException("No story to read");
@@ -57,7 +56,6 @@ class CliReader extends BasicReader {
                }
        }
 
-       @Override
        public void read(int chapter) throws IOException {
                if (getStory() == null) {
                        throw new IOException("No story to read");
@@ -77,7 +75,6 @@ class CliReader extends BasicReader {
                }
        }
 
-       @Override
        public void browse(String source) {
                List<MetaData> stories;
                stories = getLibrary().getListBySource(source);
index cdf80d0c9fda7b3d02b0d4b7d045a011f877b551..06b3473c74c95f9abe7035f78852b3a1604938f4 100644 (file)
@@ -13,10 +13,10 @@ import javax.swing.event.HyperlinkEvent;
 import javax.swing.event.HyperlinkListener;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.LocalLibrary;
 import be.nikiroo.fanfix.VersionCheck;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
@@ -69,7 +69,6 @@ class GuiReader extends BasicReader {
                localLibrary = new LocalLibrary(dir, text, images);
        }
 
-       @Override
        public void read() throws IOException {
                if (getStory() == null) {
                        throw new IOException("No story to read");
@@ -78,7 +77,6 @@ class GuiReader extends BasicReader {
                open(getStory().getMeta().getLuid(), null);
        }
 
-       @Override
        public void read(int chapter) throws IOException {
                // TODO: show a special page?
                read();
@@ -105,7 +103,7 @@ class GuiReader extends BasicReader {
                }
 
                try {
-                       Story story = Instance.getLibrary().getStory(luid, pgGetStory);
+                       Story story = getLibrary().getStory(luid, pgGetStory);
                        if (story != null) {
                                story = localLibrary.save(story, luid, pgSave);
                        } else {
@@ -131,7 +129,6 @@ class GuiReader extends BasicReader {
                return localLibrary.getInfo(luid) != null;
        }
 
-       @Override
        public void browse(String type) {
                // TODO: improve presentation of update message
                final VersionCheck updates = VersionCheck.check();
@@ -189,8 +186,7 @@ class GuiReader extends BasicReader {
                                        }
                                }
 
-                               new GuiReaderFrame(GuiReader.this, typeFinal)
-                                               .setVisible(true);
+                               new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
                        }
                });
        }
@@ -208,7 +204,7 @@ class GuiReader extends BasicReader {
        void delete(String luid) {
                try {
                        localLibrary.delete(luid);
-                       Instance.getLibrary().delete(luid);
+                       getLibrary().delete(luid);
                } catch (IOException e) {
                        Instance.syserr(e);
                }
@@ -228,7 +224,7 @@ class GuiReader extends BasicReader {
        void changeType(String luid, String newType) {
                try {
                        localLibrary.changeSource(luid, newType, null);
-                       Instance.getLibrary().changeSource(luid, newType, null);
+                       getLibrary().changeSource(luid, newType, null);
                } catch (IOException e) {
                        Instance.syserr(e);
                }
index e6fe91b4d84ed95def7fe13ba48b52850ceb9686..2a4266de25bab41c676364cb520b05b614850cf4 100644 (file)
@@ -93,12 +93,15 @@ class GuiReaderBook extends JPanel {
        private Date lastClick;
 
        private List<BookActionListener> listeners;
+       private Reader reader;
        private MetaData meta;
        private boolean cached;
 
        /**
         * Create a new {@link GuiReaderBook} item for the given {@link Story}.
         * 
+        * @param reader
+        *            the associated reader
         * @param meta
         *            the story {@link MetaData}
         * @param cached
@@ -106,7 +109,9 @@ class GuiReaderBook extends JPanel {
         * @param seeWordCount
         *            TRUE to see word counts, FALSE to see authors
         */
-       public GuiReaderBook(MetaData meta, boolean cached, boolean seeWordCount) {
+       public GuiReaderBook(Reader reader, MetaData meta, boolean cached,
+                       boolean seeWordCount) {
+               this.reader = reader;
                this.cached = cached;
                this.meta = meta;
 
@@ -375,7 +380,7 @@ class GuiReaderBook extends JPanel {
 
                if (resizedImage == null) {
                        try {
-                               BufferedImage cover = Instance.getLibrary().getCover(
+                               BufferedImage cover = reader.getLibrary().getCover(
                                                meta.getLuid());
 
                                resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH,
index 26ab23f3c83d09fc3737edb2114f805ae290e1a5..88eccf7885a8ebf715fafb548d691c52a9bff476 100644 (file)
@@ -34,11 +34,11 @@ import javax.swing.filechooser.FileFilter;
 import javax.swing.filechooser.FileNameExtensionFilter;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.LocalLibrary;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.reader.GuiReaderBook.BookActionListener;
 import be.nikiroo.utils.Progress;
@@ -125,7 +125,7 @@ class GuiReaderFrame extends JFrame {
                final String typeF = type;
                outOfUi(pg, new Runnable() {
                        public void run() {
-                               Instance.getLibrary().refresh(false, pg);
+                               GuiReaderFrame.this.reader.getLibrary().refresh(false, pg);
                                invalidate();
                                setJMenuBar(createMenu());
                                addBookPane(typeF, true);
@@ -151,13 +151,13 @@ class GuiReaderFrame extends JFrame {
        private void addBookPane(String value, boolean type) {
                if (value == null) {
                        if (type) {
-                               for (String tt : Instance.getLibrary().getSources()) {
+                               for (String tt : reader.getLibrary().getSources()) {
                                        if (tt != null) {
                                                addBookPane(tt, type);
                                        }
                                }
                        } else {
-                               for (String tt : Instance.getLibrary().getAuthors()) {
+                               for (String tt : reader.getLibrary().getAuthors()) {
                                        if (tt != null) {
                                                addBookPane(tt, type);
                                        }
@@ -220,13 +220,13 @@ class GuiReaderFrame extends JFrame {
         */
        private void refreshBooks() {
                for (GuiReaderGroup group : booksByType.keySet()) {
-                       List<MetaData> stories = Instance.getLibrary().getListBySource(
+                       List<MetaData> stories = reader.getLibrary().getListBySource(
                                        booksByType.get(group));
                        group.refreshBooks(stories, words);
                }
 
                for (GuiReaderGroup group : booksByAuthor.keySet()) {
-                       List<MetaData> stories = Instance.getLibrary().getListByAuthor(
+                       List<MetaData> stories = reader.getLibrary().getListByAuthor(
                                        booksByAuthor.get(group));
                        group.refreshBooks(stories, words);
                }
@@ -312,7 +312,7 @@ class GuiReaderFrame extends JFrame {
                JMenu sources = new JMenu("Sources");
                sources.setMnemonic(KeyEvent.VK_S);
 
-               List<String> tt = Instance.getLibrary().getSources();
+               List<String> tt = reader.getLibrary().getSources();
                tt.add(0, null);
                for (final String type : tt) {
                        JMenuItem item = new JMenuItem(type == null ? "All" : type);
@@ -335,7 +335,7 @@ class GuiReaderFrame extends JFrame {
                JMenu authors = new JMenu("Authors");
                authors.setMnemonic(KeyEvent.VK_A);
 
-               List<String> aa = Instance.getLibrary().getAuthors();
+               List<String> aa = reader.getLibrary().getAuthors();
                aa.add(0, null);
                for (final String author : aa) {
                        JMenuItem item = new JMenuItem(author == null ? "All"
@@ -463,7 +463,7 @@ class GuiReaderFrame extends JFrame {
                                                outOfUi(pg, new Runnable() {
                                                        public void run() {
                                                                try {
-                                                                       Instance.getLibrary().export(
+                                                                       reader.getLibrary().export(
                                                                                        selectedBook.getMeta().getLuid(),
                                                                                        type, path, pg);
                                                                } catch (IOException e) {
@@ -542,7 +542,7 @@ class GuiReaderFrame extends JFrame {
 
                List<String> types = new ArrayList<String>();
                types.add(null);
-               types.addAll(Instance.getLibrary().getSources());
+               types.addAll(reader.getLibrary().getSources());
 
                for (String type : types) {
                        JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
@@ -785,8 +785,7 @@ class GuiReaderFrame extends JFrame {
                        public void run() {
                                Exception ex = null;
                                try {
-                                       Instance.getLibrary().imprt(BasicReader.getUrl(url),
-                                                       pgImprt);
+                                       reader.getLibrary().imprt(BasicReader.getUrl(url), pgImprt);
                                } catch (IOException e) {
                                        ex = e;
                                }
index 2161de563aec2e25265c3ed66636a06a242167c4..ca3768d92a616a08ee714d9ca9419963ef6cc9e7 100644 (file)
@@ -33,15 +33,14 @@ public class GuiReaderGroup extends JPanel {
         * Create a new {@link GuiReaderGroup}.
         * 
         * @param reader
-        *            the {@link GuiReaderBook} used to probe some information
-        *            about the stories
+        *            the {@link GuiReaderBook} used to probe some information about
+        *            the stories
         * @param title
         *            the title of this group
         * @param backgroundColor
         *            the background colour to use (or NULL for default)
         */
-       public GuiReaderGroup(GuiReader reader, String title,
-                       Color backgroundColor) {
+       public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) {
                this.reader = reader;
                this.backgroundColor = backgroundColor;
 
@@ -101,7 +100,7 @@ public class GuiReaderGroup extends JPanel {
 
                if (stories != null) {
                        for (MetaData meta : stories) {
-                               GuiReaderBook book = new GuiReaderBook(meta,
+                               GuiReaderBook book = new GuiReaderBook(reader, meta,
                                                reader.isCached(meta.getLuid()), seeWordcount);
                                if (backgroundColor != null) {
                                        book.setBackground(backgroundColor);
@@ -116,8 +115,7 @@ public class GuiReaderGroup extends JPanel {
                                                }
                                        }
 
-                                       public void popupRequested(GuiReaderBook book,
-                                                       MouseEvent e) {
+                                       public void popupRequested(GuiReaderBook book, MouseEvent e) {
                                        }
 
                                        public void action(GuiReaderBook book) {
diff --git a/src/be/nikiroo/fanfix/reader/Reader.java b/src/be/nikiroo/fanfix/reader/Reader.java
new file mode 100644 (file)
index 0000000..d728578
--- /dev/null
@@ -0,0 +1,130 @@
+package be.nikiroo.fanfix.reader;
+
+import java.io.IOException;
+import java.net.URL;
+
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.LocalLibrary;
+import be.nikiroo.utils.Progress;
+
+public interface Reader {
+       /**
+        * A type of {@link BasicReader}.
+        * 
+        * @author niki
+        */
+       public enum ReaderType {
+               /** Simple reader that outputs everything on the console */
+               CLI,
+               /** Reader that starts local programs to handle the stories */
+               GUI,
+               /** A text (UTF-8) reader with menu and text windows */
+               TUI,
+
+               ;
+
+               /**
+                * Return the full class name of a type that implements said
+                * {@link ReaderType}.
+                * 
+                * @return the class name
+                */
+               public String getTypeName() {
+                       String pkg = "be.nikiroo.fanfix.reader.";
+                       switch (this) {
+                       case CLI:
+                               return pkg + "CliReader";
+                       case TUI:
+                               return pkg + "TuiReader";
+                       case GUI:
+                               return pkg + "GuiReader";
+                       }
+
+                       return null;
+               }
+       };
+
+       /**
+        * Return the current {@link Story}.
+        * 
+        * @return the {@link Story}
+        */
+       public Story getStory();
+
+       /**
+        * The {@link LocalLibrary} to load the stories from (by default, takes the
+        * default {@link LocalLibrary}).
+        * 
+        * @return the {@link LocalLibrary}
+        */
+       public BasicLibrary getLibrary();
+
+       /**
+        * Change the {@link LocalLibrary} that will be managed by this
+        * {@link BasicReader}.
+        * 
+        * @param lib
+        *            the new {@link LocalLibrary}
+        */
+       public void setLibrary(LocalLibrary lib);
+
+       /**
+        * Create a new {@link BasicReader} for a {@link Story} in the
+        * {@link LocalLibrary}.
+        * 
+        * @param luid
+        *            the {@link Story} ID
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public void setStory(String luid, Progress pg) throws IOException;
+
+       /**
+        * Create a new {@link BasicReader} for an external {@link Story}.
+        * 
+        * @param source
+        *            the {@link Story} {@link URL}
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public void setStory(URL source, Progress pg) throws IOException;
+
+       /**
+        * Start the {@link Story} Reading.
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the {@link Story} was not
+        *             previously set
+        */
+       public void read() throws IOException;
+
+       /**
+        * Read the selected chapter (starting at 1).
+        * 
+        * @param chapter
+        *            the chapter
+        * 
+        * @throws IOException
+        *             in case of I/O error or if the {@link Story} was not
+        *             previously set
+        */
+       public void read(int chapter) throws IOException;
+
+       /**
+        * Start the reader in browse mode for the given source (or pass NULL for
+        * all sources).
+        * 
+        * @param source
+        *            the type of {@link Story} to take into account, or NULL for
+        *            all
+        */
+       public void browse(String source);
+
+}
index 98130a989b8b0fa505f307dddd7f958bb9bdeb7f..e9f909da7674ffd41f580e6e59dc2ec88f27e5ad 100644 (file)
@@ -5,7 +5,6 @@ import java.io.IOException;
 import be.nikiroo.fanfix.Instance;
 
 class TuiReader extends BasicReader {
-       @Override
        public void read() throws IOException {
                if (getStory() == null) {
                        throw new IOException("No story to read");
@@ -14,13 +13,11 @@ class TuiReader extends BasicReader {
                open(getLibrary(), getStory().getMeta().getLuid());
        }
 
-       @Override
        public void read(int chapter) throws IOException {
                // TODO: show a special page?
                read();
        }
 
-       @Override
        public void browse(String source) {
                try {
                        TuiReaderApplication app = new TuiReaderApplication(getLibrary()
index 2b1cace8ba817462473e3bb067ce77010e044d78..71b5b5e38374901a68a2aa768ae4a64743020e43 100644 (file)
@@ -12,11 +12,11 @@ import jexer.TLabel;
 import jexer.TText;
 import jexer.TWindow;
 import jexer.event.TResizeEvent;
-import be.nikiroo.fanfix.BasicLibrary;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
 
 class TuiReaderStoryWindow extends TWindow {
        private BasicLibrary lib;
index f9d5299a575d7c7dc298ae1a631143b9c482d49b..43ca0879d60ed61fe1d7fc072820d03022bfd6ba 100644 (file)
@@ -4,12 +4,12 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
-import be.nikiroo.fanfix.BasicLibrary;
-import be.nikiroo.fanfix.LocalLibrary;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.test.TestCase;