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 setCurrentChapterText();
126 * Display the current chapter in the window, or the {@link Story} info
130 * the chapter (including "0" which is the description) or "-1"
131 * to display the info page instead
133 private void setChapter(int chapter
) {
134 if (chapter
> getStory().getChapters().size()) {
135 chapter
= getStory().getChapters().size();
138 if (chapter
!= this.chapter
) {
139 this.chapter
= chapter
;
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
);
151 displayChapterPage();
155 setCurrentChapterText();
159 * Append the info page about the current {@link Story}.
162 * the builder to append to
164 private void displayInfoPage() {
165 textField
.setVisible(false);
166 table
.setVisible(true);
167 textField
.setEnabled(false);
168 table
.setEnabled(true);
170 MetaData meta
= getStory().getMeta();
172 setCurrentTitle(meta
.getTitle());
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() } //
180 table
.setHeaders(Arrays
.asList("key", "value"), false);
184 private String
format(long value
) {
188 if (!display
.isEmpty()) {
189 display
= "." + display
;
191 display
= (value
% 1000) + display
;
192 value
= value
/ 1000;
198 private String
formatDate(String date
) {
202 ms
= StringUtils
.toTime(date
);
203 } catch (ParseException e
) {
207 SimpleDateFormat sdf
= new SimpleDateFormat(
208 "yyyy-MM-dd'T'HH:mm:ssXXX");
210 ms
= sdf
.parse(date
).getTime();
211 } catch (ParseException e
) {
216 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
217 return sdf
.format(new Date(ms
));
225 * Append the current chapter.
228 * the builder to append to
230 private void displayChapterPage() {
231 table
.setVisible(false);
232 textField
.setVisible(true);
233 table
.setEnabled(false);
234 textField
.setEnabled(true);
236 StringBuilder builder
= new StringBuilder();
240 chap
= getStory().getMeta().getResume();
241 } else if (chapter
> 0) {
242 chap
= getStory().getChapters().get(chapter
- 1);
246 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
247 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
250 for (Paragraph para
: chap
) {
251 if (para
.getType() == ParagraphType
.BREAK
) {
252 builder
.append("\n");
254 builder
.append(para
.getContent()).append("\n");
255 if (para
.getType() == ParagraphType
.BREAK
) {
256 builder
.append("\n");
261 setText(builder
.toString());
264 private Story
getStory() {
269 * Display the given text on the window.
272 * the text to display
274 private void setText(String text
) {
275 textField
.setText(text
);
276 textField
.reflowData();
281 * Set the current chapter area to the correct value.
283 private void setCurrentChapterText() {
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());
293 name
= "[No RESUME]";
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());
301 int width
= getWidth() - currentChapter
.getX();
302 name
= String
.format("%-" + width
+ "s", name
);
303 if (name
.length() > width
) {
304 name
= name
.substring(0, width
);
307 currentChapter
.setLabel(name
);
311 * Set the current title in-window.
316 private void setCurrentTitle(String title
) {
318 if (title
.length() < getWidth()) {
319 int padSize
= (getWidth() - title
.length()) / 2;
320 pad
= String
.format("%" + padSize
+ "s", "");
323 title
= pad
+ title
+ pad
;
324 titleField
.setWidth(title
.length());
325 titleField
.setLabel(title
);
328 private static String
desc(MetaData meta
) {
329 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());
333 public void onCommand(TCommandEvent command
) {
334 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
335 TuiReaderApplication
.close(this);
337 // Handle our own event if needed here
338 super.onCommand(command
);