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