X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FBasicReader.java;h=2c04006ba0d5fa4f68521ef1c17b39abe197b5a3;hb=6322ab64949f9f4ae2b04b9504d58a301039d670;hp=74bd5d48f98000606f9d8c72c67a88ea6f80403a;hpb=c1873e5678fabf306915c54f9c1736e03e027d60;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/BasicReader.java b/src/be/nikiroo/fanfix/reader/BasicReader.java index 74bd5d4..2c04006 100644 --- a/src/be/nikiroo/fanfix/reader/BasicReader.java +++ b/src/be/nikiroo/fanfix/reader/BasicReader.java @@ -7,14 +7,15 @@ import java.net.MalformedURLException; import java.net.URL; import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.Library; 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.ui.UIUtils; +import be.nikiroo.utils.serial.SerialUtils; /** * The class that handles the different {@link Story} readers you can use. @@ -23,19 +24,12 @@ import be.nikiroo.utils.ui.UIUtils; * * @author niki */ -public abstract class BasicReader { - 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, - } - +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. @@ -52,65 +46,29 @@ public abstract class BasicReader { } } - /** - * The type of this reader. - * - * @return the type - */ - public ReaderType getType() { - return type; + public Story getStory() { + return story; } - /** - * The type of this reader. - * - * @param type - * the new type - */ - protected BasicReader setType(ReaderType type) { - this.type = type; - return this; + public BasicLibrary getLibrary() { + if (lib == null) { + lib = defaultLibrary; + } + + return lib; } - /** - * Return the current {@link Story}. - * - * @return the {@link Story} - */ - public Story getStory() { - return story; + public void setLibrary(LocalLibrary lib) { + this.lib = lib; } - /** - * Create a new {@link BasicReader} for a {@link Story} in the - * {@link Library} . - * - * @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 = Instance.getLibrary().getStory(luid, pg); + story = getLibrary().getStory(luid, pg); if (story == null) { throw new IOException("Cannot retrieve story from library: " + luid); } } - /** - * 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) { @@ -126,36 +84,9 @@ 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 type (or pass NULL for all - * types). - * - * @param type - * the type of {@link Story} to take into account, or NULL for - * all - */ - public abstract void start(String type); + public void read() throws IOException { + read(-1); + } /** * Return a new {@link BasicReader} ready for use if one is configured. @@ -164,22 +95,15 @@ 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) { - switch (defaultType) { - case GUI: - UIUtils.setLookAndFeel(); - return new LocalReader().setType(ReaderType.GUI); - case CLI: - return new CliReader().setType(ReaderType.CLI); - case TUI: - return new TuiReader().setType(ReaderType.TUI); - } + return (Reader) SerialUtils.createObject(defaultType + .getTypeName()); } - } catch (IOException e) { + } catch (Exception e) { Instance.syserr(new Exception("Cannot create a reader of type: " - + defaultType, e)); + + defaultType + " (Not compiled in?)", e)); } return null; @@ -206,6 +130,17 @@ public abstract class BasicReader { BasicReader.defaultType = defaultType; } + /** + * Change the default {@link LocalLibrary} to open with the + * {@link BasicReader}s. + * + * @param lib + * the new {@link LocalLibrary} + */ + public static void setDefaultLibrary(BasicLibrary lib) { + BasicReader.defaultLibrary = lib; + } + /** * Return an {@link URL} from this {@link String}, be it a file path or an * actual {@link URL}. @@ -234,16 +169,40 @@ public abstract class BasicReader { return source; } - // open with external player the related file - public static void open(String luid) throws IOException { - MetaData meta = Instance.getLibrary().getInfo(luid); - File target = Instance.getLibrary().getFile(luid); + /** + * Open the {@link Story} with an external reader (the program will be + * passed the main file associated with this {@link Story}). + * + * @param lib + * the {@link BasicLibrary} to select the {@link Story} from + * @param luid + * the {@link Story} LUID + * + * @throws IOException + * in case of I/O error + */ + public static void openExternal(BasicLibrary lib, String luid) + throws IOException { + MetaData meta = lib.getInfo(luid); + File target = lib.getFile(luid); - open(meta, target); + openExternal(meta, target); } - // open with external player the related file - protected static void open(MetaData meta, File target) throws IOException { + /** + * Open the {@link Story} with an external reader (the program will be + * passed the given target file). + * + * @param meta + * the {@link Story} to load + * @param target + * the target {@link File} + * + * @throws IOException + * in case of I/O error + */ + protected static void openExternal(MetaData meta, File target) + throws IOException { String program = null; if (meta.isImageDocument()) { program = Instance.getUiConfig().getString(