04d38c507c46187e3478ee5329b64705a407a2c3
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 table
.setRowData(new String
[][] { //
176 new String
[] { " Author", meta
.getAuthor() }, //
177 new String
[] { " Publication date", formatDate(meta
.getDate()) },
178 new String
[] { " Word count", format(meta
.getWords()) },
179 new String
[] { " Source", meta
.getSource() } //
181 table
.setHeaders(Arrays
.asList("key", "value"), false);
185 private String
format(long value
) {
189 if (!display
.isEmpty()) {
190 display
= "." + display
;
192 display
= (value
% 1000) + display
;
193 value
= value
/ 1000;
199 private String
formatDate(String date
) {
203 ms
= StringUtils
.toTime(date
);
204 } catch (ParseException e
) {
208 SimpleDateFormat sdf
= new SimpleDateFormat(
209 "yyyy-MM-dd'T'HH:mm:ssXXX");
211 ms
= sdf
.parse(date
).getTime();
212 } catch (ParseException e
) {
217 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
218 return sdf
.format(new Date(ms
));
226 * Append the current chapter.
229 * the builder to append to
231 private void displayChapterPage() {
232 table
.setVisible(false);
233 textField
.setVisible(true);
234 table
.setEnabled(false);
235 textField
.setEnabled(true);
237 StringBuilder builder
= new StringBuilder();
241 chap
= getStory().getMeta().getResume();
242 } else if (chapter
> 0) {
243 chap
= getStory().getChapters().get(chapter
- 1);
247 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
248 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
251 for (Paragraph para
: chap
) {
252 if (para
.getType() == ParagraphType
.BREAK
) {
253 builder
.append("\n");
255 builder
.append(para
.getContent()).append("\n");
256 if (para
.getType() == ParagraphType
.BREAK
) {
257 builder
.append("\n");
262 setText(builder
.toString());
265 private Story
getStory() {
270 * Display the given text on the window.
273 * the text to display
275 private void setText(String text
) {
276 textField
.setText(text
);
277 textField
.reflowData();
282 * Set the current chapter area to the correct value.
284 private void setCurrentChapterText() {
287 name
= " " + getStory().getMeta().getTitle();
288 } else if (chapter
== 0) {
289 Chapter resume
= getStory().getMeta().getResume();
290 if (resume
!= null) {
291 name
= String
.format(" %s", resume
.getName());
294 name
= "[No RESUME]";
297 int max
= getStory().getChapters().size();
298 Chapter chap
= getStory().getChapters().get(chapter
- 1);
299 name
= String
.format(" %d/%d: %s", chapter
, max
, chap
.getName());
302 int width
= getWidth() - currentChapter
.getX();
303 name
= String
.format("%-" + width
+ "s", name
);
304 if (name
.length() > width
) {
305 name
= name
.substring(0, width
);
308 currentChapter
.setLabel(name
);
312 * Set the current title in-window.
317 private void setCurrentTitle(String title
) {
319 if (title
.length() < getWidth()) {
320 int padSize
= (getWidth() - title
.length()) / 2;
321 pad
= String
.format("%" + padSize
+ "s", "");
324 title
= pad
+ title
+ pad
;
325 titleField
.setWidth(title
.length());
326 titleField
.setLabel(title
);
329 private static String
desc(MetaData meta
) {
330 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());
334 public void onCommand(TCommandEvent command
) {
335 if (command
.getCmd().equals(TuiReaderApplication
.CMD_EXIT
)) {
336 TuiReaderApplication
.close(this);
338 // Handle our own event if needed here
339 super.onCommand(command
);