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",
56 titleField
.setEnabled(false);
58 navigationButtons
= new ArrayList
<TButton
>(5);
60 navigationButtons
.add(addButton("<<", 0, 0, new TAction() {
66 navigationButtons
.add(addButton("< ", 4, 0, new TAction() {
69 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
72 navigationButtons
.add(addButton("> ", 7, 0, new TAction() {
75 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
78 navigationButtons
.add(addButton(">>", 10, 0, new TAction() {
81 setChapter(getStory().getChapters().size());
85 navigationButtons
.get(0).setEnabled(false);
86 navigationButtons
.get(1).setEnabled(false);
88 currentChapter
= addLabel("", 0, 0);
90 TSizeConstraint
.setSize(sizeConstraints
, textField
, 1, 3, -1, -1);
91 TSizeConstraint
.setSize(sizeConstraints
, table
, 0, 3, 0, -1);
92 TSizeConstraint
.setSize(sizeConstraints
, currentChapter
, 14, -3, -1,
95 for (TButton navigationButton
: navigationButtons
) {
96 navigationButton
.setShadowColor(null);
97 // navigationButton.setEmptyBorders(false);
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 // Improve the disposition of the scrollbars
119 textField
.getVerticalScroller().setX(textField
.getWidth());
120 textField
.getVerticalScroller().setHeight(textField
.getHeight());
121 textField
.getHorizontalScroller().setX(-1);
122 textField
.getHorizontalScroller().setWidth(textField
.getWidth() + 1);
124 setCurrentChapterText();
128 * Display the current chapter in the window, or the {@link Story} info
132 * the chapter (including "0" which is the description) or "-1"
133 * to display the info page instead
135 private void setChapter(int chapter
) {
136 if (chapter
> getStory().getChapters().size()) {
137 chapter
= getStory().getChapters().size();
140 if (chapter
!= this.chapter
) {
141 this.chapter
= chapter
;
143 int max
= getStory().getChapters().size();
144 navigationButtons
.get(0).setEnabled(chapter
> -1);
145 navigationButtons
.get(1).setEnabled(chapter
> -1);
146 navigationButtons
.get(2).setEnabled(chapter
< max
);
147 navigationButtons
.get(3).setEnabled(chapter
< max
);
152 displayChapterPage();
156 setCurrentChapterText();
160 * Append the info page about the current {@link Story}.
163 * the builder to append to
165 private void displayInfoPage() {
166 textField
.setVisible(false);
167 table
.setVisible(true);
168 textField
.setEnabled(false);
169 table
.setEnabled(true);
171 MetaData meta
= getStory().getMeta();
173 setCurrentTitle(meta
.getTitle());
175 StringBuilder tags
= new StringBuilder();
176 for (String tag
: meta
.getTags()) {
177 if (tags
.length() > 0) {
183 table
.setRowData(new String
[][] { //
184 new String
[] { " Author", meta
.getAuthor() }, //
185 new String
[] { " Publication date", formatDate(meta
.getDate()) },
186 new String
[] { " Published on", meta
.getPublisher() },
187 new String
[] { " URL", meta
.getUrl() },
188 new String
[] { " Word count", format(meta
.getWords()) },
189 new String
[] { " Source", meta
.getSource() },
190 new String
[] { " Subject", meta
.getSubject() },
191 new String
[] { " Language", meta
.getLang() },
192 new String
[] { " Tags", tags
.toString() } //
194 table
.setHeaders(Arrays
.asList("key", "value"), false);
198 private String
format(long value
) {
202 if (!display
.isEmpty()) {
203 display
= "." + display
;
205 display
= (value
% 1000) + display
;
206 value
= value
/ 1000;
212 private String
formatDate(String date
) {
216 ms
= StringUtils
.toTime(date
);
217 } catch (ParseException e
) {
221 SimpleDateFormat sdf
= new SimpleDateFormat(
222 "yyyy-MM-dd'T'HH:mm:ssXXX");
224 ms
= sdf
.parse(date
).getTime();
225 } catch (ParseException e
) {
230 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
231 return sdf
.format(new Date(ms
));
239 * Append the current chapter.
242 * the builder to append to
244 private void displayChapterPage() {
245 table
.setVisible(false);
246 textField
.setVisible(true);
247 table
.setEnabled(false);
248 textField
.setEnabled(true);
250 StringBuilder builder
= new StringBuilder();
254 chap
= getStory().getMeta().getResume();
255 } else if (chapter
> 0) {
256 chap
= getStory().getChapters().get(chapter
- 1);
260 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
261 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
264 for (Paragraph para
: chap
) {
265 if (para
.getType() == ParagraphType
.BREAK
) {
266 builder
.append("\n");
268 builder
.append(para
.getContent()).append("\n");
269 if (para
.getType() == ParagraphType
.BREAK
) {
270 builder
.append("\n");
275 setText(builder
.toString());
278 private Story
getStory() {
283 * Display the given text on the window.
286 * the text to display
288 private void setText(String text
) {
289 textField
.setText(text
);
290 textField
.reflowData();
295 * Set the current chapter area to the correct value.
297 private void setCurrentChapterText() {
300 name
= " " + getStory().getMeta().getTitle();
301 } else if (chapter
== 0) {
302 Chapter resume
= getStory().getMeta().getResume();
303 if (resume
!= null) {
304 name
= String
.format(" %s", resume
.getName());
307 name
= "[No RESUME]";
310 int max
= getStory().getChapters().size();
311 Chapter chap
= getStory().getChapters().get(chapter
- 1);
312 name
= String
.format(" %d/%d: %s", chapter
, max
, chap
.getName());
315 int width
= getWidth() - currentChapter
.getX();
316 name
= String
.format("%-" + width
+ "s", name
);
317 if (name
.length() > width
) {
318 name
= name
.substring(0, width
);
321 currentChapter
.setLabel(name
);
325 * Set the current title in-window.
330 private void setCurrentTitle(String title
) {
332 if (title
.length() < getWidth()) {
333 int padSize
= (getWidth() - title
.length()) / 2;
334 pad
= String
.format("%" + padSize
+ "s", "");
337 title
= pad
+ title
+ pad
;
338 titleField
.setWidth(title
.length());
339 titleField
.setLabel(title
);
342 private static String
desc(MetaData meta
) {
343 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());
347 public void onCommand(TCommandEvent command
) {
348 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
349 TuiReaderApplication
.close(this);
351 // Handle our own event if needed here
352 super.onCommand(command
);