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
.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
;
26 * This window will contain the {@link Story} in a readable format, with a
31 class TuiReaderStoryWindow
extends TWindow
{
33 private TLabel titleField
;
34 private TText textField
;
36 private int chapter
= -99; // invalid value
37 private List
<TButton
> navigationButtons
;
38 private TLabel currentChapter
;
39 private List
<TSizeConstraint
> sizeConstraints
= new ArrayList
<TSizeConstraint
>();
41 // chapter: -1 for "none" (0 is desc)
42 public TuiReaderStoryWindow(TuiReaderApplication app
, Story story
,
44 super(app
, desc(story
.getMeta()), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
48 app
.setStatusBar(this, desc(story
.getMeta()));
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",
56 titleField
.setEnabled(false);
58 navigationButtons
= new ArrayList
<TButton
>(5);
60 // for bg colour when << button is pressed
61 navigationButtons
.add(addButton(" ", 0, 0, null));
62 navigationButtons
.add(addButton("<< ", 0, 0, new TAction() {
68 navigationButtons
.add(addButton("< ", 4, 0, new TAction() {
71 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
74 navigationButtons
.add(addButton("> ", 7, 0, new TAction() {
77 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
80 navigationButtons
.add(addButton(">> ", 10, 0, new TAction() {
83 setChapter(getStory().getChapters().size());
87 navigationButtons
.get(0).setEnabled(false);
88 navigationButtons
.get(1).setEnabled(false);
89 navigationButtons
.get(2).setEnabled(false);
91 currentChapter
= addLabel("", 0, 0);
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,
98 for (TButton navigationButton
: navigationButtons
) {
99 TSizeConstraint
.setSize(sizeConstraints
, navigationButton
, null,
109 public void onResize(TResizeEvent resize
) {
110 if (resize
!= null) {
111 super.onResize(resize
);
114 // TODO: find out why TText and TTable does not behave the same way
115 // (offset of 2 for height and width)
117 TSizeConstraint
.resize(sizeConstraints
);
119 textField
.getVerticalScroller().setX(
120 textField
.getVerticalScroller().getX() + 1);
122 String name
= currentChapter
.getLabel();
123 int size
= Math
.max(name
.length(), currentChapter
.getWidth());
124 name
= String
.format("%" + size
+ "s", name
);
126 currentChapter
.setLabel(name
);
130 * Display the current chapter in the window, or the {@link Story} info
134 * the chapter (including "0" which is the description) or "-1"
135 * to display the info page instead
137 private void setChapter(int chapter
) {
138 if (chapter
> getStory().getChapters().size()) {
139 chapter
= getStory().getChapters().size();
142 if (chapter
!= this.chapter
) {
143 this.chapter
= chapter
;
145 int max
= getStory().getChapters().size();
146 navigationButtons
.get(0).setEnabled(chapter
> -1);
147 navigationButtons
.get(1).setEnabled(chapter
> -1);
148 navigationButtons
.get(2).setEnabled(chapter
> -1);
149 navigationButtons
.get(3).setEnabled(chapter
< max
);
150 navigationButtons
.get(4).setEnabled(chapter
< max
);
155 displayChapterPage();
159 setCurrentChapterText();
163 * Append the info page about the current {@link Story}.
166 * the builder to append to
168 private void displayInfoPage() {
169 textField
.setVisible(false);
170 table
.setVisible(true);
171 textField
.setEnabled(false);
172 table
.setEnabled(true);
174 MetaData meta
= getStory().getMeta();
176 setCurrentTitle(meta
.getTitle());
178 table
.setRowData(new String
[][] { //
179 new String
[] { " Author", meta
.getAuthor() }, //
180 new String
[] { " Publication date", formatDate(meta
.getDate()) },
181 new String
[] { " Word count", format(meta
.getWords()) },
182 new String
[] { " Source", meta
.getSource() } //
184 table
.setHeaders(Arrays
.asList("key", "value"), false);
188 private String
format(long value
) {
192 if (!display
.isEmpty()) {
193 display
= "." + display
;
195 display
= (value
% 1000) + display
;
196 value
= value
/ 1000;
202 private String
formatDate(String date
) {
206 ms
= StringUtils
.toTime(date
);
207 } catch (ParseException e
) {
211 SimpleDateFormat sdf
= new SimpleDateFormat(
212 "yyyy-MM-dd'T'HH:mm:ssXXX");
214 ms
= sdf
.parse(date
).getTime();
215 } catch (ParseException e
) {
220 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
221 return sdf
.format(new Date(ms
));
229 * Append the current chapter.
232 * the builder to append to
234 private void displayChapterPage() {
235 table
.setVisible(false);
236 textField
.setVisible(true);
237 table
.setEnabled(false);
238 textField
.setEnabled(true);
240 StringBuilder builder
= new StringBuilder();
244 chap
= getStory().getMeta().getResume();
245 } else if (chapter
> 0) {
246 chap
= getStory().getChapters().get(chapter
- 1);
250 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
251 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
254 for (Paragraph para
: chap
) {
255 if (para
.getType() == ParagraphType
.BREAK
) {
256 builder
.append("\n");
258 builder
.append(para
.getContent()).append("\n");
259 if (para
.getType() == ParagraphType
.BREAK
) {
260 builder
.append("\n");
265 setText(builder
.toString());
268 private Story
getStory() {
273 * Display the given text on the window.
276 * the text to display
278 private void setText(String text
) {
279 textField
.setText(text
);
280 textField
.reflowData();
285 * Set the current chapter area to the correct value.
287 private void setCurrentChapterText() {
290 name
= " " + getStory().getMeta().getTitle();
291 } else if (chapter
== 0) {
292 Chapter resume
= getStory().getMeta().getResume();
293 if (resume
!= null) {
294 name
= String
.format(" %s", resume
.getName());
297 name
= "[No RESUME]";
300 int max
= getStory().getChapters().size();
301 Chapter chap
= getStory().getChapters().get(chapter
- 1);
302 name
= String
.format(" %d/%d: %s", chapter
, max
, chap
.getName());
305 int width
= getWidth() - currentChapter
.getX();
306 while (name
.length() < width
) {
310 if (name
.length() > width
) {
311 name
= name
.substring(0, width
);
314 currentChapter
.setLabel(name
);
318 * Set the current title in-window.
323 private void setCurrentTitle(String title
) {
325 if (title
.length() < getWidth()) {
326 int padSize
= (getWidth() - title
.length()) / 2;
327 pad
= String
.format("%" + padSize
+ "s", "");
330 title
= pad
+ title
+ pad
;
331 titleField
.setWidth(title
.length());
332 titleField
.setLabel(title
);
335 private static String
desc(MetaData meta
) {
336 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());
340 public void onCommand(TCommandEvent command
) {
341 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
342 TuiReaderApplication
.close(this);
344 // Handle our own event if needed here
345 super.onCommand(command
);