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