01db6e2d7b3309043055698779a765d2b4b74945
[fanfix.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderStoryWindow.java
1 package be.nikiroo.fanfix.reader.tui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import jexer.TAction;
7 import jexer.TApplication;
8 import jexer.TButton;
9 import jexer.TCommand;
10 import jexer.TKeypress;
11 import jexer.TLabel;
12 import jexer.TText;
13 import jexer.TWindow;
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;
22
23 class TuiReaderStoryWindow extends TWindow {
24 private BasicLibrary lib;
25 private MetaData meta;
26 private Story story;
27 private TText textField;
28 private int chapter = -1;
29 private List<TButton> navigationButtons;
30 private TLabel chapterName;
31
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);
36
37 this.lib = lib;
38 this.meta = meta;
39
40 // TODO: show all meta info before?
41
42 textField = new TText(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
43
44 statusBar = newStatusBar(desc(meta));
45 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
46
47 navigationButtons = new ArrayList<TButton>(5);
48
49 // -3 because 0-based and 2 for borders
50 int row = getHeight() - 3;
51
52 navigationButtons.add(addButton(" ", 0, row, null)); // for bg colour
53 // when <<
54 // button is
55 // pressed
56 navigationButtons.add(addButton("<< ", 0, row, new TAction() {
57 @Override
58 public void DO() {
59 setChapter(0);
60 }
61 }));
62 navigationButtons.add(addButton("< ", 4, row, new TAction() {
63 @Override
64 public void DO() {
65 setChapter(TuiReaderStoryWindow.this.chapter - 1);
66 }
67 }));
68 navigationButtons.add(addButton("> ", 7, row, new TAction() {
69 @Override
70 public void DO() {
71 setChapter(TuiReaderStoryWindow.this.chapter + 1);
72 }
73 }));
74 navigationButtons.add(addButton(">> ", 10, row, new TAction() {
75 @Override
76 public void DO() {
77 setChapter(getStory().getChapters().size());
78 }
79 }));
80
81 navigationButtons.get(0).setEnabled(false);
82 navigationButtons.get(1).setEnabled(false);
83 navigationButtons.get(2).setEnabled(false);
84
85 chapterName = addLabel("", 14, row);
86 chapterName.setWidth(getWidth() - 10);
87 setChapter(chapter);
88 }
89
90 @Override
91 public void onResize(TResizeEvent resize) {
92 super.onResize(resize);
93
94 // Resize the text field TODO: why setW/setH/reflow not enough for the
95 // scrollbars?
96 textField.onResize(new TResizeEvent(Type.WIDGET, resize.getWidth() - 2,
97 resize.getHeight() - 2));
98
99 // -3 because 0-based and 2 for borders
100 int row = getHeight() - 3;
101
102 String name = chapterName.getLabel();
103 while (name.length() < resize.getWidth() - chapterName.getX()) {
104 name += " ";
105 }
106 chapterName.setLabel(name);
107 chapterName.setWidth(resize.getWidth() - 10);
108 chapterName.setY(row);
109
110 for (TButton button : navigationButtons) {
111 button.setY(row);
112 }
113 }
114
115 private void setChapter(int chapter) {
116 if (chapter < 0) {
117 chapter = 0;
118 }
119
120 if (chapter > getStory().getChapters().size()) {
121 chapter = getStory().getChapters().size();
122 }
123
124 if (chapter != this.chapter) {
125 this.chapter = chapter;
126
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);
133
134 Chapter chap;
135 String name;
136 if (chapter == 0) {
137 chap = getStory().getMeta().getResume();
138 if (chap != null)
139 name = String.format(" %s", chap.getName());
140 else
141 name = "[No RESUME]";
142 } else {
143 chap = getStory().getChapters().get(chapter - 1);
144 name = String
145 .format(" %d/%d: %s", chapter, max, chap.getName());
146 }
147
148 while (name.length() < getWidth() - chapterName.getX()) {
149 name += " ";
150 }
151
152 chapterName.setLabel(name);
153
154 StringBuilder builder = new StringBuilder();
155 // TODO: i18n
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++) {
160 builder.append("═");
161 }
162 builder.append("\n\n");
163 if (chap != null) {
164 for (Paragraph para : chap) {
165 if (para.getType() == ParagraphType.BREAK) {
166 builder.append("\n");
167 }
168 builder.append(para.getContent()).append("\n");
169 if (para.getType() == ParagraphType.BREAK) {
170 builder.append("\n");
171 }
172 }
173 }
174 textField.setText(builder.toString());
175 textField.reflowData();
176 textField.toTop();
177 }
178 }
179
180 private Story getStory() {
181 if (story == null) {
182 // TODO: progress bar?
183 story = lib.getStory(meta.getLuid(), null);
184 }
185 return story;
186 }
187
188 private static String desc(MetaData meta) {
189 return String.format("%s: %s", meta.getLuid(), meta.getTitle());
190 }
191 }