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
.BasicLibrary
;
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
.Story
;
21 class TuiReaderStoryWindow
extends TWindow
{
22 private BasicLibrary lib
;
23 private MetaData meta
;
25 private TText textField
;
26 private int chapter
= -2;
27 private List
<TButton
> navigationButtons
;
28 private TLabel chapterName
;
30 public TuiReaderStoryWindow(TApplication app
, BasicLibrary lib
,
32 this(app
, lib
, meta
, 0);
35 public TuiReaderStoryWindow(TApplication app
, BasicLibrary lib
,
36 MetaData meta
, int chapter
) {
37 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
42 // TODO: show all meta info before?
44 textField
= new TText(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
46 statusBar
= newStatusBar(desc(meta
));
47 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
49 navigationButtons
= new ArrayList
<TButton
>(5);
51 // -3 because 0-based and 2 for borders
52 int row
= getHeight() - 3;
54 navigationButtons
.add(addButton(" ", 0, row
, null)); // for bg colour
58 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
63 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
65 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
68 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
70 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
73 navigationButtons
.add(addButton(">> ", 10, row
, new TAction() {
75 setChapter(getStory().getChapters().size());
79 navigationButtons
.get(0).setEnabled(false);
80 navigationButtons
.get(1).setEnabled(false);
81 navigationButtons
.get(2).setEnabled(false);
83 chapterName
= addLabel("", 14, row
);
84 chapterName
.setWidth(getWidth() - 10);
89 public void onResize(TResizeEvent resize
) {
90 super.onResize(resize
);
92 // Resize the text field
93 textField
.setWidth(resize
.getWidth() - 2);
94 textField
.setHeight(resize
.getHeight() - 2);
97 // -3 because 0-based and 2 for borders
98 int row
= getHeight() - 3;
100 String name
= chapterName
.getLabel();
101 while (name
.length() < resize
.getWidth() - chapterName
.getX()) {
104 chapterName
.setLabel(name
);
105 chapterName
.setWidth(resize
.getWidth() - 10);
106 chapterName
.setY(row
);
108 for (TButton button
: navigationButtons
) {
113 private void setChapter(int chapter
) {
118 if (chapter
> getStory().getChapters().size()) {
119 chapter
= getStory().getChapters().size();
122 if (chapter
!= this.chapter
) {
123 this.chapter
= chapter
;
125 int max
= getStory().getChapters().size();
126 navigationButtons
.get(0).setEnabled(chapter
> 0);
127 navigationButtons
.get(1).setEnabled(chapter
> 0);
128 navigationButtons
.get(2).setEnabled(chapter
> 0);
129 navigationButtons
.get(3).setEnabled(chapter
< max
);
130 navigationButtons
.get(4).setEnabled(chapter
< max
);
135 chap
= getStory().getMeta().getResume();
136 name
= String
.format(" %s", chap
.getName());
138 chap
= getStory().getChapters().get(chapter
- 1);
140 .format(" %d/%d: %s", chapter
, max
, chap
.getName());
143 while (name
.length() < getWidth() - chapterName
.getX()) {
147 chapterName
.setLabel(name
);
149 StringBuilder builder
= new StringBuilder();
151 String c
= String
.format("Chapter %d: %s", chapter
, chap
.getName());
152 builder
.append(c
).append("\n");
153 for (int i
= 0; i
< c
.length(); i
++) {
156 builder
.append("\n\n");
157 for (Paragraph para
: chap
) {
158 builder
.append(para
.getContent()).append("\n\n");
160 textField
.setText(builder
.toString());
166 private Story
getStory() {
168 // TODO: progress bar?
169 story
= lib
.getStory(meta
.getLuid(), null);
174 private static String
desc(MetaData meta
) {
175 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());