01db6e2d7b3309043055698779a765d2b4b74945
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 jexer
.event
.TResizeEvent
.Type
;
16 import be
.nikiroo
.fanfix
.data
.Chapter
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
18 import be
.nikiroo
.fanfix
.data
.Paragraph
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
20 import be
.nikiroo
.fanfix
.data
.Story
;
21 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
23 class TuiReaderStoryWindow
extends TWindow
{
24 private BasicLibrary lib
;
25 private MetaData meta
;
27 private TText textField
;
28 private int chapter
= -1;
29 private List
<TButton
> navigationButtons
;
30 private TLabel chapterName
;
32 // chapter: -1 for "none" (0 is desc)
33 public TuiReaderStoryWindow(TApplication app
, BasicLibrary lib
,
34 MetaData meta
, int chapter
) {
35 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
40 // TODO: show all meta info before?
42 textField
= new TText(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
44 statusBar
= newStatusBar(desc(meta
));
45 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
47 navigationButtons
= new ArrayList
<TButton
>(5);
49 // -3 because 0-based and 2 for borders
50 int row
= getHeight() - 3;
52 navigationButtons
.add(addButton(" ", 0, row
, null)); // for bg colour
56 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
62 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
65 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
68 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
71 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
74 navigationButtons
.add(addButton(">> ", 10, row
, new TAction() {
77 setChapter(getStory().getChapters().size());
81 navigationButtons
.get(0).setEnabled(false);
82 navigationButtons
.get(1).setEnabled(false);
83 navigationButtons
.get(2).setEnabled(false);
85 chapterName
= addLabel("", 14, row
);
86 chapterName
.setWidth(getWidth() - 10);
91 public void onResize(TResizeEvent resize
) {
92 super.onResize(resize
);
94 // Resize the text field TODO: why setW/setH/reflow not enough for the
96 textField
.onResize(new TResizeEvent(Type
.WIDGET
, resize
.getWidth() - 2,
97 resize
.getHeight() - 2));
99 // -3 because 0-based and 2 for borders
100 int row
= getHeight() - 3;
102 String name
= chapterName
.getLabel();
103 while (name
.length() < resize
.getWidth() - chapterName
.getX()) {
106 chapterName
.setLabel(name
);
107 chapterName
.setWidth(resize
.getWidth() - 10);
108 chapterName
.setY(row
);
110 for (TButton button
: navigationButtons
) {
115 private void setChapter(int chapter
) {
120 if (chapter
> getStory().getChapters().size()) {
121 chapter
= getStory().getChapters().size();
124 if (chapter
!= this.chapter
) {
125 this.chapter
= chapter
;
127 int max
= getStory().getChapters().size();
128 navigationButtons
.get(0).setEnabled(chapter
> 0);
129 navigationButtons
.get(1).setEnabled(chapter
> 0);
130 navigationButtons
.get(2).setEnabled(chapter
> 0);
131 navigationButtons
.get(3).setEnabled(chapter
< max
);
132 navigationButtons
.get(4).setEnabled(chapter
< max
);
137 chap
= getStory().getMeta().getResume();
139 name
= String
.format(" %s", chap
.getName());
141 name
= "[No RESUME]";
143 chap
= getStory().getChapters().get(chapter
- 1);
145 .format(" %d/%d: %s", chapter
, max
, chap
.getName());
148 while (name
.length() < getWidth() - chapterName
.getX()) {
152 chapterName
.setLabel(name
);
154 StringBuilder builder
= new StringBuilder();
156 String c
= String
.format("Chapter %d: %s", chapter
,
157 chap
== null ?
"[No RESUME]" : chap
.getName());
158 builder
.append(c
).append("\n");
159 for (int i
= 0; i
< c
.length(); i
++) {
162 builder
.append("\n\n");
164 for (Paragraph para
: chap
) {
165 if (para
.getType() == ParagraphType
.BREAK
) {
166 builder
.append("\n");
168 builder
.append(para
.getContent()).append("\n");
169 if (para
.getType() == ParagraphType
.BREAK
) {
170 builder
.append("\n");
174 textField
.setText(builder
.toString());
175 textField
.reflowData();
180 private Story
getStory() {
182 // TODO: progress bar?
183 story
= lib
.getStory(meta
.getLuid(), null);
188 private static String
desc(MetaData meta
) {
189 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());