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