X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Ftui%2FTuiReaderStoryWindow.java;h=ddf7ad13d98e35a0d47e1984881067aa44131c8b;hb=f00daa5ee4c1e5de819f06b17282a87e8d328fd4;hp=5874015153e799e8af55e46b157a3f6aac10b8e0;hpb=6a26aa7608a8e5573fb69b1ef949980d5c4676bf;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java b/src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java index 5874015..ddf7ad1 100644 --- a/src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java @@ -1,7 +1,10 @@ package be.nikiroo.fanfix.reader.tui; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.List; import jexer.TAction; @@ -10,14 +13,14 @@ import jexer.TLabel; import jexer.TTable; import jexer.TText; import jexer.TWindow; +import jexer.event.TCommandEvent; import jexer.event.TResizeEvent; -import jexer.event.TResizeEvent.Type; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Paragraph.ParagraphType; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.library.BasicLibrary; +import be.nikiroo.utils.StringUtils; /** * This window will contain the {@link Story} in a readable format, with a @@ -26,8 +29,6 @@ import be.nikiroo.fanfix.library.BasicLibrary; * @author niki */ class TuiReaderStoryWindow extends TWindow { - private BasicLibrary lib; - private MetaData meta; private Story story; private TLabel titleField; private TText textField; @@ -35,49 +36,48 @@ class TuiReaderStoryWindow extends TWindow { private int chapter = -99; // invalid value private List navigationButtons; private TLabel currentChapter; + private List sizeConstraints = new ArrayList(); // chapter: -1 for "none" (0 is desc) - public TuiReaderStoryWindow(TuiReaderApplication app, BasicLibrary lib, - MetaData meta, int chapter) { - super(app, desc(meta), 0, 0, 60, 18, CENTERED | RESIZABLE); + public TuiReaderStoryWindow(TuiReaderApplication app, Story story, + int chapter) { + super(app, desc(story.getMeta()), 0, 0, 60, 18, CENTERED | RESIZABLE); - this.lib = lib; - this.meta = meta; + this.story = story; - app.setStatusBar(this, desc(meta)); + app.setStatusBar(this, desc(story.getMeta())); // last = use window background titleField = new TLabel(this, " Title", 0, 1, "tlabel", false); - textField = new TText(this, "", 0, 3, getWidth() - 2, getHeight() - 5); - table = new TTable(this, 0, 3, getWidth(), getHeight() - 4, null, null, - Arrays.asList("Key", "Value"), true); + textField = new TText(this, "", 0, 0, 1, 1); + table = new TTable(this, 0, 0, 1, 1, null, null, Arrays.asList("Key", + "Value"), true); - navigationButtons = new ArrayList(5); + titleField.setEnabled(false); - // -3 because 0-based and 2 for borders - int row = getHeight() - 3; + navigationButtons = new ArrayList(5); // for bg colour when << button is pressed - navigationButtons.add(addButton(" ", 0, row, null)); - navigationButtons.add(addButton("<< ", 0, row, new TAction() { + navigationButtons.add(addButton(" ", 0, 0, null)); + navigationButtons.add(addButton("<< ", 0, 0, new TAction() { @Override public void DO() { setChapter(-1); } })); - navigationButtons.add(addButton("< ", 4, row, new TAction() { + navigationButtons.add(addButton("< ", 4, 0, new TAction() { @Override public void DO() { setChapter(TuiReaderStoryWindow.this.chapter - 1); } })); - navigationButtons.add(addButton("> ", 7, row, new TAction() { + navigationButtons.add(addButton("> ", 7, 0, new TAction() { @Override public void DO() { setChapter(TuiReaderStoryWindow.this.chapter + 1); } })); - navigationButtons.add(addButton(">> ", 10, row, new TAction() { + navigationButtons.add(addButton(">> ", 10, 0, new TAction() { @Override public void DO() { setChapter(getStory().getChapters().size()); @@ -88,39 +88,42 @@ class TuiReaderStoryWindow extends TWindow { navigationButtons.get(1).setEnabled(false); navigationButtons.get(2).setEnabled(false); - currentChapter = addLabel("", 14, row); - currentChapter.setWidth(getWidth() - 10); + currentChapter = addLabel("", 0, 0); + + TSizeConstraint.setSize(sizeConstraints, textField, 1, 3, -1, 0); + TSizeConstraint.setSize(sizeConstraints, table, 0, 3, 0, 0); + TSizeConstraint.setSize(sizeConstraints, currentChapter, 14, -3, -1, + null); + + for (TButton navigationButton : navigationButtons) { + TSizeConstraint.setSize(sizeConstraints, navigationButton, null, + -3, null, null); + } + + onResize(null); setChapter(chapter); } @Override public void onResize(TResizeEvent resize) { - super.onResize(resize); + if (resize != null) { + super.onResize(resize); + } - // Resize the text field TODO: why setW/setH/reflow not enough for the - // scrollbars? - textField.onResize(new TResizeEvent(Type.WIDGET, resize.getWidth() - 2, - resize.getHeight() - 5)); + // TODO: find out why TText and TTable does not behave the same way + // (offset of 2 for height and width) - table.setWidth(getWidth()); - table.setHeight(getHeight() - 4); - table.reflowData(); + TSizeConstraint.resize(sizeConstraints); - // -3 because 0-based and 2 for borders - int row = getHeight() - 3; + textField.getVerticalScroller().setX( + textField.getVerticalScroller().getX() + 1); String name = currentChapter.getLabel(); - while (name.length() < resize.getWidth() - currentChapter.getX()) { - name += " "; - } - currentChapter.setLabel(name); - currentChapter.setWidth(resize.getWidth() - 10); - currentChapter.setY(row); + int size = Math.max(name.length(), currentChapter.getWidth()); + name = String.format("%" + size + "s", name); - for (TButton button : navigationButtons) { - button.setY(row); - } + currentChapter.setLabel(name); } /** @@ -165,21 +168,63 @@ class TuiReaderStoryWindow extends TWindow { private void displayInfoPage() { textField.setVisible(false); table.setVisible(true); + textField.setEnabled(false); + table.setEnabled(true); MetaData meta = getStory().getMeta(); setCurrentTitle(meta.getTitle()); table.setRowData(new String[][] { // - new String[] { "Author", meta.getAuthor() }, // - new String[] { "Publication date", meta.getDate() }, - new String[] { "Word count", Long.toString(meta.getWords()) }, - new String[] { "Source", meta.getSource() } // + new String[] { " Author", meta.getAuthor() }, // + new String[] { " Publication date", formatDate(meta.getDate()) }, + new String[] { " Word count", format(meta.getWords()) }, + new String[] { " Source", meta.getSource() } // }); table.setHeaders(Arrays.asList("key", "value"), false); table.toTop(); } + private String format(long value) { + String display = ""; + + while (value > 0) { + if (!display.isEmpty()) { + display = "." + display; + } + display = (value % 1000) + display; + value = value / 1000; + } + + return display; + } + + private String formatDate(String date) { + long ms = 0; + + try { + ms = StringUtils.toTime(date); + } catch (ParseException e) { + } + + if (ms <= 0) { + SimpleDateFormat sdf = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ssXXX"); + try { + ms = sdf.parse(date).getTime(); + } catch (ParseException e) { + } + } + + if (ms > 0) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(new Date(ms)); + } + + // :( + return date; + } + /** * Append the current chapter. * @@ -189,6 +234,8 @@ class TuiReaderStoryWindow extends TWindow { private void displayChapterPage() { table.setVisible(false); textField.setVisible(true); + table.setEnabled(false); + textField.setEnabled(true); StringBuilder builder = new StringBuilder(); @@ -219,10 +266,6 @@ class TuiReaderStoryWindow extends TWindow { } private Story getStory() { - if (story == null) { - // TODO: progress bar? - story = lib.getStory(meta.getLuid(), null); - } return story; } @@ -278,6 +321,13 @@ class TuiReaderStoryWindow extends TWindow { * the new title */ private void setCurrentTitle(String title) { + String pad = ""; + if (title.length() < getWidth()) { + int padSize = (getWidth() - title.length()) / 2; + pad = String.format("%" + padSize + "s", ""); + } + + title = pad + title + pad; titleField.setWidth(title.length()); titleField.setLabel(title); } @@ -285,4 +335,14 @@ class TuiReaderStoryWindow extends TWindow { private static String desc(MetaData meta) { return String.format("%s: %s", meta.getLuid(), meta.getTitle()); } + + @Override + public void onCommand(TCommandEvent command) { + if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) { + TuiReaderApplication.close(this); + } else { + // Handle our own event if needed here + super.onCommand(command); + } + } }