5874015153e799e8af55e46b157a3f6aac10b8e0
1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
13 import jexer
.event
.TResizeEvent
;
14 import jexer
.event
.TResizeEvent
.Type
;
15 import be
.nikiroo
.fanfix
.data
.Chapter
;
16 import be
.nikiroo
.fanfix
.data
.MetaData
;
17 import be
.nikiroo
.fanfix
.data
.Paragraph
;
18 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
19 import be
.nikiroo
.fanfix
.data
.Story
;
20 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
23 * This window will contain the {@link Story} in a readable format, with a
28 class TuiReaderStoryWindow
extends TWindow
{
29 private BasicLibrary lib
;
30 private MetaData meta
;
32 private TLabel titleField
;
33 private TText textField
;
35 private int chapter
= -99; // invalid value
36 private List
<TButton
> navigationButtons
;
37 private TLabel currentChapter
;
39 // chapter: -1 for "none" (0 is desc)
40 public TuiReaderStoryWindow(TuiReaderApplication app
, BasicLibrary lib
,
41 MetaData meta
, int chapter
) {
42 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
47 app
.setStatusBar(this, desc(meta
));
49 // last = use window background
50 titleField
= new TLabel(this, " Title", 0, 1, "tlabel", false);
51 textField
= new TText(this, "", 0, 3, getWidth() - 2, getHeight() - 5);
52 table
= new TTable(this, 0, 3, getWidth(), getHeight() - 4, null, null,
53 Arrays
.asList("Key", "Value"), true);
55 navigationButtons
= new ArrayList
<TButton
>(5);
57 // -3 because 0-based and 2 for borders
58 int row
= getHeight() - 3;
60 // for bg colour when << button is pressed
61 navigationButtons
.add(addButton(" ", 0, row
, null));
62 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
68 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
71 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
74 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
77 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
80 navigationButtons
.add(addButton(">> ", 10, row
, 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("", 14, row
);
92 currentChapter
.setWidth(getWidth() - 10);
98 public void onResize(TResizeEvent resize
) {
99 super.onResize(resize
);
101 // Resize the text field TODO: why setW/setH/reflow not enough for the
103 textField
.onResize(new TResizeEvent(Type
.WIDGET
, resize
.getWidth() - 2,
104 resize
.getHeight() - 5));
106 table
.setWidth(getWidth());
107 table
.setHeight(getHeight() - 4);
110 // -3 because 0-based and 2 for borders
111 int row
= getHeight() - 3;
113 String name
= currentChapter
.getLabel();
114 while (name
.length() < resize
.getWidth() - currentChapter
.getX()) {
117 currentChapter
.setLabel(name
);
118 currentChapter
.setWidth(resize
.getWidth() - 10);
119 currentChapter
.setY(row
);
121 for (TButton button
: navigationButtons
) {
127 * Display the current chapter in the window, or the {@link Story} info
131 * the chapter (including "0" which is the description) or "-1"
132 * to display the info page instead
134 private void setChapter(int chapter
) {
135 if (chapter
> getStory().getChapters().size()) {
136 chapter
= getStory().getChapters().size();
139 if (chapter
!= this.chapter
) {
140 this.chapter
= chapter
;
142 int max
= getStory().getChapters().size();
143 navigationButtons
.get(0).setEnabled(chapter
> -1);
144 navigationButtons
.get(1).setEnabled(chapter
> -1);
145 navigationButtons
.get(2).setEnabled(chapter
> -1);
146 navigationButtons
.get(3).setEnabled(chapter
< max
);
147 navigationButtons
.get(4).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);
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", meta
.getDate() },
176 new String
[] { "Word count", Long
.toString(meta
.getWords()) },
177 new String
[] { "Source", meta
.getSource() } //
179 table
.setHeaders(Arrays
.asList("key", "value"), false);
184 * Append the current chapter.
187 * the builder to append to
189 private void displayChapterPage() {
190 table
.setVisible(false);
191 textField
.setVisible(true);
193 StringBuilder builder
= new StringBuilder();
197 chap
= getStory().getMeta().getResume();
198 } else if (chapter
> 0) {
199 chap
= getStory().getChapters().get(chapter
- 1);
203 String chapName
= chap
== null ?
"[No RESUME]" : chap
.getName();
204 setCurrentTitle(String
.format("Chapter %d: %s", chapter
, chapName
));
207 for (Paragraph para
: chap
) {
208 if (para
.getType() == ParagraphType
.BREAK
) {
209 builder
.append("\n");
211 builder
.append(para
.getContent()).append("\n");
212 if (para
.getType() == ParagraphType
.BREAK
) {
213 builder
.append("\n");
218 setText(builder
.toString());
221 private Story
getStory() {
223 // TODO: progress bar?
224 story
= lib
.getStory(meta
.getLuid(), null);
230 * Display the given text on the window.
233 * the text to display
235 private void setText(String text
) {
236 textField
.setText(text
);
237 textField
.reflowData();
242 * Set the current chapter area to the correct value.
244 private void setCurrentChapterText() {
247 name
= " " + getStory().getMeta().getTitle();
248 } else if (chapter
== 0) {
249 Chapter resume
= getStory().getMeta().getResume();
250 if (resume
!= null) {
251 name
= String
.format(" %s", resume
.getName());
254 name
= "[No RESUME]";
257 int max
= getStory().getChapters().size();
258 Chapter chap
= getStory().getChapters().get(chapter
- 1);
259 name
= String
.format(" %d/%d: %s", chapter
, max
, chap
.getName());
262 int width
= getWidth() - currentChapter
.getX();
263 while (name
.length() < width
) {
267 if (name
.length() > width
) {
268 name
= name
.substring(0, width
);
271 currentChapter
.setLabel(name
);
275 * Set the current title in-window.
280 private void setCurrentTitle(String title
) {
281 titleField
.setWidth(title
.length());
282 titleField
.setLabel(title
);
285 private static String
desc(MetaData meta
) {
286 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());