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
;
15 import jexer
.event
.TCommandEvent
;
16 import jexer
.event
.TResizeEvent
;
17 import be
.nikiroo
.fanfix
.data
.Chapter
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
;
20 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
21 import be
.nikiroo
.fanfix
.data
.Story
;
22 import be
.nikiroo
.jexer
.TTable
;
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", "Value"), true);
55 titleField
.setEnabled(false);
57 navigationButtons
= new ArrayList
<TButton
>(5);
59 // for bg colour when << button is pressed
60 navigationButtons
.add(addButton(" ", 0, 0, null));
61 navigationButtons
.add(addButton("<< ", 0, 0, new TAction() {
67 navigationButtons
.add(addButton("< ", 4, 0, new TAction() {
70 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
73 navigationButtons
.add(addButton("> ", 7, 0, new TAction() {
76 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
79 navigationButtons
.add(addButton(">> ", 10, 0, new TAction() {
82 setChapter(getStory().getChapters().size());
86 navigationButtons
.get(0).setEnabled(false);
87 navigationButtons
.get(1).setEnabled(false);
88 navigationButtons
.get(2).setEnabled(false);
90 currentChapter
= addLabel("", 0, 0);
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,
97 for (TButton navigationButton
: navigationButtons
) {
98 TSizeConstraint
.setSize(sizeConstraints
, navigationButton
, null,
108 public void onResize(TResizeEvent resize
) {
109 if (resize
!= null) {
110 super.onResize(resize
);
113 // TODO: find out why TText and TTable does not behave the same way
114 // (offset of 2 for height and width)
116 TSizeConstraint
.resize(sizeConstraints
);
118 textField
.getVerticalScroller().setX(
119 textField
.getVerticalScroller().getX() + 1);
121 setCurrentChapterText();
125 * Display the current chapter in the window, or the {@link Story} info
129 * the chapter (including "0" which is the description) or "-1"
130 * to display the info page instead
132 private void setChapter(int chapter
) {
133 if (chapter
> getStory().getChapters().size()) {
134 chapter
= getStory().getChapters().size();
137 if (chapter
!= this.chapter
) {
138 this.chapter
= chapter
;
140 int max
= getStory().getChapters().size();
141 navigationButtons
.get(0).setEnabled(chapter
> -1);
142 navigationButtons
.get(1).setEnabled(chapter
> -1);
143 navigationButtons
.get(2).setEnabled(chapter
> -1);
144 navigationButtons
.get(3).setEnabled(chapter
< max
);
145 navigationButtons
.get(4).setEnabled(chapter
< max
);
150 displayChapterPage();
154 setCurrentChapterText();
158 * Append the info page about the current {@link Story}.
161 * the builder to append to
163 private void displayInfoPage() {
164 textField
.setVisible(false);
165 table
.setVisible(true);
166 textField
.setEnabled(false);
167 table
.setEnabled(true);
169 MetaData meta
= getStory().getMeta();
171 setCurrentTitle(meta
.getTitle());
173 table
.setRowData(new String
[][] { //
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() } //
179 table
.setHeaders(Arrays
.asList("key", "value"), false);
183 private String
format(long value
) {
187 if (!display
.isEmpty()) {
188 display
= "." + display
;
190 display
= (value
% 1000) + display
;
191 value
= value
/ 1000;
197 private String
formatDate(String date
) {
201 ms
= StringUtils
.toTime(date
);
202 } catch (ParseException e
) {
206 SimpleDateFormat sdf
= new SimpleDateFormat(
207 "yyyy-MM-dd'T'HH:mm:ssXXX");
209 ms
= sdf
.parse(date
).getTime();
210 } catch (ParseException e
) {
215 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
216 return sdf
.format(new Date(ms
));
224 * Append the current chapter.
227 * the builder to append to
229 private void displayChapterPage() {
230 table
.setVisible(false);
231 textField
.setVisible(true);
232 table
.setEnabled(false);
233 textField
.setEnabled(true);
235 StringBuilder builder
= new StringBuilder();
239 chap
= getStory().getMeta().getResume();
240 } else if (chapter
> 0) {
241 chap
= getStory().getChapters().get(chapter
- 1);
245 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
246 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
249 for (Paragraph para
: chap
) {
250 if (para
.getType() == ParagraphType
.BREAK
) {
251 builder
.append("\n");
253 builder
.append(para
.getContent()).append("\n");
254 if (para
.getType() == ParagraphType
.BREAK
) {
255 builder
.append("\n");
260 setText(builder
.toString());
263 private Story
getStory() {
268 * Display the given text on the window.
271 * the text to display
273 private void setText(String text
) {
274 textField
.setText(text
);
275 textField
.reflowData();
280 * Set the current chapter area to the correct value.
282 private void setCurrentChapterText() {
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());
292 name
= "[No RESUME]";
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());
300 int width
= getWidth() - currentChapter
.getX();
301 name
= String
.format("%-" + width
+ "s", name
);
302 if (name
.length() > width
) {
303 name
= name
.substring(0, width
);
306 currentChapter
.setLabel(name
);
310 * Set the current title in-window.
315 private void setCurrentTitle(String title
) {
317 if (title
.length() < getWidth()) {
318 int padSize
= (getWidth() - title
.length()) / 2;
319 pad
= String
.format("%" + padSize
+ "s", "");
322 title
= pad
+ title
+ pad
;
323 titleField
.setWidth(title
.length());
324 titleField
.setLabel(title
);
327 private static String
desc(MetaData meta
) {
328 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());
332 public void onCommand(TCommandEvent command
) {
333 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
334 TuiReaderApplication
.close(this);
336 // Handle our own event if needed here
337 super.onCommand(command
);