From: Niki Roo Date: Wed, 5 Jul 2017 19:18:49 +0000 (+0200) Subject: Code cleanup 3 and update jexer-niki : X-Git-Tag: fanfix-1.6.0~18 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=bc2ea776b67cabcbdcbbc6d8a4e2df1aafa9101a;p=fanfix.git 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 --- 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 f843897..c213e75 100644 Binary files a/libs/jexer-0.0.4-niki0-sources.jar and b/libs/jexer-0.0.4-niki1-sources.jar differ diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index ee122bd..adbd5a3 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -398,14 +398,15 @@ public class Main { try { Reader reader = BasicReader.getReader(); if (library) { - reader.setStory(story, null); + reader.setMeta(story); } else { - reader.setStory(BasicReader.getUrl(story), null); + reader.setMeta(BasicReader.getUrl(story), null); } if (chapString != null) { try { - reader.read(Integer.parseInt(chapString)); + reader.setChapter(Integer.parseInt(chapString)); + reader.read(); } catch (NumberFormatException e) { Instance.syserr(new IOException( "Chapter number cannot be parsed: " + chapString, e)); diff --git a/src/be/nikiroo/fanfix/package-info.java b/src/be/nikiroo/fanfix/package-info.java index eadaa5a..6929cac 100644 --- a/src/be/nikiroo/fanfix/package-info.java +++ b/src/be/nikiroo/fanfix/package-info.java @@ -3,7 +3,8 @@ * which to retrieve stories, then process them into epub (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;