X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Ftui%2FTuiReaderApplication.java;h=b85bb8612768a30480942ccf760d6cb5baf9d449;hb=e0fb1417b679a2f3cb0fef4937e79b211f1ce3c4;hp=1de62a46a2a420484cf7ea8fa54efee1d83cc692;hpb=cf9c5ed1164709d753d9c93ae8589f09b2f90ac0;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java b/src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java index 1de62a4..b85bb86 100644 --- a/src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java @@ -4,6 +4,7 @@ import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.io.IOException; import java.net.URL; +import java.net.UnknownHostException; import jexer.TApplication; import jexer.TCommand; @@ -43,10 +44,7 @@ class TuiReaderApplication extends TApplication implements Reader { private Reader reader; private TuiReaderMainWindow main; - - private MetaData meta; private String source; - private boolean useMeta; // start reading if meta present public TuiReaderApplication(Reader reader, BackendType backend) @@ -54,7 +52,10 @@ class TuiReaderApplication extends TApplication implements Reader { super(backend); init(reader); - showMain(getMeta(), null, true); + MetaData meta = getMeta(); + if (meta != null) { + read(); + } } public TuiReaderApplication(Reader reader, String source, @@ -62,30 +63,13 @@ class TuiReaderApplication extends TApplication implements Reader { super(backend); init(reader); - showMain(null, source, false); + showMain(); + setSource(source); } @Override public void read() throws IOException { - MetaData meta = getMeta(); - - if (meta == null) { - throw new IOException("No story to read"); - } - - // TODO: open in editor + external option - if (!meta.isImageDocument()) { - TWindow window = new TuiReaderStoryWindow(this, getLibrary(), meta, - getChapter()); - window.maximize(); - } else { - try { - openExternal(getLibrary(), meta.getLuid()); - } catch (IOException e) { - messageBox("Error when trying to open the story", - e.getMessage(), TMessageBox.Type.OK); - } - } + read(getStory(null)); } @Override @@ -138,6 +122,25 @@ class TuiReaderApplication extends TApplication implements Reader { reader.setChapter(chapter); } + public void read(Story story) throws IOException { + if (story == null) { + throw new IOException("No story to read"); + } + + // TODO: open in editor + external option + if (!story.getMeta().isImageDocument()) { + TWindow window = new TuiReaderStoryWindow(this, story, getChapter()); + window.maximize(); + } else { + try { + openExternal(getLibrary(), story.getMeta().getLuid()); + } catch (IOException e) { + messageBox("Error when trying to open the story", + e.getMessage(), TMessageBox.Type.OK); + } + } + } + /** * Set the default status bar when this window appear. *

@@ -158,13 +161,7 @@ class TuiReaderApplication extends TApplication implements Reader { } - private void showMain(MetaData meta, String source, boolean useMeta) - throws IOException { - // TODO: thread-safety - this.meta = meta; - this.source = source; - this.useMeta = useMeta; - + private void showMain() { if (main != null && main.isVisible()) { main.activate(); } else { @@ -172,17 +169,16 @@ class TuiReaderApplication extends TApplication implements Reader { main.close(); } main = new TuiReaderMainWindow(this); - if (useMeta) { - main.setMeta(meta); - if (meta != null) { - read(); - } - } else { - main.setSource(source); - } + main.maximize(); } } + private void setSource(String source) { + this.source = source; + showMain(); + main.setSource(source); + } + private void init(Reader reader) { this.reader = reader; @@ -220,6 +216,20 @@ class TuiReaderApplication extends TApplication implements Reader { switch (menu.getId()) { case MENU_EXIT: close(this); + return true; + case MENU_OPEN: + String openfile = null; + try { + openfile = fileOpenBox("."); + reader.setMeta(BasicReader.getUrl(openfile), null); + read(); + } catch (IOException e) { + // TODO: i18n + error("Fail to open file" + + (openfile == null ? "" : ": " + openfile), + "Import error", e); + } + return true; case MENU_IMPORT_URL: String clipboard = ""; @@ -238,42 +248,59 @@ class TuiReaderApplication extends TApplication implements Reader { String url = inputBox("Import story", "URL to import", clipboard) .getText(); - if (!imprt(url)) { - // TODO: bad import + try { + if (!imprt(url)) { + // TODO: i18n + error("URK not supported: " + url, "Import error"); + } + } catch (IOException e) { + // TODO: i18n + error("Fail to import URL: " + url, "Import error", e); } return true; case MENU_IMPORT_FILE: + String filename = null; try { - String filename = fileOpenBox("."); + filename = fileOpenBox("."); if (!imprt(filename)) { - // TODO: bad import + // TODO: i18n + error("File not supported: " + filename, "Import error"); } } catch (IOException e) { - // TODO: bad file - e.printStackTrace(); + // TODO: i18n + error("Fail to import file" + + (filename == null ? "" : ": " + filename), + "Import error", e); } - return true; case MENU_LIBRARY: - try { - showMain(meta, source, useMeta); - } catch (IOException e) { - e.printStackTrace(); - } - + showMain(); + setSource(source); return true; } return super.onMenu(menu); } - private boolean imprt(String url) { + /** + * Import the given url. + *

+ * Can fail if the host is not supported. + * + * @param url + * + * @return TRUE in case of success, FALSE if the host is not supported + * + * @throws IOException + * in case of I/O error + */ + private boolean imprt(String url) throws IOException { try { reader.getLibrary().imprt(BasicReader.getUrl(url), null); main.refreshStories(); return true; - } catch (IOException e) { + } catch (UnknownHostException e) { return false; } } @@ -283,6 +310,43 @@ class TuiReaderApplication extends TApplication implements Reader { reader.openExternal(lib, luid); } + /** + * Display an error message and log it. + * + * @param message + * the message + * @param title + * the title of the error message + */ + private void error(String message, String title) { + error(message, title, null); + } + + /** + * Display an error message and log it, including the linked + * {@link Exception}. + * + * @param message + * the message + * @param title + * the title of the error message + * @param e + * the exception to log if any (can be NULL) + */ + private void error(String message, String title, Exception e) { + Instance.getTraceHandler().error(title + ": " + message); + if (e != null) { + Instance.getTraceHandler().error(e); + } + + if (e != null) { + messageBox(title, message // + + "\n" + e.getMessage()); + } else { + messageBox(title, message); + } + } + /** * Ask the user and, if confirmed, close the {@link TApplication} this * {@link TWidget} is running on.