Code cleanup 3 and update jexer-niki :
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderMainWindow.java
index 3b5dc5dcfff3901c96f7880c244a963e2116b3a8..f0765809adb67b303eea69a1a07d3598bd8f3223 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.fanfix.reader;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -7,15 +8,14 @@ import jexer.TAction;
 import jexer.TCommand;
 import jexer.TKeypress;
 import jexer.TList;
-import jexer.TRadioGroup;
-import jexer.TTreeItem;
-import jexer.TTreeView;
 import jexer.TWindow;
+import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.library.BasicLibrary;
 
 /**
  * The library window, that will list all the (filtered) stories available in
- * this {@link Library}.
+ * this {@link BasicLibrary}.
  * 
  * @author niki
  */
@@ -23,43 +23,23 @@ class TuiReaderMainWindow extends TWindow {
        private TList list;
        private List<MetaData> listKeys;
        private List<String> listItems;
-       private TuiReaderApplication reader;
+       private Reader reader;
 
        /**
-        * Create a new {@link TuiReaderMainWindow} with the given stories in the
+        * Create a new {@link TuiReaderMainWindow} with the given story in the
         * list.
         * 
         * @param reader
         *            the reader and main application
-        * @param metas
-        *            the stories to display
-        */
-       public TuiReaderMainWindow(TuiReaderApplication reader, List<MetaData> metas) {
-               this(reader);
-               setMetas(metas);
-       }
-
-       /**
-        * Create a new {@link TuiReaderMainWindow} with only the given
-        * {@link MetaData} in the list, and open this {@link MetaData} at the given
-        * chapter.
-        * 
-        * @param reader
-        *            the reader and main application
         * @param meta
         *            the story to display
-        * @param chapter
-        *            the chapter to open
         */
-       public TuiReaderMainWindow(TuiReaderApplication reader, MetaData meta,
-                       int chapter) {
+       public TuiReaderMainWindow(TuiReaderApplication reader, MetaData meta) {
                this(reader);
 
                List<MetaData> metas = new ArrayList<MetaData>();
                metas.add(meta);
                setMetas(metas);
-
-               reader.open(meta, chapter);
        }
 
        /**
@@ -70,7 +50,7 @@ class TuiReaderMainWindow extends TWindow {
         */
        public TuiReaderMainWindow(TuiReaderApplication reader) {
                // Construct a demo window. X and Y don't matter because it will be
-               // centered on screen.
+               // centred on screen.
                super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE
                                | UNCLOSABLE);
 
@@ -93,30 +73,44 @@ class TuiReaderMainWindow extends TWindow {
                statusBar = newStatusBar("Library");
                statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
 
-               if (false) {
-                       addLabel("Label (1,1)", 1, 1);
-                       addButton("&Button (35,1)", 35, 1, new TAction() {
-                               public void DO() {
-                               }
-                       });
-                       addCheckbox(1, 2, "Checky (1,2)", false);
-                       addProgressBar(1, 3, 30, 42);
-                       TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
-                       groupy.addRadioButton("Fanfan");
-                       groupy.addRadioButton("Tulipe");
-                       addField(1, 10, 20, false, "text not fixed.");
-                       addField(1, 11, 20, true, "text fixed.");
-                       addText("20x4 Text in (12,20)", 1, 12, 20, 4);
-
-                       TTreeView tree = addTreeView(30, 5, 20, 5);
-                       TTreeItem root = new TTreeItem(tree, "expended root", true);
-                       tree.setSelected(root); // needed to allow arrow navigation without
-                                                                       // mouse-clicking before
-
-                       root.addChild("child");
-                       root.addChild("child 2").addChild("sub child");
+               // TODO: remove when not used anymore
+
+               // addLabel("Label (1,1)", 1, 1);
+               // addButton("&Button (35,1)", 35, 1, new TAction() {
+               // public void DO() {
+               // }
+               // });
+               // addCheckbox(1, 2, "Checky (1,2)", false);
+               // addProgressBar(1, 3, 30, 42);
+               // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
+               // groupy.addRadioButton("Fanfan");
+               // groupy.addRadioButton("Tulipe");
+               // addField(1, 10, 20, false, "text not fixed.");
+               // addField(1, 11, 20, true, "text fixed.");
+               // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
+               //
+               // TTreeView tree = addTreeView(30, 5, 20, 5);
+               // TTreeItem root = new TTreeItem(tree, "expended root", true);
+               // tree.setSelected(root); // needed to allow arrow navigation without
+               // // mouse-clicking before
+               //
+               // root.addChild("child");
+               // root.addChild("child 2").addChild("sub child");
+       }
 
+       /**
+        * Update the list of stories displayed in this {@link TWindow}.
+        * 
+        * @param meta
+        *            the new (unique) story to display
+        */
+       public void setMeta(MetaData meta) {
+               List<MetaData> metas = new ArrayList<MetaData>();
+               if (meta != null) {
+                       metas.add(meta);
                }
+
+               setMetas(metas);
        }
 
        /**
@@ -140,7 +134,13 @@ class TuiReaderMainWindow extends TWindow {
        }
 
        private void enterOnStory(MetaData meta) {
-               reader.open(meta);
+               try {
+                       reader.setChapter(-1);
+                       reader.setMeta(meta);
+                       reader.read();
+               } catch (IOException e) {
+                       Instance.syserr(e);
+               }
        }
 
        private String desc(MetaData meta) {