b1ebcc2e6971de4c25e119c5e6fbbb7a32043844
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderMainWindow.java
1 package be.nikiroo.fanfix.reader.tui;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import jexer.TAction;
8 import jexer.TComboBox;
9 import jexer.TCommand;
10 import jexer.TField;
11 import jexer.TFileOpenBox.Type;
12 import jexer.TKeypress;
13 import jexer.TLabel;
14 import jexer.TList;
15 import jexer.TStatusBar;
16 import jexer.TWindow;
17 import jexer.event.TCommandEvent;
18 import jexer.event.TKeypressEvent;
19 import jexer.event.TMenuEvent;
20 import jexer.event.TResizeEvent;
21 import be.nikiroo.fanfix.Instance;
22 import be.nikiroo.fanfix.data.MetaData;
23 import be.nikiroo.fanfix.library.BasicLibrary;
24 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
25 import be.nikiroo.jexer.TSizeConstraint;
26
27 /**
28 * The library window, that will list all the (filtered) stories available in
29 * this {@link BasicLibrary}.
30 *
31 * @author niki
32 */
33 class TuiReaderMainWindow extends TWindow {
34 public static final int MENU_SEARCH = 1100;
35 public static final TCommand CMD_SEARCH = new TCommand(MENU_SEARCH) {
36 };
37
38 public enum Mode {
39 SOURCE, AUTHOR,
40 }
41
42 private TList list;
43 private List<MetaData> listKeys;
44 private List<String> listItems;
45 private TuiReaderApplication reader;
46
47 private Mode mode = Mode.SOURCE;
48 private String target = null;
49 private String filter = "";
50
51 private List<TSizeConstraint> sizeConstraints = new ArrayList<TSizeConstraint>();
52
53 // The 2 comboboxes used to select by source/author
54 private TComboBox selectTargetBox;
55 private TComboBox selectBox;
56
57 /**
58 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
59 *
60 * @param reader
61 * the reader and main application
62 */
63 public TuiReaderMainWindow(TuiReaderApplication reader) {
64 // Construct a demo window. X and Y don't matter because it will be
65 // centred on screen.
66 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE);
67
68 this.reader = reader;
69
70 listKeys = new ArrayList<MetaData>();
71 listItems = new ArrayList<String>();
72
73 addList();
74 addSearch();
75 addSelect();
76
77 TStatusBar statusBar = reader.setStatusBar(this, "Library");
78 statusBar.addShortcutKeypress(TKeypress.kbCtrlF, CMD_SEARCH, "Search");
79
80 TSizeConstraint.resize(sizeConstraints);
81
82 // TODO: remove when not used anymore
83
84 // addLabel("Label (1,1)", 1, 1);
85 // addButton("&Button (35,1)", 35, 1, new TAction() {
86 // public void DO() {
87 // }
88 // });
89 // addCheckbox(1, 2, "Checky (1,2)", false);
90 // addProgressBar(1, 3, 30, 42);
91 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
92 // groupy.addRadioButton("Fanfan");
93 // groupy.addRadioButton("Tulipe");
94 // addField(1, 10, 20, false, "text not fixed.");
95 // addField(1, 11, 20, true, "text fixed.");
96 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
97 //
98 // TTreeView tree = addTreeView(30, 5, 20, 5);
99 // TTreeItem root = new TTreeItem(tree, "expended root", true);
100 // tree.setSelected(root); // needed to allow arrow navigation without
101 // // mouse-clicking before
102 //
103 // root.addChild("child");
104 // root.addChild("child 2").addChild("sub child");
105 }
106
107 private void addSearch() {
108 TLabel lblSearch = addLabel("Search: ", 0, 0);
109
110 TField search = new TField(this, 0, 0, 1, true) {
111 @Override
112 public void onKeypress(TKeypressEvent keypress) {
113 super.onKeypress(keypress);
114 TKeypress key = keypress.getKey();
115 if (key.isFnKey() && key.getKeyCode() == TKeypress.ENTER) {
116 TuiReaderMainWindow.this.filter = getText();
117 TuiReaderMainWindow.this.refreshStories();
118 }
119 }
120 };
121
122 TSizeConstraint.setSize(sizeConstraints, lblSearch, 5, 1, null, null);
123 TSizeConstraint.setSize(sizeConstraints, search, 15, 1, -5, null);
124 }
125
126 private void addList() {
127 list = addList(listItems, 0, 0, 10, 10, new TAction() {
128 @Override
129 public void DO() {
130 MetaData meta = getSelectedMeta();
131 if (meta != null) {
132 readStory(meta);
133 }
134 }
135 });
136
137 TSizeConstraint.setSize(sizeConstraints, list, 0, 7, 0, 0);
138 }
139
140 private void addSelect() {
141 // TODO: i18n
142 final List<String> selects = new ArrayList<String>();
143 selects.add("(show all)");
144 selects.add("Sources");
145 selects.add("Author");
146
147 final List<String> selectTargets = new ArrayList<String>();
148 selectTargets.add("");
149
150 TLabel lblSelect = addLabel("Select: ", 0, 0);
151
152 TAction onSelect = new TAction() {
153 @Override
154 public void DO() {
155 String smode = selectBox.getText();
156 boolean showTarget;
157 if (smode == null || smode.equals("(show all)")) {
158 showTarget = false;
159 } else if (smode.equals("Sources")) {
160 selectTargets.clear();
161 selectTargets.add("(show all)");
162 try {
163 for (String source : reader.getLibrary().getSources()) {
164 selectTargets.add(source);
165 }
166 } catch (IOException e) {
167 Instance.getInstance().getTraceHandler().error(e);
168 }
169
170 showTarget = true;
171 } else {
172 selectTargets.clear();
173 selectTargets.add("(show all)");
174 try {
175 for (String author : reader.getLibrary().getAuthors()) {
176 selectTargets.add(author);
177 }
178 } catch (IOException e) {
179 Instance.getInstance().getTraceHandler().error(e);
180 }
181
182 showTarget = true;
183 }
184
185 selectTargetBox.setVisible(showTarget);
186 selectTargetBox.setEnabled(showTarget);
187 if (showTarget) {
188 selectTargetBox.reflowData();
189 }
190
191 selectTargetBox.setText(selectTargets.get(0));
192 if (showTarget) {
193 TuiReaderMainWindow.this.activate(selectTargetBox);
194 } else {
195 TuiReaderMainWindow.this.activate(list);
196 }
197 }
198 };
199
200 selectBox = addComboBox(0, 0, 10, selects, 0, -1, onSelect);
201
202 selectTargetBox = addComboBox(0, 0, 0, selectTargets, 0, -1,
203 new TAction() {
204 @Override
205 public void DO() {
206 if (selectTargetBox.getText().equals(
207 selectTargets.get(0))) {
208 setMode(mode, null);
209 } else {
210 setMode(mode, selectTargetBox.getText());
211 }
212 }
213 });
214
215 // Set defaults
216 onSelect.DO();
217
218 TSizeConstraint.setSize(sizeConstraints, lblSelect, 5, 3, null, null);
219 TSizeConstraint.setSize(sizeConstraints, selectBox, 15, 3, -5, null);
220 TSizeConstraint.setSize(sizeConstraints, selectTargetBox, 15, 4, -5,
221 null);
222 }
223
224 @Override
225 public void onResize(TResizeEvent resize) {
226 super.onResize(resize);
227 TSizeConstraint.resize(sizeConstraints);
228 }
229
230 @Override
231 public void onClose() {
232 setVisible(false);
233 super.onClose();
234 }
235
236 /**
237 * Refresh the list of stories displayed in this library.
238 * <p>
239 * Will take the current settings into account (filter, source...).
240 */
241 public void refreshStories() {
242 List<MetaData> metas;
243
244 try {
245 if (mode == Mode.SOURCE) {
246 metas = reader.getLibrary().getListBySource(target);
247 } else if (mode == Mode.AUTHOR) {
248 metas = reader.getLibrary().getListByAuthor(target);
249 } else {
250 metas = reader.getLibrary().getList();
251 }
252 } catch (IOException e) {
253 Instance.getInstance().getTraceHandler().error(e);
254 metas = new ArrayList<MetaData>();
255 }
256
257 setMetas(metas);
258 }
259
260 /**
261 * Change the author/source filter and display all stories matching this
262 * target.
263 *
264 * @param mode
265 * the new mode or NULL for no sorting
266 * @param target
267 * the actual target for the given mode, or NULL for all of them
268 */
269 public void setMode(Mode mode, String target) {
270 this.mode = mode;
271 this.target = target;
272 refreshStories();
273 }
274
275 /**
276 * Update the list of stories displayed in this {@link TWindow}.
277 * <p>
278 * If a filter is set, only the stories which pass the filter will be
279 * displayed.
280 *
281 * @param metas
282 * the new list of stories to display
283 */
284 private void setMetas(List<MetaData> metas) {
285 listKeys.clear();
286 listItems.clear();
287
288 if (metas != null) {
289 for (MetaData meta : metas) {
290 String desc = desc(meta);
291 if (filter.isEmpty()
292 || desc.toLowerCase().contains(filter.toLowerCase())) {
293 listKeys.add(meta);
294 listItems.add(desc);
295 }
296 }
297 }
298
299 list.setList(listItems);
300 if (listItems.size() > 0) {
301 list.setSelectedIndex(0);
302 }
303 }
304
305 public MetaData getSelectedMeta() {
306 if (list.getSelectedIndex() >= 0) {
307 return listKeys.get(list.getSelectedIndex());
308 }
309
310 return null;
311 }
312
313 public void readStory(MetaData meta) {
314 try {
315 reader.setChapter(-1);
316 reader.setMeta(meta);
317 reader.read(false);
318 } catch (IOException e) {
319 Instance.getInstance().getTraceHandler().error(e);
320 }
321 }
322
323 private String desc(MetaData meta) {
324 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
325 }
326
327 @Override
328 public void onCommand(TCommandEvent command) {
329 if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) {
330 TuiReaderApplication.close(this);
331 } else {
332 // Handle our own event if needed here
333 super.onCommand(command);
334 }
335 }
336
337 @Override
338 public void onMenu(TMenuEvent menu) {
339 MetaData meta = getSelectedMeta();
340 if (meta != null) {
341 switch (menu.getId()) {
342 case TuiReaderApplication.MENU_FILE_OPEN:
343 readStory(meta);
344
345 return;
346 case TuiReaderApplication.MENU_FILE_EXPORT:
347
348 try {
349 // TODO: choose type, pg, error
350 OutputType outputType = OutputType.EPUB;
351 String path = fileOpenBox(".", Type.SAVE);
352 reader.getLibrary().export(meta.getLuid(), outputType,
353 path, null);
354 } catch (IOException e) {
355 // TODO
356 e.printStackTrace();
357 }
358
359 return;
360
361 case -1:
362 try {
363 reader.getLibrary().delete(meta.getLuid());
364 } catch (IOException e) {
365 // TODO
366 }
367
368 return;
369 }
370 }
371
372 super.onMenu(menu);
373 }
374 }