26c1917deb441cf3939b3d669fcbdf89cc2c3323
[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.TText2;
13 import jexer.TWindow;
14 import jexer.event.TResizeEvent;
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix.data.Chapter;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.fanfix.data.Paragraph;
19 import be.nikiroo.fanfix.data.Story;
20
21 public class TuiReaderStoryWindow extends TWindow {
22 private MetaData meta;
23 private Story story;
24 private TText2 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 TText2(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 chapterName = addLabel("", 14, row);
72 chapterName.setWidth(getWidth() - 10);
73 setChapter(chapter);
74 }
75
76 @Override
77 public void onResize(TResizeEvent resize) {
78 super.onResize(resize);
79
80 // Resize the text field
81 textField.setWidth(resize.getWidth() - 2);
82 textField.setHeight(resize.getHeight() - 2);
83 textField.reflow();
84
85 // -3 because 0-based and 2 for borders
86 int row = getHeight() - 3;
87
88 String name = chapterName.getLabel();
89 while (name.length() < resize.getWidth() - chapterName.getX()) {
90 name += " ";
91 }
92 chapterName.setLabel(name);
93 chapterName.setWidth(resize.getWidth() - 10);
94 chapterName.setY(row);
95
96 for (TButton button : navigationButtons) {
97 button.setY(row);
98 }
99 }
100
101 private void setChapter(int chapter) {
102 if (chapter < 0) {
103 chapter = 0;
104 }
105
106 if (chapter > getStory().getChapters().size()) {
107 chapter = getStory().getChapters().size();
108 }
109
110 if (chapter != this.chapter) {
111 this.chapter = chapter;
112 Chapter chap;
113 String name;
114 if (chapter == 0) {
115 chap = getStory().getMeta().getResume();
116 name = String.format(" %s", chap.getName());
117 } else {
118 chap = getStory().getChapters().get(chapter - 1);
119 name = String.format(" %d/%d: %s", chapter, getStory()
120 .getChapters().size(), chap.getName());
121 }
122
123 while (name.length() < getWidth() - chapterName.getX()) {
124 name += " ";
125 }
126
127 chapterName.setLabel(name);
128
129 StringBuilder builder = new StringBuilder();
130 // TODO: i18n
131 String c = String.format("Chapter %d: %s", chapter, chap.getName());
132 builder.append(c).append("\n");
133 for (int i = 0; i < c.length(); i++) {
134 builder.append("═");
135 }
136 builder.append("\n\n");
137 for (Paragraph para : chap) {
138 builder.append(para.getContent()).append("\n\n");
139 }
140 textField.setText(builder.toString());
141 textField.reflow();
142 }
143 }
144
145 private Story getStory() {
146 if (story == null) {
147 // TODO: progress bar?
148 story = Instance.getLibrary().getStory(meta.getLuid(), null);
149 }
150 return story;
151 }
152
153 private static String desc(MetaData meta) {
154 return String.format("%s: %s", meta.getLuid(), meta.getTitle());
155 }
156 }