TUI: Disable "< << > >>" buttons as required
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderStoryWindow.java
CommitLineData
c1873e56
NR
1package be.nikiroo.fanfix.reader;
2
edfd3577
NR
3import java.util.ArrayList;
4import java.util.List;
5
0861d62a
NR
6import jexer.TAction;
7import jexer.TApplication;
8import jexer.TButton;
9import jexer.TCommand;
10import jexer.TKeypress;
11import jexer.TLabel;
12import jexer.TText2;
13import jexer.TWindow;
c1873e56
NR
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;
0861d62a 24 private TText2 textField;
edfd3577
NR
25 private int chapter = -2;
26 private List<TButton> navigationButtons;
27 private TLabel chapterName;
c1873e56
NR
28
29 public TuiReaderStoryWindow(TApplication app, MetaData meta) {
edfd3577
NR
30 this(app, meta, 0);
31 }
32
33 public TuiReaderStoryWindow(TApplication app, MetaData meta, int chapter) {
c1873e56
NR
34 super(app, desc(meta), 0, 0, 60, 18, CENTERED | RESIZABLE);
35 this.meta = meta;
36
edfd3577 37 // TODO: show all meta info before?
c1873e56 38
0861d62a 39 textField = new TText2(this, "", 0, 0, getWidth() - 2, getHeight() - 2);
edfd3577 40
396e924c
NR
41 statusBar = newStatusBar(desc(meta));
42 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
edfd3577 43
0b7e3d78 44 navigationButtons = new ArrayList<TButton>(5);
edfd3577 45
396e924c 46 // -3 because 0-based and 2 for borders
edfd3577
NR
47 int row = getHeight() - 3;
48
0b7e3d78 49 navigationButtons.add(addButton(" ", 0, row, null)); // for bg colour when << button is pressed
0861d62a 50 navigationButtons.add(addButton("<< ", 0, row, new TAction() {
396e924c 51 public void DO() {
edfd3577 52 setChapter(0);
396e924c 53 }
edfd3577 54 }));
0861d62a 55 navigationButtons.add(addButton("< ", 4, row, new TAction() {
edfd3577
NR
56 public void DO() {
57 setChapter(TuiReaderStoryWindow.this.chapter - 1);
58 }
59 }));
0861d62a 60 navigationButtons.add(addButton("> ", 7, row, new TAction() {
edfd3577
NR
61 public void DO() {
62 setChapter(TuiReaderStoryWindow.this.chapter + 1);
63 }
64 }));
0861d62a 65 navigationButtons.add(addButton(">> ", 10, row, new TAction() {
edfd3577
NR
66 public void DO() {
67 setChapter(getStory().getChapters().size());
68 }
69 }));
8670a283
NR
70
71 navigationButtons.get(0).setEnabled(false);
72 navigationButtons.get(1).setEnabled(false);
73 navigationButtons.get(2).setEnabled(false);
edfd3577 74
0861d62a 75 chapterName = addLabel("", 14, row);
edfd3577
NR
76 chapterName.setWidth(getWidth() - 10);
77 setChapter(chapter);
c1873e56
NR
78 }
79
80 @Override
81 public void onResize(TResizeEvent resize) {
82 super.onResize(resize);
83
84 // Resize the text field
0861d62a 85 textField.setWidth(resize.getWidth() - 2);
396e924c 86 textField.setHeight(resize.getHeight() - 2);
c1873e56 87 textField.reflow();
edfd3577
NR
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;
8670a283
NR
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
edfd3577
NR
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);
8670a283 131 name = String.format(" %d/%d: %s", chapter, max, chap.getName());
edfd3577
NR
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 }
c1873e56
NR
154 }
155
156 private Story getStory() {
157 if (story == null) {
edfd3577 158 // TODO: progress bar?
c1873e56
NR
159 story = Instance.getLibrary().getStory(meta.getLuid(), null);
160 }
161 return story;
162 }
163
164 private static String desc(MetaData meta) {
165 return String.format("%s: %s", meta.getLuid(), meta.getTitle());
166 }
167}