From bc2ea776b67cabcbdcbbc6d8a4e2df1aafa9101a Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 5 Jul 2017 21:18:49 +0200 Subject: [PATCH] Code cleanup 3 and update jexer-niki : - The readers are now ~ clean - jexer-0.0.4-niki0 was missing a public modifiers, causing build problems --- ...rces.jar => jexer-0.0.4-niki1-sources.jar} | Bin 248583 -> 248583 bytes src/be/nikiroo/fanfix/Main.java | 7 +- src/be/nikiroo/fanfix/package-info.java | 3 +- src/be/nikiroo/fanfix/reader/BasicReader.java | 44 ++++++-- src/be/nikiroo/fanfix/reader/CliReader.java | 33 +++--- src/be/nikiroo/fanfix/reader/GuiReader.java | 21 ++-- .../nikiroo/fanfix/reader/GuiReaderFrame.java | 2 +- src/be/nikiroo/fanfix/reader/Reader.java | 72 +++++++++---- src/be/nikiroo/fanfix/reader/TuiReader.java | 38 ++----- .../fanfix/reader/TuiReaderApplication.java | 99 ++++++++++------- .../fanfix/reader/TuiReaderMainWindow.java | 102 +++++++++--------- .../fanfix/reader/TuiReaderStoryWindow.java | 2 +- 12 files changed, 236 insertions(+), 187 deletions(-) rename libs/{jexer-0.0.4-niki0-sources.jar => jexer-0.0.4-niki1-sources.jar} (97%) diff --git a/libs/jexer-0.0.4-niki0-sources.jar b/libs/jexer-0.0.4-niki1-sources.jar similarity index 97% rename from libs/jexer-0.0.4-niki0-sources.jar rename to libs/jexer-0.0.4-niki1-sources.jar index f84389747c65f934560fb2ec8cb21bc57777560c..c213e75dbfb1e7051af814e2c94a740f063fcfe4 100644 GIT binary patch delta 4064 zcmV<64o4LSo6spXMMg9AgT<$=(Z&;geNJgDW9 z@dg)%fdK)BfdK-yfdK=|6$3-4<+o550|g3ys~s^5l93{}q0f-d&-2j+fBsBTQjQv6 z>V>XfI3V*drAXKXd&$8r-4ItC;!4gaKjZTGtjJR(K@M|%p+X9Js_hAkV^43Un<>+( z3X%Vzj@mf-yQ2W(kTe#NY_es~o)p~)vFNI^Yc7{bS%s>g3xQY^X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m>Jur}$cmMHEt| zrc`YCP!Y*ZA1RjiVCoB|FkZ0B6Q=Q(SSBn%+YY=_zQu>E=oRY(7-BuU$zA&%?Rr-{Xj?Gk1D z=X&%p*j0#R`Q&Pn`wr;)5XZA+qWW(2rIJ@LZC;hfF7fc`MJ}`m zL7X%QUyQeGA@h-UU+hU=RgZ?Lfg?R4y9Iw~jJI=9Xagicm=Fx?{eC&%j$J-NWB5 zx~GS|i+|s6yMO2Lyl`p%r=82;Bf8_#{?GcIpS!*D!DIU6QX)=zKbLf{2?t%;@w;B{ zrJdv$ypqt6UfZ^XU)%3DS~ z6s5R}*7iz5;|51Rj*~G&pbTI3wkzKj98$=$XMYg>1X#;x&aZW}x5YONv!+RLDRzFm zLMOC=7Y}Y};d06CTKMhqs^Pj~(ek<#JRA~ZI$7@GEilc;~5HofU|7%vegiCyY2HRFTLtkDkv$}XbkvnKw z`PvY^qM`}`iv!lnuGK|xLthml7Kpfo`hU~EtCT;Y?blR|bd2@J3`2bnb(MIL;V;p( ze0A0W0mFOt&aA(lemAC_%{dOf}6VlwuAN*Wbq~HmlPLUo7j(sedVU zXNyN}X_8?fncs#uN#dE9cp(c7arb-VdRfUQ5_BZrIUY}n&}SwMpLF=UJqB^s&?

~T>=_j&;>2gbW_UE(z2T)4~ z2o)%c>8uz40F#ga08mQ<1QY-W2$!o90TP!Gj{yp|Q!oSA5d$wl4e08mQ@2tKY=1P%ZI01N;C08mQ-0u%rg00;;O0J3r9mtn61I0LeA zYU13v>VLFBjIuLB_mm$0@21h)gX1H=yvEJ5T-6)218tQY_Qlb6)S1HA(;LFBiT V#{)D22zO`XNepub (or other) * files that you can read anywhere. *

- * It has support for a {@link be.nikiroo.fanfix.BasicLibrary} system, too. + * It has support for a {@link be.nikiroo.fanfix.library.BasicLibrary} system, + * too. * * @author niki */ diff --git a/src/be/nikiroo/fanfix/reader/BasicReader.java b/src/be/nikiroo/fanfix/reader/BasicReader.java index 2c04006..cf83788 100644 --- a/src/be/nikiroo/fanfix/reader/BasicReader.java +++ b/src/be/nikiroo/fanfix/reader/BasicReader.java @@ -29,7 +29,9 @@ public abstract class BasicReader implements Reader { private static ReaderType defaultType = ReaderType.GUI; private BasicLibrary lib; + private MetaData meta; private Story story; + private int chapter; /** * Take the default reader type configuration from the config file. @@ -46,7 +48,11 @@ public abstract class BasicReader implements Reader { } } - public Story getStory() { + public synchronized Story getStory(Progress pg) { + if (story == null) { + story = getLibrary().getStory(meta.getLuid(), pg); + } + return story; } @@ -58,18 +64,29 @@ public abstract class BasicReader implements Reader { return lib; } - public void setLibrary(LocalLibrary lib) { + public void setLibrary(BasicLibrary lib) { this.lib = lib; } - public void setStory(String luid, Progress pg) throws IOException { - story = getLibrary().getStory(luid, pg); - if (story == null) { + public MetaData getMeta() { + return meta; + } + + public synchronized void setMeta(MetaData meta) throws IOException { + setMeta(meta == null ? null : meta.getLuid()); // must check the library + } + + public synchronized void setMeta(String luid) throws IOException { + story = null; + meta = getLibrary().getInfo(luid); + + if (meta == null) { throw new IOException("Cannot retrieve story from library: " + luid); } } - public void setStory(URL source, Progress pg) throws IOException { + public synchronized void setMeta(URL source, Progress pg) + throws IOException { BasicSupport support = BasicSupport.getSupport(source); if (support == null) { throw new IOException("URL not supported: " + source.toString()); @@ -80,12 +97,17 @@ public abstract class BasicReader implements Reader { throw new IOException( "Cannot retrieve story from external source: " + source.toString()); - } + + meta = story.getMeta(); + } + + public int getChapter() { + return chapter; } - public void read() throws IOException { - read(-1); + public void setChapter(int chapter) { + this.chapter = chapter; } /** @@ -110,7 +132,7 @@ public abstract class BasicReader implements Reader { } /** - * The default {@link ReaderType} used when calling + * The default {@link Reader.ReaderType} used when calling * {@link BasicReader#getReader()}. * * @return the default type @@ -120,7 +142,7 @@ public abstract class BasicReader implements Reader { } /** - * The default {@link ReaderType} used when calling + * The default {@link Reader.ReaderType} used when calling * {@link BasicReader#getReader()}. * * @param defaultType diff --git a/src/be/nikiroo/fanfix/reader/CliReader.java b/src/be/nikiroo/fanfix/reader/CliReader.java index de55bf9..8a45885 100644 --- a/src/be/nikiroo/fanfix/reader/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/CliReader.java @@ -19,24 +19,23 @@ import be.nikiroo.fanfix.data.Story; */ class CliReader extends BasicReader { public void read() throws IOException { - if (getStory() == null) { + MetaData meta = getMeta(); + + if (meta == null) { throw new IOException("No story to read"); } String title = ""; String author = ""; - MetaData meta = getStory().getMeta(); - if (meta != null) { - if (meta.getTitle() != null) { - title = meta.getTitle(); - } + if (meta.getTitle() != null) { + title = meta.getTitle(); + } - if (meta.getAuthor() != null) { - author = "©" + meta.getAuthor(); - if (meta.getDate() != null && !meta.getDate().isEmpty()) { - author = author + " (" + meta.getDate() + ")"; - } + if (meta.getAuthor() != null) { + author = "©" + meta.getAuthor(); + if (meta.getDate() != null && !meta.getDate().isEmpty()) { + author = author + " (" + meta.getDate() + ")"; } } @@ -44,7 +43,8 @@ class CliReader extends BasicReader { System.out.println(author); System.out.println(""); - for (Chapter chap : getStory()) { + // TODO: progress? + for (Chapter chap : getStory(null)) { if (chap.getName() != null && !chap.getName().isEmpty()) { System.out.println(Instance.getTrans().getString( StringId.CHAPTER_NAMED, chap.getNumber(), @@ -57,14 +57,17 @@ class CliReader extends BasicReader { } public void read(int chapter) throws IOException { - if (getStory() == null) { + MetaData meta = getMeta(); + + if (meta == null) { throw new IOException("No story to read"); } - if (chapter > getStory().getChapters().size()) { + // TODO: progress? + if (chapter > getStory(null).getChapters().size()) { System.err.println("Chapter " + chapter + ": no such chapter"); } else { - Chapter chap = getStory().getChapters().get(chapter - 1); + Chapter chap = getStory(null).getChapters().get(chapter - 1); System.out.println("Chapter " + chap.getNumber() + ": " + chap.getName()); diff --git a/src/be/nikiroo/fanfix/reader/GuiReader.java b/src/be/nikiroo/fanfix/reader/GuiReader.java index 2c30ab6..9324258 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReader.java +++ b/src/be/nikiroo/fanfix/reader/GuiReader.java @@ -15,6 +15,7 @@ import javax.swing.event.HyperlinkListener; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.VersionCheck; 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; @@ -70,16 +71,13 @@ class GuiReader extends BasicReader { } public void read() throws IOException { - if (getStory() == null) { + MetaData meta = getMeta(); + + if (meta == null) { throw new IOException("No story to read"); } - open(getStory().getMeta().getLuid(), null); - } - - public void read(int chapter) throws IOException { - // TODO: show a special page? - read(); + read(meta.getLuid(), null); } /** @@ -211,20 +209,21 @@ class GuiReader extends BasicReader { } // open the given book - void open(String luid, Progress pg) throws IOException { + void read(String luid, Progress pg) throws IOException { File file = localLibrary.getFile(luid); if (file == null) { imprt(luid, pg); file = localLibrary.getFile(luid); } + // TODO: show a special page for the chapter? openExternal(getLibrary().getInfo(luid), file); } - void changeType(String luid, String newType) { + void changeType(String luid, String newSource) { try { - localLibrary.changeSource(luid, newType, null); - getLibrary().changeSource(luid, newType, null); + localLibrary.changeSource(luid, newSource, null); + getLibrary().changeSource(luid, newSource, null); } catch (IOException e) { Instance.syserr(e); } diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java b/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java index 88eccf7..59663fe 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java +++ b/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java @@ -669,7 +669,7 @@ class GuiReaderFrame extends JFrame { outOfUi(pg, new Runnable() { public void run() { try { - reader.open(book.getMeta().getLuid(), pg); + reader.read(book.getMeta().getLuid(), pg); SwingUtilities.invokeLater(new Runnable() { public void run() { book.setCached(true); diff --git a/src/be/nikiroo/fanfix/reader/Reader.java b/src/be/nikiroo/fanfix/reader/Reader.java index 09d45c1..d2984bd 100644 --- a/src/be/nikiroo/fanfix/reader/Reader.java +++ b/src/be/nikiroo/fanfix/reader/Reader.java @@ -3,11 +3,17 @@ package be.nikiroo.fanfix.reader; import java.io.IOException; import java.net.URL; +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.utils.Progress; +/** + * A {@link Reader} is a class that will handle {@link Story} reading and + * browsing. + * + * @author niki + */ public interface Reader { /** * A type of {@link BasicReader}. @@ -46,42 +52,63 @@ public interface Reader { }; /** - * Return the current {@link Story}. + * Return the current target {@link MetaData}. + * + * @return the meta + */ + public MetaData getMeta(); + + /** + * Return the current {@link Story} as described by the current + * {@link MetaData}. + * + * @param pg + * the optional progress * * @return the {@link Story} */ - public Story getStory(); + public Story getStory(Progress pg); /** - * The {@link LocalLibrary} to load the stories from (by default, takes the - * default {@link LocalLibrary}). + * The {@link BasicLibrary} to load the stories from (by default, takes the + * default {@link BasicLibrary}). * - * @return the {@link LocalLibrary} + * @return the {@link BasicLibrary} */ public BasicLibrary getLibrary(); /** - * Change the {@link LocalLibrary} that will be managed by this + * Change the {@link BasicLibrary} that will be managed by this * {@link BasicReader}. * * @param lib - * the new {@link LocalLibrary} + * the new {@link BasicLibrary} */ - public void setLibrary(LocalLibrary lib); + public void setLibrary(BasicLibrary lib); /** - * Set a {@link Story} from the current {@link Library} into the + * Set a {@link Story} from the current {@link BasicLibrary} into the * {@link Reader}. * * @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; + public void setMeta(String luid) throws IOException; + + /** + * Set a {@link Story} from the current {@link BasicLibrary} into the + * {@link Reader}. + * + * @param meta + * the meta + * + * @throws IOException + * in case of I/O error + */ + public void setMeta(MetaData meta) throws IOException; /** * Set an external {@link Story} into this {@link Reader}. @@ -94,7 +121,7 @@ public interface Reader { * @throws IOException * in case of I/O error */ - public void setStory(URL source, Progress pg) throws IOException; + public void setMeta(URL source, Progress pg) throws IOException; /** * Start the {@link Story} Reading. @@ -106,16 +133,21 @@ public interface Reader { public void read() throws IOException; /** - * Read the selected chapter (starting at 1, 0 = description, -1 = none). + * The selected chapter to start reading at (starting at 1, 0 = description, + * -1 = none). + * + * @return the chapter, or -1 for "no chapter" + */ + public int getChapter(); + + /** + * The selected chapter to start reading at (starting at 1, 0 = description, + * -1 = none). * * @param chapter * the chapter, or -1 for "no 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; + public void setChapter(int chapter); /** * Start the reader in browse mode for the given source (or pass NULL for diff --git a/src/be/nikiroo/fanfix/reader/TuiReader.java b/src/be/nikiroo/fanfix/reader/TuiReader.java index 91b6268..8456da3 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReader.java +++ b/src/be/nikiroo/fanfix/reader/TuiReader.java @@ -49,27 +49,9 @@ class TuiReader extends BasicReader { return backendType; } - public void read(int chapter) throws IOException { - 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 metas) { + public void read() throws IOException { try { - TuiReaderApplication app = new TuiReaderApplication(metas, this, + TuiReaderApplication app = new TuiReaderApplication(this, guessBackendType()); new Thread(app).start(); } catch (Exception e) { @@ -77,19 +59,11 @@ class TuiReader extends BasicReader { } } - /** - * 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) { + public void browse(String source) { + List metas = getLibrary().getListBySource(source); try { - TuiReaderApplication app = new TuiReaderApplication(meta, chapter, - this, guessBackendType()); + TuiReaderApplication app = new TuiReaderApplication(metas, this, + guessBackendType()); new Thread(app).start(); } catch (Exception e) { Instance.syserr(e); diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderApplication.java b/src/be/nikiroo/fanfix/reader/TuiReaderApplication.java index b7f8308..93a0167 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderApplication.java +++ b/src/be/nikiroo/fanfix/reader/TuiReaderApplication.java @@ -10,63 +10,52 @@ import jexer.TWindow; 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.utils.Progress; /** - * Manages the TUI reader, links and manages the {@link TWindow}s, starts the - * correct output mode. + * Manages the TUI general mode and links and manages the {@link TWindow}s. + *

+ * It will also enclose a {@link Reader} and simply handle the reading part + * differently (it will create the required sub-windows and display them). * * @author niki */ class TuiReaderApplication extends TApplication implements Reader { private Reader reader; - public TuiReaderApplication(MetaData meta, int chapter, Reader reader, - BackendType backend) throws Exception { - this(reader, backend); + // start reading if meta present + public TuiReaderApplication(Reader reader, BackendType backend) + throws Exception { + super(backend); + init(reader); - new TuiReaderMainWindow(this, meta, chapter); - } + MetaData meta = getMeta(); - public TuiReaderApplication(List stories, Reader reader, - TApplication.BackendType backend) throws Exception { - this(reader, backend); + new TuiReaderMainWindow(this).setMeta(meta); - new TuiReaderMainWindow(this, stories); + if (meta != null) { + read(); + } } - private TuiReaderApplication(Reader reader, TApplication.BackendType backend) - throws Exception { + public TuiReaderApplication(List stories, Reader reader, + TApplication.BackendType backend) throws Exception { super(backend); + init(reader); - this.reader = reader; - - // Add the menus - addFileMenu(); - addEditMenu(); - addWindowMenu(); - addHelpMenu(); - - getBackend().setTitle("Fanfix"); + new TuiReaderMainWindow(this).setMetas(stories); } public void read() throws IOException { - reader.read(); - } - - public void read(int chapter) throws IOException { - reader.read(chapter); - } + MetaData meta = getMeta(); - public void open(MetaData meta) { - open(meta, -1); - } + if (meta == null) { + throw new IOException("No story to read"); + } - public void open(MetaData meta, int chapter) { // TODO: open in editor + external option if (!meta.isImageDocument()) { - new TuiReaderStoryWindow(this, getLibrary(), meta, chapter); + new TuiReaderStoryWindow(this, getLibrary(), meta, getChapter()); } else { try { BasicReader.openExternal(getLibrary(), meta.getLuid()); @@ -77,27 +66,55 @@ class TuiReaderApplication extends TApplication implements Reader { } } - public Story getStory() { - return reader.getStory(); + public MetaData getMeta() { + return reader.getMeta(); + } + + public Story getStory(Progress pg) { + return reader.getStory(pg); } public BasicLibrary getLibrary() { return reader.getLibrary(); } - public void setLibrary(LocalLibrary lib) { + public void setLibrary(BasicLibrary lib) { reader.setLibrary(lib); } - public void setStory(String luid, Progress pg) throws IOException { - reader.setStory(luid, pg); + public void setMeta(MetaData meta) throws IOException { + reader.setMeta(meta); + } + + public void setMeta(String luid) throws IOException { + reader.setMeta(luid); } - public void setStory(URL source, Progress pg) throws IOException { - reader.setStory(source, pg); + public void setMeta(URL source, Progress pg) throws IOException { + reader.setMeta(source, pg); } public void browse(String source) { reader.browse(source); } + + public int getChapter() { + return reader.getChapter(); + } + + public void setChapter(int chapter) { + reader.setChapter(chapter); + } + + private void init(Reader reader) { + this.reader = reader; + + // Add the menus + addFileMenu(); + addEditMenu(); + addWindowMenu(); + addHelpMenu(); + + getBackend().setTitle("Fanfix"); + } } diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java b/src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java index 3b5dc5d..f076580 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java +++ b/src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java @@ -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 listKeys; private List 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 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 metas = new ArrayList(); 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 metas = new ArrayList(); + 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) { diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java b/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java index 0f6af07..9bc18fe 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java +++ b/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java @@ -23,7 +23,7 @@ class TuiReaderStoryWindow extends TWindow { private MetaData meta; private Story story; private TText textField; - private int chapter = -2; + private int chapter = -1; private List navigationButtons; private TLabel chapterName; -- 2.27.0