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