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