| 1 | package be.nikiroo.fanfix.reader.tui; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Arrays; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import jexer.TAction; |
| 8 | import jexer.TButton; |
| 9 | import jexer.TLabel; |
| 10 | import jexer.TTable; |
| 11 | import jexer.TText; |
| 12 | import jexer.TWindow; |
| 13 | import jexer.event.TResizeEvent; |
| 14 | import jexer.event.TResizeEvent.Type; |
| 15 | import be.nikiroo.fanfix.data.Chapter; |
| 16 | import be.nikiroo.fanfix.data.MetaData; |
| 17 | import be.nikiroo.fanfix.data.Paragraph; |
| 18 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
| 19 | import be.nikiroo.fanfix.data.Story; |
| 20 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 21 | |
| 22 | /** |
| 23 | * This window will contain the {@link Story} in a readable format, with a |
| 24 | * chapter browser. |
| 25 | * |
| 26 | * @author niki |
| 27 | */ |
| 28 | class TuiReaderStoryWindow extends TWindow { |
| 29 | private BasicLibrary lib; |
| 30 | private MetaData meta; |
| 31 | private Story story; |
| 32 | private TLabel titleField; |
| 33 | private TText textField; |
| 34 | private TTable table; |
| 35 | private int chapter = -99; // invalid value |
| 36 | private List<TButton> navigationButtons; |
| 37 | private TLabel currentChapter; |
| 38 | |
| 39 | // chapter: -1 for "none" (0 is desc) |
| 40 | public TuiReaderStoryWindow(TuiReaderApplication app, BasicLibrary lib, |
| 41 | MetaData meta, int chapter) { |
| 42 | super(app, desc(meta), 0, 0, 60, 18, CENTERED | RESIZABLE); |
| 43 | |
| 44 | this.lib = lib; |
| 45 | this.meta = meta; |
| 46 | |
| 47 | app.setStatusBar(this, desc(meta)); |
| 48 | |
| 49 | // last = use window background |
| 50 | titleField = new TLabel(this, " Title", 0, 1, "tlabel", false); |
| 51 | textField = new TText(this, "", 0, 3, getWidth() - 2, getHeight() - 5); |
| 52 | table = new TTable(this, 0, 3, getWidth(), getHeight() - 4, null, null, |
| 53 | Arrays.asList("Key", "Value"), true); |
| 54 | |
| 55 | navigationButtons = new ArrayList<TButton>(5); |
| 56 | |
| 57 | // -3 because 0-based and 2 for borders |
| 58 | int row = getHeight() - 3; |
| 59 | |
| 60 | // for bg colour when << button is pressed |
| 61 | navigationButtons.add(addButton(" ", 0, row, null)); |
| 62 | navigationButtons.add(addButton("<< ", 0, row, new TAction() { |
| 63 | @Override |
| 64 | public void DO() { |
| 65 | setChapter(-1); |
| 66 | } |
| 67 | })); |
| 68 | navigationButtons.add(addButton("< ", 4, row, new TAction() { |
| 69 | @Override |
| 70 | public void DO() { |
| 71 | setChapter(TuiReaderStoryWindow.this.chapter - 1); |
| 72 | } |
| 73 | })); |
| 74 | navigationButtons.add(addButton("> ", 7, row, new TAction() { |
| 75 | @Override |
| 76 | public void DO() { |
| 77 | setChapter(TuiReaderStoryWindow.this.chapter + 1); |
| 78 | } |
| 79 | })); |
| 80 | navigationButtons.add(addButton(">> ", 10, row, new TAction() { |
| 81 | @Override |
| 82 | public void DO() { |
| 83 | setChapter(getStory().getChapters().size()); |
| 84 | } |
| 85 | })); |
| 86 | |
| 87 | navigationButtons.get(0).setEnabled(false); |
| 88 | navigationButtons.get(1).setEnabled(false); |
| 89 | navigationButtons.get(2).setEnabled(false); |
| 90 | |
| 91 | currentChapter = addLabel("", 14, row); |
| 92 | currentChapter.setWidth(getWidth() - 10); |
| 93 | |
| 94 | setChapter(chapter); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onResize(TResizeEvent resize) { |
| 99 | super.onResize(resize); |
| 100 | |
| 101 | // Resize the text field TODO: why setW/setH/reflow not enough for the |
| 102 | // scrollbars? |
| 103 | textField.onResize(new TResizeEvent(Type.WIDGET, resize.getWidth() - 2, |
| 104 | resize.getHeight() - 5)); |
| 105 | |
| 106 | table.setWidth(getWidth()); |
| 107 | table.setHeight(getHeight() - 4); |
| 108 | table.reflowData(); |
| 109 | |
| 110 | // -3 because 0-based and 2 for borders |
| 111 | int row = getHeight() - 3; |
| 112 | |
| 113 | String name = currentChapter.getLabel(); |
| 114 | while (name.length() < resize.getWidth() - currentChapter.getX()) { |
| 115 | name += " "; |
| 116 | } |
| 117 | currentChapter.setLabel(name); |
| 118 | currentChapter.setWidth(resize.getWidth() - 10); |
| 119 | currentChapter.setY(row); |
| 120 | |
| 121 | for (TButton button : navigationButtons) { |
| 122 | button.setY(row); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Display the current chapter in the window, or the {@link Story} info |
| 128 | * page. |
| 129 | * |
| 130 | * @param chapter |
| 131 | * the chapter (including "0" which is the description) or "-1" |
| 132 | * to display the info page instead |
| 133 | */ |
| 134 | private void setChapter(int chapter) { |
| 135 | if (chapter > getStory().getChapters().size()) { |
| 136 | chapter = getStory().getChapters().size(); |
| 137 | } |
| 138 | |
| 139 | if (chapter != this.chapter) { |
| 140 | this.chapter = chapter; |
| 141 | |
| 142 | int max = getStory().getChapters().size(); |
| 143 | navigationButtons.get(0).setEnabled(chapter > -1); |
| 144 | navigationButtons.get(1).setEnabled(chapter > -1); |
| 145 | navigationButtons.get(2).setEnabled(chapter > -1); |
| 146 | navigationButtons.get(3).setEnabled(chapter < max); |
| 147 | navigationButtons.get(4).setEnabled(chapter < max); |
| 148 | |
| 149 | if (chapter < 0) { |
| 150 | displayInfoPage(); |
| 151 | } else { |
| 152 | displayChapterPage(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | setCurrentChapterText(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Append the info page about the current {@link Story}. |
| 161 | * |
| 162 | * @param builder |
| 163 | * the builder to append to |
| 164 | */ |
| 165 | private void displayInfoPage() { |
| 166 | textField.setVisible(false); |
| 167 | table.setVisible(true); |
| 168 | |
| 169 | MetaData meta = getStory().getMeta(); |
| 170 | |
| 171 | setCurrentTitle(meta.getTitle()); |
| 172 | |
| 173 | table.setRowData(new String[][] { // |
| 174 | new String[] { "Author", meta.getAuthor() }, // |
| 175 | new String[] { "Publication date", meta.getDate() }, |
| 176 | new String[] { "Word count", Long.toString(meta.getWords()) }, |
| 177 | new String[] { "Source", meta.getSource() } // |
| 178 | }); |
| 179 | table.setHeaders(Arrays.asList("key", "value"), false); |
| 180 | table.toTop(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Append the current chapter. |
| 185 | * |
| 186 | * @param builder |
| 187 | * the builder to append to |
| 188 | */ |
| 189 | private void displayChapterPage() { |
| 190 | table.setVisible(false); |
| 191 | textField.setVisible(true); |
| 192 | |
| 193 | StringBuilder builder = new StringBuilder(); |
| 194 | |
| 195 | Chapter chap = null; |
| 196 | if (chapter == 0) { |
| 197 | chap = getStory().getMeta().getResume(); |
| 198 | } else if (chapter > 0) { |
| 199 | chap = getStory().getChapters().get(chapter - 1); |
| 200 | } |
| 201 | |
| 202 | // TODO: i18n |
| 203 | String chapName = chap == null ? "[No RESUME]" : chap.getName(); |
| 204 | setCurrentTitle(String.format("Chapter %d: %s", chapter, chapName)); |
| 205 | |
| 206 | if (chap != null) { |
| 207 | for (Paragraph para : chap) { |
| 208 | if (para.getType() == ParagraphType.BREAK) { |
| 209 | builder.append("\n"); |
| 210 | } |
| 211 | builder.append(para.getContent()).append("\n"); |
| 212 | if (para.getType() == ParagraphType.BREAK) { |
| 213 | builder.append("\n"); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | setText(builder.toString()); |
| 219 | } |
| 220 | |
| 221 | private Story getStory() { |
| 222 | if (story == null) { |
| 223 | // TODO: progress bar? |
| 224 | story = lib.getStory(meta.getLuid(), null); |
| 225 | } |
| 226 | return story; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Display the given text on the window. |
| 231 | * |
| 232 | * @param text |
| 233 | * the text to display |
| 234 | */ |
| 235 | private void setText(String text) { |
| 236 | textField.setText(text); |
| 237 | textField.reflowData(); |
| 238 | textField.toTop(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Set the current chapter area to the correct value. |
| 243 | */ |
| 244 | private void setCurrentChapterText() { |
| 245 | String name; |
| 246 | if (chapter < 0) { |
| 247 | name = " " + getStory().getMeta().getTitle(); |
| 248 | } else if (chapter == 0) { |
| 249 | Chapter resume = getStory().getMeta().getResume(); |
| 250 | if (resume != null) { |
| 251 | name = String.format(" %s", resume.getName()); |
| 252 | } else { |
| 253 | // TODO: i18n |
| 254 | name = "[No RESUME]"; |
| 255 | } |
| 256 | } else { |
| 257 | int max = getStory().getChapters().size(); |
| 258 | Chapter chap = getStory().getChapters().get(chapter - 1); |
| 259 | name = String.format(" %d/%d: %s", chapter, max, chap.getName()); |
| 260 | } |
| 261 | |
| 262 | int width = getWidth() - currentChapter.getX(); |
| 263 | while (name.length() < width) { |
| 264 | name += " "; |
| 265 | } |
| 266 | |
| 267 | if (name.length() > width) { |
| 268 | name = name.substring(0, width); |
| 269 | } |
| 270 | |
| 271 | currentChapter.setLabel(name); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Set the current title in-window. |
| 276 | * |
| 277 | * @param title |
| 278 | * the new title |
| 279 | */ |
| 280 | private void setCurrentTitle(String title) { |
| 281 | titleField.setWidth(title.length()); |
| 282 | titleField.setLabel(title); |
| 283 | } |
| 284 | |
| 285 | private static String desc(MetaData meta) { |
| 286 | return String.format("%s: %s", meta.getLuid(), meta.getTitle()); |
| 287 | } |
| 288 | } |