51d8fa032fd9dbf8e575299c359e5cecec736761
1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.util
.ArrayList
;
7 import jexer
.TApplication
;
10 import jexer
.TKeypress
;
14 import jexer
.event
.TResizeEvent
;
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
;
22 class TuiReaderStoryWindow
extends TWindow
{
23 private BasicLibrary lib
;
24 private MetaData meta
;
26 private TText textField
;
27 private int chapter
= -1;
28 private List
<TButton
> navigationButtons
;
29 private TLabel chapterName
;
31 // chapter: -1 for "none" (0 is desc)
32 public TuiReaderStoryWindow(TApplication app
, BasicLibrary lib
,
33 MetaData meta
, int chapter
) {
34 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
39 // TODO: show all meta info before?
41 textField
= new TText(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
43 statusBar
= newStatusBar(desc(meta
));
44 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
46 navigationButtons
= new ArrayList
<TButton
>(5);
48 // -3 because 0-based and 2 for borders
49 int row
= getHeight() - 3;
51 navigationButtons
.add(addButton(" ", 0, row
, null)); // for bg colour
55 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
61 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
64 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
67 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
70 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
73 navigationButtons
.add(addButton(">> ", 10, row
, new TAction() {
76 setChapter(getStory().getChapters().size());
80 navigationButtons
.get(0).setEnabled(false);
81 navigationButtons
.get(1).setEnabled(false);
82 navigationButtons
.get(2).setEnabled(false);
84 chapterName
= addLabel("", 14, row
);
85 chapterName
.setWidth(getWidth() - 10);
90 public void onResize(TResizeEvent resize
) {
91 super.onResize(resize
);
93 // Resize the text field
94 textField
.setWidth(resize
.getWidth() - 2);
95 textField
.setHeight(resize
.getHeight() - 2);
98 // -3 because 0-based and 2 for borders
99 int row
= getHeight() - 3;
101 String name
= chapterName
.getLabel();
102 while (name
.length() < resize
.getWidth() - chapterName
.getX()) {
105 chapterName
.setLabel(name
);
106 chapterName
.setWidth(resize
.getWidth() - 10);
107 chapterName
.setY(row
);
109 for (TButton button
: navigationButtons
) {
114 private void setChapter(int chapter
) {
119 if (chapter
> getStory().getChapters().size()) {
120 chapter
= getStory().getChapters().size();
123 if (chapter
!= this.chapter
) {
124 this.chapter
= chapter
;
126 int max
= getStory().getChapters().size();
127 navigationButtons
.get(0).setEnabled(chapter
> 0);
128 navigationButtons
.get(1).setEnabled(chapter
> 0);
129 navigationButtons
.get(2).setEnabled(chapter
> 0);
130 navigationButtons
.get(3).setEnabled(chapter
< max
);
131 navigationButtons
.get(4).setEnabled(chapter
< max
);
136 chap
= getStory().getMeta().getResume();
138 name
= String
.format(" %s", chap
.getName());
140 name
= "[No RESUME]";
142 chap
= getStory().getChapters().get(chapter
- 1);
144 .format(" %d/%d: %s", chapter
, max
, chap
.getName());
147 while (name
.length() < getWidth() - chapterName
.getX()) {
151 chapterName
.setLabel(name
);
153 StringBuilder builder
= new StringBuilder();
155 String c
= String
.format("Chapter %d: %s", chapter
,
156 chap
== null ?
"[No RESUME]" : chap
.getName());
157 builder
.append(c
).append("\n");
158 for (int i
= 0; i
< c
.length(); i
++) {
161 builder
.append("\n\n");
163 for (Paragraph para
: chap
) {
164 if (para
.getType() == ParagraphType
.BREAK
) {
165 builder
.append("\n");
167 builder
.append(para
.getContent()).append("\n");
168 if (para
.getType() == ParagraphType
.BREAK
) {
169 builder
.append("\n");
173 textField
.setText(builder
.toString());
179 private Story
getStory() {
181 // TODO: progress bar?
182 story
= lib
.getStory(meta
.getLuid(), null);
187 private static String
desc(MetaData meta
) {
188 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());