9613e63258c4c6bdbd7ad5f851842509d4d3bdb7
1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.util
.ArrayList
;
7 import jexer
.event
.TResizeEvent
;
8 import be
.nikiroo
.fanfix
.Instance
;
9 import be
.nikiroo
.fanfix
.data
.Chapter
;
10 import be
.nikiroo
.fanfix
.data
.MetaData
;
11 import be
.nikiroo
.fanfix
.data
.Paragraph
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
14 public class TuiReaderStoryWindow
extends TWindow
{
15 private MetaData meta
;
17 private TText textField
;
18 private int chapter
= -2;
19 private List
<TButton
> navigationButtons
;
20 private TLabel chapterName
;
22 public TuiReaderStoryWindow(TApplication app
, MetaData meta
) {
26 public TuiReaderStoryWindow(TApplication app
, MetaData meta
, int chapter
) {
27 super(app
, desc(meta
), 0, 0, 60, 18, CENTERED
| RESIZABLE
);
30 // TODO: show all meta info before?
32 // -2 because 0-based, 2 for borders, -1 to hide the HScroll
33 textField
= addText("", 0, 0, getWidth() - 2, getHeight() - 2);
35 statusBar
= newStatusBar(desc(meta
));
36 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
38 navigationButtons
= new ArrayList
<TButton
>(4);
40 // -3 because 0-based and 2 for borders
41 int row
= getHeight() - 3;
43 navigationButtons
.add(addButton("<<", 0, row
, new TAction() {
48 navigationButtons
.add(addButton("<", 3, row
, new TAction() {
50 setChapter(TuiReaderStoryWindow
.this.chapter
- 1);
53 navigationButtons
.add(addButton(">", 5, row
, new TAction() {
55 setChapter(TuiReaderStoryWindow
.this.chapter
+ 1);
58 navigationButtons
.add(addButton(">>", 7, row
, new TAction() {
60 setChapter(getStory().getChapters().size());
64 chapterName
= addLabel("", 11, row
);
65 chapterName
.setWidth(getWidth() - 10);
70 public void onResize(TResizeEvent resize
) {
71 super.onResize(resize
);
73 // Resize the text field
74 textField
.setWidth(resize
.getWidth());
75 textField
.setHeight(resize
.getHeight() - 2);
78 // -3 because 0-based and 2 for borders
79 int row
= getHeight() - 3;
81 String name
= chapterName
.getLabel();
82 while (name
.length() < resize
.getWidth() - chapterName
.getX()) {
85 chapterName
.setLabel(name
);
86 chapterName
.setWidth(resize
.getWidth() - 10);
87 chapterName
.setY(row
);
89 for (TButton button
: navigationButtons
) {
94 private void setChapter(int chapter
) {
99 if (chapter
> getStory().getChapters().size()) {
100 chapter
= getStory().getChapters().size();
103 if (chapter
!= this.chapter
) {
104 this.chapter
= chapter
;
108 chap
= getStory().getMeta().getResume();
109 name
= String
.format(" %s", chap
.getName());
111 chap
= getStory().getChapters().get(chapter
- 1);
112 name
= String
.format(" %d/%d: %s", chapter
, getStory()
113 .getChapters().size(), chap
.getName());
116 while (name
.length() < getWidth() - chapterName
.getX()) {
120 chapterName
.setLabel(name
);
122 StringBuilder builder
= new StringBuilder();
124 String c
= String
.format("Chapter %d: %s", chapter
, chap
.getName());
125 builder
.append(c
).append("\n");
126 for (int i
= 0; i
< c
.length(); i
++) {
129 builder
.append("\n\n");
130 for (Paragraph para
: chap
) {
131 builder
.append(para
.getContent()).append("\n\n");
133 textField
.setText(builder
.toString());
138 private Story
getStory() {
140 // TODO: progress bar?
141 story
= Instance
.getLibrary().getStory(meta
.getLuid(), null);
146 private static String
desc(MetaData meta
) {
147 return String
.format("%s: %s", meta
.getLuid(), meta
.getTitle());