1 package be
.nikiroo
.fanfix
.reader
;
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
.Story
;
19 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
21 class TuiReaderStoryWindow
extends TWindow
{
22 private BasicLibrary lib
;
23 private MetaData meta
;
25 private TText textField
;
26 private int chapter
= -1;
27 private List
<TButton
> navigationButtons
;
28 private TLabel chapterName
;
30 // chapter: -1 for "none" (0 is desc)
31 public TuiReaderStoryWindow(TApplication app
, BasicLibrary lib
,
32 MetaData meta
, int chapter
) {
33 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
38 // TODO: show all meta info before?
40 textField
= new TText(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
42 statusBar
= newStatusBar(desc(meta
));
43 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
45 navigationButtons
= new ArrayList
<TButton
>(5);
47 // -3 because 0-based and 2 for borders
48 int row
= getHeight() - 3;
50 navigationButtons
.add(addButton(" ", 0, row
, null)); // for bg colour
54 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
59 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
61 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
64 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
66 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
69 navigationButtons
.add(addButton(">> ", 10, row
, new TAction() {
71 setChapter(getStory().getChapters().size());
75 navigationButtons
.get(0).setEnabled(false);
76 navigationButtons
.get(1).setEnabled(false);
77 navigationButtons
.get(2).setEnabled(false);
79 chapterName
= addLabel("", 14, row
);
80 chapterName
.setWidth(getWidth() - 10);
85 public void onResize(TResizeEvent resize
) {
86 super.onResize(resize
);
88 // Resize the text field
89 textField
.setWidth(resize
.getWidth() - 2);
90 textField
.setHeight(resize
.getHeight() - 2);
93 // -3 because 0-based and 2 for borders
94 int row
= getHeight() - 3;
96 String name
= chapterName
.getLabel();
97 while (name
.length() < resize
.getWidth() - chapterName
.getX()) {
100 chapterName
.setLabel(name
);
101 chapterName
.setWidth(resize
.getWidth() - 10);
102 chapterName
.setY(row
);
104 for (TButton button
: navigationButtons
) {
109 private void setChapter(int chapter
) {
114 if (chapter
> getStory().getChapters().size()) {
115 chapter
= getStory().getChapters().size();
118 if (chapter
!= this.chapter
) {
119 this.chapter
= chapter
;
121 int max
= getStory().getChapters().size();
122 navigationButtons
.get(0).setEnabled(chapter
> 0);
123 navigationButtons
.get(1).setEnabled(chapter
> 0);
124 navigationButtons
.get(2).setEnabled(chapter
> 0);
125 navigationButtons
.get(3).setEnabled(chapter
< max
);
126 navigationButtons
.get(4).setEnabled(chapter
< max
);
131 chap
= getStory().getMeta().getResume();
132 name
= String
.format(" %s", chap
.getName());
134 chap
= getStory().getChapters().get(chapter
- 1);
136 .format(" %d/%d: %s", chapter
, max
, chap
.getName());
139 while (name
.length() < getWidth() - chapterName
.getX()) {
143 chapterName
.setLabel(name
);
145 StringBuilder builder
= new StringBuilder();
147 String c
= String
.format("Chapter %d: %s", chapter
, chap
.getName());
148 builder
.append(c
).append("\n");
149 for (int i
= 0; i
< c
.length(); i
++) {
152 builder
.append("\n\n");
153 for (Paragraph para
: chap
) {
154 builder
.append(para
.getContent()).append("\n\n");
156 textField
.setText(builder
.toString());
162 private Story
getStory() {
164 // TODO: progress bar?
165 story
= lib
.getStory(meta
.getLuid(), null);
170 private static String
desc(MetaData meta
) {
171 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());