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