Commit | Line | Data |
---|---|---|
16a81ef7 | 1 | package be.nikiroo.fanfix.reader.tui; |
c1873e56 | 2 | |
e8796963 NR |
3 | import java.text.ParseException; |
4 | import java.text.SimpleDateFormat; | |
edfd3577 | 5 | import java.util.ArrayList; |
6a26aa76 | 6 | import java.util.Arrays; |
e8796963 | 7 | import java.util.Date; |
edfd3577 NR |
8 | import java.util.List; |
9 | ||
0861d62a | 10 | import jexer.TAction; |
0861d62a | 11 | import jexer.TButton; |
0861d62a | 12 | import jexer.TLabel; |
f63c4267 | 13 | import jexer.TText; |
0861d62a | 14 | import jexer.TWindow; |
cf9c5ed1 | 15 | import jexer.event.TCommandEvent; |
c1873e56 | 16 | import jexer.event.TResizeEvent; |
c1873e56 NR |
17 | import be.nikiroo.fanfix.data.Chapter; |
18 | import be.nikiroo.fanfix.data.MetaData; | |
19 | import be.nikiroo.fanfix.data.Paragraph; | |
9293188f | 20 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
c1873e56 | 21 | import be.nikiroo.fanfix.data.Story; |
8f34a795 | 22 | import be.nikiroo.jexer.TTable; |
e8796963 | 23 | import be.nikiroo.utils.StringUtils; |
c1873e56 | 24 | |
6a26aa76 NR |
25 | /** |
26 | * This window will contain the {@link Story} in a readable format, with a | |
27 | * chapter browser. | |
28 | * | |
29 | * @author niki | |
30 | */ | |
5dd985cf | 31 | class TuiReaderStoryWindow extends TWindow { |
c1873e56 | 32 | private Story story; |
6a26aa76 | 33 | private TLabel titleField; |
f63c4267 | 34 | private TText textField; |
6a26aa76 NR |
35 | private TTable table; |
36 | private int chapter = -99; // invalid value | |
edfd3577 | 37 | private List<TButton> navigationButtons; |
5b00c122 | 38 | private TLabel currentChapter; |
f00daa5e | 39 | private List<TSizeConstraint> sizeConstraints = new ArrayList<TSizeConstraint>(); |
c1873e56 | 40 | |
6322ab64 | 41 | // chapter: -1 for "none" (0 is desc) |
e0fb1417 NR |
42 | public TuiReaderStoryWindow(TuiReaderApplication app, Story story, |
43 | int chapter) { | |
44 | super(app, desc(story.getMeta()), 0, 0, 60, 18, CENTERED | RESIZABLE); | |
68e2c6d2 | 45 | |
e0fb1417 | 46 | this.story = story; |
c1873e56 | 47 | |
e0fb1417 | 48 | app.setStatusBar(this, desc(story.getMeta())); |
c1873e56 | 49 | |
6a26aa76 NR |
50 | // last = use window background |
51 | titleField = new TLabel(this, " Title", 0, 1, "tlabel", false); | |
f00daa5e | 52 | textField = new TText(this, "", 0, 0, 1, 1); |
8f34a795 | 53 | table = new TTable(this, 0, 0, 1, 1, null, null, Arrays.asList("Key", "Value"), true); |
edfd3577 | 54 | |
e8796963 | 55 | titleField.setEnabled(false); |
e8796963 | 56 | |
0b7e3d78 | 57 | navigationButtons = new ArrayList<TButton>(5); |
edfd3577 | 58 | |
6a26aa76 | 59 | // for bg colour when << button is pressed |
f00daa5e NR |
60 | navigationButtons.add(addButton(" ", 0, 0, null)); |
61 | navigationButtons.add(addButton("<< ", 0, 0, new TAction() { | |
211f7ddb | 62 | @Override |
396e924c | 63 | public void DO() { |
6a26aa76 | 64 | setChapter(-1); |
396e924c | 65 | } |
edfd3577 | 66 | })); |
f00daa5e | 67 | navigationButtons.add(addButton("< ", 4, 0, new TAction() { |
211f7ddb | 68 | @Override |
edfd3577 NR |
69 | public void DO() { |
70 | setChapter(TuiReaderStoryWindow.this.chapter - 1); | |
71 | } | |
72 | })); | |
f00daa5e | 73 | navigationButtons.add(addButton("> ", 7, 0, new TAction() { |
211f7ddb | 74 | @Override |
edfd3577 NR |
75 | public void DO() { |
76 | setChapter(TuiReaderStoryWindow.this.chapter + 1); | |
77 | } | |
78 | })); | |
f00daa5e | 79 | navigationButtons.add(addButton(">> ", 10, 0, new TAction() { |
211f7ddb | 80 | @Override |
edfd3577 NR |
81 | public void DO() { |
82 | setChapter(getStory().getChapters().size()); | |
83 | } | |
84 | })); | |
b0e88ebd | 85 | |
8670a283 NR |
86 | navigationButtons.get(0).setEnabled(false); |
87 | navigationButtons.get(1).setEnabled(false); | |
88 | navigationButtons.get(2).setEnabled(false); | |
edfd3577 | 89 | |
f00daa5e NR |
90 | currentChapter = addLabel("", 0, 0); |
91 | ||
92 | TSizeConstraint.setSize(sizeConstraints, textField, 1, 3, -1, 0); | |
93 | TSizeConstraint.setSize(sizeConstraints, table, 0, 3, 0, 0); | |
94 | TSizeConstraint.setSize(sizeConstraints, currentChapter, 14, -3, -1, | |
95 | null); | |
96 | ||
97 | for (TButton navigationButton : navigationButtons) { | |
98 | TSizeConstraint.setSize(sizeConstraints, navigationButton, null, | |
99 | -3, null, null); | |
100 | } | |
101 | ||
102 | onResize(null); | |
6a26aa76 | 103 | |
edfd3577 | 104 | setChapter(chapter); |
c1873e56 NR |
105 | } |
106 | ||
107 | @Override | |
108 | public void onResize(TResizeEvent resize) { | |
f00daa5e NR |
109 | if (resize != null) { |
110 | super.onResize(resize); | |
111 | } | |
c1873e56 | 112 | |
f00daa5e NR |
113 | // TODO: find out why TText and TTable does not behave the same way |
114 | // (offset of 2 for height and width) | |
6a26aa76 | 115 | |
f00daa5e | 116 | TSizeConstraint.resize(sizeConstraints); |
edfd3577 | 117 | |
f00daa5e NR |
118 | textField.getVerticalScroller().setX( |
119 | textField.getVerticalScroller().getX() + 1); | |
edfd3577 | 120 | |
ce424b19 | 121 | setCurrentChapterText(); |
edfd3577 NR |
122 | } |
123 | ||
5b00c122 NR |
124 | /** |
125 | * Display the current chapter in the window, or the {@link Story} info | |
126 | * page. | |
127 | * | |
128 | * @param chapter | |
129 | * the chapter (including "0" which is the description) or "-1" | |
130 | * to display the info page instead | |
131 | */ | |
edfd3577 | 132 | private void setChapter(int chapter) { |
edfd3577 NR |
133 | if (chapter > getStory().getChapters().size()) { |
134 | chapter = getStory().getChapters().size(); | |
135 | } | |
136 | ||
137 | if (chapter != this.chapter) { | |
138 | this.chapter = chapter; | |
b0e88ebd | 139 | |
8670a283 | 140 | int max = getStory().getChapters().size(); |
5b00c122 NR |
141 | navigationButtons.get(0).setEnabled(chapter > -1); |
142 | navigationButtons.get(1).setEnabled(chapter > -1); | |
143 | navigationButtons.get(2).setEnabled(chapter > -1); | |
8670a283 NR |
144 | navigationButtons.get(3).setEnabled(chapter < max); |
145 | navigationButtons.get(4).setEnabled(chapter < max); | |
b0e88ebd | 146 | |
5b00c122 | 147 | if (chapter < 0) { |
6a26aa76 | 148 | displayInfoPage(); |
edfd3577 | 149 | } else { |
6a26aa76 | 150 | displayChapterPage(); |
edfd3577 | 151 | } |
5b00c122 NR |
152 | } |
153 | ||
154 | setCurrentChapterText(); | |
155 | } | |
edfd3577 | 156 | |
5b00c122 NR |
157 | /** |
158 | * Append the info page about the current {@link Story}. | |
159 | * | |
160 | * @param builder | |
161 | * the builder to append to | |
162 | */ | |
6a26aa76 NR |
163 | private void displayInfoPage() { |
164 | textField.setVisible(false); | |
165 | table.setVisible(true); | |
e8796963 NR |
166 | textField.setEnabled(false); |
167 | table.setEnabled(true); | |
5b00c122 | 168 | |
6a26aa76 | 169 | MetaData meta = getStory().getMeta(); |
5b00c122 | 170 | |
6a26aa76 | 171 | setCurrentTitle(meta.getTitle()); |
5b00c122 | 172 | |
6a26aa76 | 173 | table.setRowData(new String[][] { // |
e8796963 NR |
174 | new String[] { " Author", meta.getAuthor() }, // |
175 | new String[] { " Publication date", formatDate(meta.getDate()) }, | |
176 | new String[] { " Word count", format(meta.getWords()) }, | |
177 | new String[] { " Source", meta.getSource() } // | |
6a26aa76 NR |
178 | }); |
179 | table.setHeaders(Arrays.asList("key", "value"), false); | |
180 | table.toTop(); | |
5b00c122 NR |
181 | } |
182 | ||
e8796963 NR |
183 | private String format(long value) { |
184 | String display = ""; | |
185 | ||
186 | while (value > 0) { | |
187 | if (!display.isEmpty()) { | |
188 | display = "." + display; | |
189 | } | |
190 | display = (value % 1000) + display; | |
191 | value = value / 1000; | |
192 | } | |
193 | ||
194 | return display; | |
195 | } | |
196 | ||
197 | private String formatDate(String date) { | |
198 | long ms = 0; | |
199 | ||
200 | try { | |
201 | ms = StringUtils.toTime(date); | |
202 | } catch (ParseException e) { | |
203 | } | |
204 | ||
205 | if (ms <= 0) { | |
206 | SimpleDateFormat sdf = new SimpleDateFormat( | |
207 | "yyyy-MM-dd'T'HH:mm:ssXXX"); | |
208 | try { | |
209 | ms = sdf.parse(date).getTime(); | |
210 | } catch (ParseException e) { | |
211 | } | |
212 | } | |
213 | ||
214 | if (ms > 0) { | |
215 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
216 | return sdf.format(new Date(ms)); | |
217 | } | |
218 | ||
219 | // :( | |
220 | return date; | |
221 | } | |
222 | ||
5b00c122 NR |
223 | /** |
224 | * Append the current chapter. | |
225 | * | |
226 | * @param builder | |
227 | * the builder to append to | |
228 | */ | |
6a26aa76 NR |
229 | private void displayChapterPage() { |
230 | table.setVisible(false); | |
231 | textField.setVisible(true); | |
e8796963 NR |
232 | table.setEnabled(false); |
233 | textField.setEnabled(true); | |
6a26aa76 NR |
234 | |
235 | StringBuilder builder = new StringBuilder(); | |
236 | ||
5b00c122 NR |
237 | Chapter chap = null; |
238 | if (chapter == 0) { | |
239 | chap = getStory().getMeta().getResume(); | |
240 | } else if (chapter > 0) { | |
241 | chap = getStory().getChapters().get(chapter - 1); | |
242 | } | |
243 | ||
244 | // TODO: i18n | |
245 | String chapName = chap == null ? "[No RESUME]" : chap.getName(); | |
6a26aa76 | 246 | setCurrentTitle(String.format("Chapter %d: %s", chapter, chapName)); |
5b00c122 NR |
247 | |
248 | if (chap != null) { | |
249 | for (Paragraph para : chap) { | |
250 | if (para.getType() == ParagraphType.BREAK) { | |
251 | builder.append("\n"); | |
252 | } | |
253 | builder.append(para.getContent()).append("\n"); | |
254 | if (para.getType() == ParagraphType.BREAK) { | |
255 | builder.append("\n"); | |
fdc55375 | 256 | } |
edfd3577 | 257 | } |
edfd3577 | 258 | } |
6a26aa76 NR |
259 | |
260 | setText(builder.toString()); | |
c1873e56 NR |
261 | } |
262 | ||
263 | private Story getStory() { | |
c1873e56 NR |
264 | return story; |
265 | } | |
266 | ||
5b00c122 NR |
267 | /** |
268 | * Display the given text on the window. | |
269 | * | |
270 | * @param text | |
271 | * the text to display | |
272 | */ | |
273 | private void setText(String text) { | |
274 | textField.setText(text); | |
275 | textField.reflowData(); | |
276 | textField.toTop(); | |
277 | } | |
278 | ||
279 | /** | |
280 | * Set the current chapter area to the correct value. | |
281 | */ | |
282 | private void setCurrentChapterText() { | |
283 | String name; | |
284 | if (chapter < 0) { | |
285 | name = " " + getStory().getMeta().getTitle(); | |
286 | } else if (chapter == 0) { | |
287 | Chapter resume = getStory().getMeta().getResume(); | |
288 | if (resume != null) { | |
289 | name = String.format(" %s", resume.getName()); | |
290 | } else { | |
291 | // TODO: i18n | |
292 | name = "[No RESUME]"; | |
293 | } | |
294 | } else { | |
295 | int max = getStory().getChapters().size(); | |
296 | Chapter chap = getStory().getChapters().get(chapter - 1); | |
297 | name = String.format(" %d/%d: %s", chapter, max, chap.getName()); | |
298 | } | |
299 | ||
300 | int width = getWidth() - currentChapter.getX(); | |
ce424b19 | 301 | name = String.format("%-" + width + "s", name); |
5b00c122 NR |
302 | if (name.length() > width) { |
303 | name = name.substring(0, width); | |
304 | } | |
305 | ||
306 | currentChapter.setLabel(name); | |
c1873e56 | 307 | } |
5b00c122 NR |
308 | |
309 | /** | |
6a26aa76 | 310 | * Set the current title in-window. |
5b00c122 | 311 | * |
5b00c122 | 312 | * @param title |
6a26aa76 | 313 | * the new title |
5b00c122 | 314 | */ |
6a26aa76 | 315 | private void setCurrentTitle(String title) { |
e8796963 NR |
316 | String pad = ""; |
317 | if (title.length() < getWidth()) { | |
318 | int padSize = (getWidth() - title.length()) / 2; | |
319 | pad = String.format("%" + padSize + "s", ""); | |
320 | } | |
321 | ||
322 | title = pad + title + pad; | |
6a26aa76 NR |
323 | titleField.setWidth(title.length()); |
324 | titleField.setLabel(title); | |
325 | } | |
5b00c122 | 326 | |
6a26aa76 NR |
327 | private static String desc(MetaData meta) { |
328 | return String.format("%s: %s", meta.getLuid(), meta.getTitle()); | |
5b00c122 | 329 | } |
cf9c5ed1 NR |
330 | |
331 | @Override | |
332 | public void onCommand(TCommandEvent command) { | |
333 | if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) { | |
334 | TuiReaderApplication.close(this); | |
335 | } else { | |
336 | // Handle our own event if needed here | |
337 | super.onCommand(command); | |
338 | } | |
339 | } | |
c1873e56 | 340 | } |