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