full selection
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderMainWindow.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.reader.tui;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6
7import jexer.TAction;
8import jexer.TComboBox;
9import jexer.TCommand;
10import jexer.TField;
11import jexer.TFileOpenBox.Type;
12import jexer.TKeypress;
13import jexer.TLabel;
14import jexer.TList;
15import jexer.TStatusBar;
16import jexer.TWindow;
17import jexer.event.TCommandEvent;
18import jexer.event.TKeypressEvent;
19import jexer.event.TMenuEvent;
20import jexer.event.TResizeEvent;
21import be.nikiroo.fanfix.Instance;
22import be.nikiroo.fanfix.data.MetaData;
23import be.nikiroo.fanfix.library.BasicLibrary;
24import be.nikiroo.fanfix.output.BasicOutput.OutputType;
25import 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 */
33class 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 for (String source : reader.getLibrary().getSources()) {
163 selectTargets.add(source);
164 }
165 showTarget = true;
166 } else {
167 selectTargets.clear();
168 selectTargets.add("(show all)");
169 for (String author : reader.getLibrary().getAuthors()) {
170 selectTargets.add(author);
171 }
172
173 showTarget = true;
174 }
175
176 selectTargetBox.setVisible(showTarget);
177 selectTargetBox.setEnabled(showTarget);
178 if (showTarget) {
179 selectTargetBox.reflowData();
180 }
181
182 selectTargetBox.setText(selectTargets.get(0));
183 if (showTarget) {
184 TuiReaderMainWindow.this.activate(selectTargetBox);
185 } else {
186 TuiReaderMainWindow.this.activate(list);
187 }
188 }
189 };
190
191 selectBox = addComboBox(0, 0, 10, selects, 0, -1, onSelect);
192
193 selectTargetBox = addComboBox(0, 0, 0, selectTargets, 0, -1,
194 new TAction() {
195 @Override
196 public void DO() {
197 if (selectTargetBox.getText().equals(
198 selectTargets.get(0))) {
199 setMode(mode, null);
200 } else {
201 setMode(mode, selectTargetBox.getText());
202 }
203 }
204 });
205
206 // Set defaults
207 onSelect.DO();
208
209 TSizeConstraint.setSize(sizeConstraints, lblSelect, 5, 3, null, null);
210 TSizeConstraint.setSize(sizeConstraints, selectBox, 15, 3, -5, null);
211 TSizeConstraint.setSize(sizeConstraints, selectTargetBox, 15, 4, -5,
212 null);
213 }
214
215 @Override
216 public void onResize(TResizeEvent resize) {
217 super.onResize(resize);
218 TSizeConstraint.resize(sizeConstraints);
219 }
220
221 @Override
222 public void onClose() {
223 setVisible(false);
224 super.onClose();
225 }
226
227 /**
228 * Refresh the list of stories displayed in this library.
229 * <p>
230 * Will take the current settings into account (filter, source...).
231 */
232 public void refreshStories() {
233 List<MetaData> metas;
234 if (mode == Mode.SOURCE) {
235 metas = reader.getLibrary().getListBySource(target);
236 } else if (mode == Mode.AUTHOR) {
237 metas = reader.getLibrary().getListByAuthor(target);
238 } else {
239 metas = reader.getLibrary().getList();
240 }
241
242 setMetas(metas);
243 }
244
245 /**
246 * Change the author/source filter and display all stories matching this
247 * target.
248 *
249 * @param mode
250 * the new mode or NULL for no sorting
251 * @param target
252 * the actual target for the given mode, or NULL for all of them
253 */
254 public void setMode(Mode mode, String target) {
255 this.mode = mode;
256 this.target = target;
257 refreshStories();
258 }
259
260 /**
261 * Update the list of stories displayed in this {@link TWindow}.
262 * <p>
263 * If a filter is set, only the stories which pass the filter will be
264 * displayed.
265 *
266 * @param metas
267 * the new list of stories to display
268 */
269 private void setMetas(List<MetaData> metas) {
270 listKeys.clear();
271 listItems.clear();
272
273 if (metas != null) {
274 for (MetaData meta : metas) {
275 String desc = desc(meta);
276 if (filter.isEmpty()
277 || desc.toLowerCase().contains(filter.toLowerCase())) {
278 listKeys.add(meta);
279 listItems.add(desc);
280 }
281 }
282 }
283
284 list.setList(listItems);
285 if (listItems.size() > 0) {
286 list.setSelectedIndex(0);
287 }
288 }
289
290 public MetaData getSelectedMeta() {
291 if (list.getSelectedIndex() >= 0) {
292 return listKeys.get(list.getSelectedIndex());
293 }
294
295 return null;
296 }
297
298 public void readStory(MetaData meta) {
299 try {
300 reader.setChapter(-1);
301 reader.setMeta(meta);
302 reader.read(false);
303 } catch (IOException e) {
304 Instance.getTraceHandler().error(e);
305 }
306 }
307
308 private String desc(MetaData meta) {
309 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
310 }
311
312 @Override
313 public void onCommand(TCommandEvent command) {
314 if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) {
315 TuiReaderApplication.close(this);
316 } else {
317 // Handle our own event if needed here
318 super.onCommand(command);
319 }
320 }
321
322 @Override
323 public void onMenu(TMenuEvent menu) {
324 MetaData meta = getSelectedMeta();
325 if (meta != null) {
326 switch (menu.getId()) {
327 case TuiReaderApplication.MENU_OPEN:
328 readStory(meta);
329
330 return;
331 case TuiReaderApplication.MENU_EXPORT:
332
333 try {
334 // TODO: choose type, pg, error
335 OutputType outputType = OutputType.EPUB;
336 String path = fileOpenBox(".", Type.SAVE);
337 reader.getLibrary().export(meta.getLuid(), outputType,
338 path, null);
339 } catch (IOException e) {
340 // TODO
341 e.printStackTrace();
342 }
343
344 return;
345
346 case -1:
347 try {
348 reader.getLibrary().delete(meta.getLuid());
349 } catch (IOException e) {
350 // TODO
351 }
352
353 return;
354 }
355 }
356
357 super.onMenu(menu);
358 }
359}