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
.Instance
;
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 public class TuiReaderStoryWindow
extends TWindow
{
22 private MetaData meta
;
24 private TText textField
;
25 private int chapter
= -2;
26 private List
<TButton
> navigationButtons
;
27 private TLabel chapterName
;
29 public TuiReaderStoryWindow(TApplication app
, MetaData meta
) {
33 public TuiReaderStoryWindow(TApplication app
, MetaData meta
, int chapter
) {
34 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
37 // TODO: show all meta info before?
39 textField
= new TText(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
41 statusBar
= newStatusBar(desc(meta
));
42 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
44 navigationButtons
= new ArrayList
<TButton
>(5);
46 // -3 because 0-based and 2 for borders
47 int row
= getHeight() - 3;
49 navigationButtons
.add(addButton(" ", 0, row
, null)); // for bg colour when << button is pressed
50 navigationButtons
.add(addButton("<< ", 0, row
, new TAction() {
55 navigationButtons
.add(addButton("< ", 4, row
, new TAction() {
57 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
60 navigationButtons
.add(addButton("> ", 7, row
, new TAction() {
62 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
65 navigationButtons
.add(addButton(">> ", 10, row
, new TAction() {
67 setChapter(getStory().getChapters().size());
71 navigationButtons
.get(0).setEnabled(false);
72 navigationButtons
.get(1).setEnabled(false);
73 navigationButtons
.get(2).setEnabled(false);
75 chapterName
= addLabel("", 14, row
);
76 chapterName
.setWidth(getWidth() - 10);
81 public void onResize(TResizeEvent resize
) {
82 super.onResize(resize
);
84 // Resize the text field
85 textField
.setWidth(resize
.getWidth() - 2);
86 textField
.setHeight(resize
.getHeight() - 2);
89 // -3 because 0-based and 2 for borders
90 int row
= getHeight() - 3;
92 String name
= chapterName
.getLabel();
93 while (name
.length() < resize
.getWidth() - chapterName
.getX()) {
96 chapterName
.setLabel(name
);
97 chapterName
.setWidth(resize
.getWidth() - 10);
98 chapterName
.setY(row
);
100 for (TButton button
: navigationButtons
) {
105 private void setChapter(int chapter
) {
110 if (chapter
> getStory().getChapters().size()) {
111 chapter
= getStory().getChapters().size();
114 if (chapter
!= this.chapter
) {
115 this.chapter
= chapter
;
117 int max
= getStory().getChapters().size();
118 navigationButtons
.get(0).setEnabled(chapter
> 0);
119 navigationButtons
.get(1).setEnabled(chapter
> 0);
120 navigationButtons
.get(2).setEnabled(chapter
> 0);
121 navigationButtons
.get(3).setEnabled(chapter
< max
);
122 navigationButtons
.get(4).setEnabled(chapter
< max
);
127 chap
= getStory().getMeta().getResume();
128 name
= String
.format(" %s", chap
.getName());
130 chap
= getStory().getChapters().get(chapter
- 1);
131 name
= String
.format(" %d/%d: %s", chapter
, max
, chap
.getName());
134 while (name
.length() < getWidth() - chapterName
.getX()) {
138 chapterName
.setLabel(name
);
140 StringBuilder builder
= new StringBuilder();
142 String c
= String
.format("Chapter %d: %s", chapter
, chap
.getName());
143 builder
.append(c
).append("\n");
144 for (int i
= 0; i
< c
.length(); i
++) {
147 builder
.append("\n\n");
148 for (Paragraph para
: chap
) {
149 builder
.append(para
.getContent()).append("\n\n");
151 textField
.setText(builder
.toString());
157 private Story
getStory() {
159 // TODO: progress bar?
160 story
= Instance
.getLibrary().getStory(meta
.getLuid(), null);
165 private static String
desc(MetaData meta
) {
166 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());