Code cleanup 2 (a third one is pending)
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReader.java
index e9f909da7674ffd41f580e6e59dc2ec88f27e5ad..91b62683aff9de15da1645a7ac08e62cacb7c916 100644 (file)
@@ -1,27 +1,95 @@
 package be.nikiroo.fanfix.reader;
 
 import java.io.IOException;
+import java.util.List;
 
+import jexer.TApplication;
+import jexer.TApplication.BackendType;
 import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.data.MetaData;
 
+/**
+ * This {@link Reader}is based upon the TUI widget library 'jexer'
+ * (https://github.com/klamonte/jexer/) and offer, as its name suggest, a Text
+ * User Interface.
+ * <p>
+ * It is expected to be on par with the GUI version.
+ * 
+ * @author niki
+ */
 class TuiReader extends BasicReader {
-       public void read() throws IOException {
-               if (getStory() == null) {
-                       throw new IOException("No story to read");
+       /**
+        * Will detect the backend to use.
+        * <p>
+        * Swing is the default backend on Windows and MacOS while evreything else
+        * will use XTERM unless explicitly overridden by <tt>jexer.Swing</tt> =
+        * <tt>true</tt> or <tt>false</tt>.
+        * 
+        * @return the backend to use
+        */
+       private static BackendType guessBackendType() {
+               // TODO: allow a config option to force one or the other?
+               TApplication.BackendType backendType = TApplication.BackendType.XTERM;
+               if (System.getProperty("os.name").startsWith("Windows")) {
+                       backendType = TApplication.BackendType.SWING;
+               }
+
+               if (System.getProperty("os.name").startsWith("Mac")) {
+                       backendType = TApplication.BackendType.SWING;
+               }
+
+               if (System.getProperty("jexer.Swing") != null) {
+                       if (System.getProperty("jexer.Swing", "false").equals("true")) {
+                               backendType = TApplication.BackendType.SWING;
+                       } else {
+                               backendType = TApplication.BackendType.XTERM;
+                       }
                }
 
-               open(getLibrary(), getStory().getMeta().getLuid());
+               return backendType;
        }
 
        public void read(int chapter) throws IOException {
-               // TODO: show a special page?
-               read();
+               if (getStory() == null) {
+                       throw new IOException("No story to read");
+               }
+
+               start(getStory().getMeta(), chapter);
        }
 
        public void browse(String source) {
+               start(getLibrary().getListBySource(source));
+       }
+
+       /**
+        * Start the application with the given stories.
+        * 
+        * @param metas
+        *            the stories to display
+        */
+       private void start(List<MetaData> metas) {
+               try {
+                       TuiReaderApplication app = new TuiReaderApplication(metas, this,
+                                       guessBackendType());
+                       new Thread(app).start();
+               } catch (Exception e) {
+                       Instance.syserr(e);
+               }
+       }
+
+       /**
+        * Start the application with the given {@link MetaData} at the given open
+        * chapter.
+        * 
+        * @param meta
+        *            the story to display
+        * @param chapter
+        *            the chapter to open
+        */
+       private void start(MetaData meta, int chapter) {
                try {
-                       TuiReaderApplication app = new TuiReaderApplication(getLibrary()
-                                       .getListBySource(source), this);
+                       TuiReaderApplication app = new TuiReaderApplication(meta, chapter,
+                                       this, guessBackendType());
                        new Thread(app).start();
                } catch (Exception e) {
                        Instance.syserr(e);