d28dff5d4244213e753fade45f03fa1dc9b7fb58
1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.text
.ParseException
;
4 import java
.text
.SimpleDateFormat
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
16 import jexer
.event
.TResizeEvent
;
17 import jexer
.event
.TResizeEvent
.Type
;
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
.fanfix
.library
.BasicLibrary
;
24 import be
.nikiroo
.utils
.StringUtils
;
27 * This window will contain the {@link Story} in a readable format, with a
32 class TuiReaderStoryWindow
extends TWindow
{
33 private BasicLibrary lib
;
34 private MetaData meta
;
36 private TLabel titleField
;
37 private TText textField
;
39 private int chapter
= -99; // invalid value
40 private List
<TButton
> navigationButtons
;
41 private TLabel currentChapter
;
43 // chapter: -1 for "none" (0 is desc)
44 public TuiReaderStoryWindow(TuiReaderApplication app
, BasicLibrary lib
,
45 MetaData meta
, int chapter
) {
46 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
51 app
.setStatusBar(this, desc(meta
));
53 // last = use window background
54 titleField
= new TLabel(this, " Title", 0, 1, "tlabel", false);
55 textField
= new TText(this, "", 1, 3, getWidth() - 4, getHeight() - 5);
56 table
= new TTable(this, 0, 3, getWidth(), getHeight() - 4, null, null,
57 Arrays
.asList("Key", "Value"), true);
59 titleField
.setEnabled(false);
60 textField
.getVerticalScroller().setX(
61 textField
.getVerticalScroller().getX() + 1);
63 navigationButtons
= new ArrayList
<TButton
>(5);
65 // -3 because 0-based and 2 for borders
66 int row
= getHeight() - 3;
68 // for bg colour when << button is pressed
69 navigationButtons
.add(addButton(" ", 0, row
, null));
70 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
76 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
79 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
82 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
85 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
88 navigationButtons
.add(addButton(">> ", 10, row
, new TAction() {
91 setChapter(getStory().getChapters().size());
95 navigationButtons
.get(0).setEnabled(false);
96 navigationButtons
.get(1).setEnabled(false);
97 navigationButtons
.get(2).setEnabled(false);
99 currentChapter
= addLabel("", 14, row
);
100 currentChapter
.setWidth(getWidth() - 10);
106 public void onResize(TResizeEvent resize
) {
107 super.onResize(resize
);
109 // Resize the text field
110 // TODO: why setW/setH/reflow not enough for the scrollbars?
111 textField
.onResize(new TResizeEvent(Type
.WIDGET
, resize
.getWidth() - 4,
112 resize
.getHeight() - 5));
113 textField
.getVerticalScroller().setX(
114 textField
.getVerticalScroller().getX() + 1);
116 table
.setWidth(getWidth());
117 table
.setHeight(getHeight() - 4);
120 // -3 because 0-based and 2 for borders
121 int row
= getHeight() - 3;
123 String name
= currentChapter
.getLabel();
124 while (name
.length() < resize
.getWidth() - currentChapter
.getX()) {
127 currentChapter
.setLabel(name
);
128 currentChapter
.setWidth(resize
.getWidth() - 10);
129 currentChapter
.setY(row
);
131 for (TButton button
: navigationButtons
) {
137 * Display the current chapter in the window, or the {@link Story} info
141 * the chapter (including "0" which is the description) or "-1"
142 * to display the info page instead
144 private void setChapter(int chapter
) {
145 if (chapter
> getStory().getChapters().size()) {
146 chapter
= getStory().getChapters().size();
149 if (chapter
!= this.chapter
) {
150 this.chapter
= chapter
;
152 int max
= getStory().getChapters().size();
153 navigationButtons
.get(0).setEnabled(chapter
> -1);
154 navigationButtons
.get(1).setEnabled(chapter
> -1);
155 navigationButtons
.get(2).setEnabled(chapter
> -1);
156 navigationButtons
.get(3).setEnabled(chapter
< max
);
157 navigationButtons
.get(4).setEnabled(chapter
< max
);
162 displayChapterPage();
166 setCurrentChapterText();
170 * Append the info page about the current {@link Story}.
173 * the builder to append to
175 private void displayInfoPage() {
176 textField
.setVisible(false);
177 table
.setVisible(true);
178 textField
.setEnabled(false);
179 table
.setEnabled(true);
181 MetaData meta
= getStory().getMeta();
183 setCurrentTitle(meta
.getTitle());
185 table
.setRowData(new String
[][] { //
186 new String
[] { " Author", meta
.getAuthor() }, //
187 new String
[] { " Publication date", formatDate(meta
.getDate()) },
188 new String
[] { " Word count", format(meta
.getWords()) },
189 new String
[] { " Source", meta
.getSource() } //
191 table
.setHeaders(Arrays
.asList("key", "value"), false);
195 private String
format(long value
) {
199 if (!display
.isEmpty()) {
200 display
= "." + display
;
202 display
= (value
% 1000) + display
;
203 value
= value
/ 1000;
209 private String
formatDate(String date
) {
213 ms
= StringUtils
.toTime(date
);
214 } catch (ParseException e
) {
218 SimpleDateFormat sdf
= new SimpleDateFormat(
219 "yyyy-MM-dd'T'HH:mm:ssXXX");
221 ms
= sdf
.parse(date
).getTime();
222 } catch (ParseException e
) {
227 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
228 return sdf
.format(new Date(ms
));
236 * Append the current chapter.
239 * the builder to append to
241 private void displayChapterPage() {
242 table
.setVisible(false);
243 textField
.setVisible(true);
244 table
.setEnabled(false);
245 textField
.setEnabled(true);
247 StringBuilder builder
= new StringBuilder();
251 chap
= getStory().getMeta().getResume();
252 } else if (chapter
> 0) {
253 chap
= getStory().getChapters().get(chapter
- 1);
257 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
258 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
261 for (Paragraph para
: chap
) {
262 if (para
.getType() == ParagraphType
.BREAK
) {
263 builder
.append("\n");
265 builder
.append(para
.getContent()).append("\n");
266 if (para
.getType() == ParagraphType
.BREAK
) {
267 builder
.append("\n");
272 setText(builder
.toString());
275 private Story
getStory() {
277 // TODO: progress bar?
278 story
= lib
.getStory(meta
.getLuid(), null);
284 * Display the given text on the window.
287 * the text to display
289 private void setText(String text
) {
290 textField
.setText(text
);
291 textField
.reflowData();
296 * Set the current chapter area to the correct value.
298 private void setCurrentChapterText() {
301 name
= " " + getStory().getMeta().getTitle();
302 } else if (chapter
== 0) {
303 Chapter resume
= getStory().getMeta().getResume();
304 if (resume
!= null) {
305 name
= String
.format(" %s", resume
.getName());
308 name
= "[No RESUME]";
311 int max
= getStory().getChapters().size();
312 Chapter chap
= getStory().getChapters().get(chapter
- 1);
313 name
= String
.format(" %d/%d: %s", chapter
, max
, chap
.getName());
316 int width
= getWidth() - currentChapter
.getX();
317 while (name
.length() < width
) {
321 if (name
.length() > width
) {
322 name
= name
.substring(0, width
);
325 currentChapter
.setLabel(name
);
329 * Set the current title in-window.
334 private void setCurrentTitle(String title
) {
336 if (title
.length() < getWidth()) {
337 int padSize
= (getWidth() - title
.length()) / 2;
338 pad
= String
.format("%" + padSize
+ "s", "");
341 title
= pad
+ title
+ pad
;
342 titleField
.setWidth(title
.length());
343 titleField
.setLabel(title
);
346 private static String
desc(MetaData meta
) {
347 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());