wip
[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.Arrays;
6 import java.util.List;
7
8 import jexer.TAction;
9 import jexer.TFileOpenBox.Type;
10 import jexer.TList;
11 import jexer.TWindow;
12 import jexer.event.TCommandEvent;
13 import jexer.event.TMenuEvent;
14 import be.nikiroo.fanfix.Instance;
15 import be.nikiroo.fanfix.data.MetaData;
16 import be.nikiroo.fanfix.library.BasicLibrary;
17 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
18 import be.nikiroo.fanfix.reader.Reader;
19
20 /**
21 * The library window, that will list all the (filtered) stories available in
22 * this {@link BasicLibrary}.
23 *
24 * @author niki
25 */
26 class TuiReaderMainWindow extends TWindow {
27 private TList list;
28 private List<MetaData> listKeys;
29 private List<String> listItems;
30 private Reader reader;
31 private String source;
32
33 /**
34 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
35 *
36 * @param reader
37 * the reader and main application
38 */
39 public TuiReaderMainWindow(TuiReaderApplication reader) {
40 // Construct a demo window. X and Y don't matter because it will be
41 // centred on screen.
42 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE);
43
44 this.reader = reader;
45
46 maximize();
47
48 listKeys = new ArrayList<MetaData>();
49 listItems = new ArrayList<String>();
50
51 // TODO size + onResize
52
53 addLabel("Sort by: ", 5, 1);
54 // -1 = no default index (0 means first,...) 1=height when visible, null
55 // = action
56 addComboBox(15, 1, 12,
57 Arrays.asList("(show all)", "Source", "Name", "Author"), 0, 1,
58 null);
59
60 addLabel("Search: ", 5, 3);
61 addEditor("...", 15, 3, 12, 1); // TODO: cannot see where to type, ^H
62 // not working, TAB insert a TAB (cannot
63 // switch to next)
64
65 list = addList(listItems, 0, 5, getWidth(), getHeight(), new TAction() {
66 @Override
67 public void DO() {
68 MetaData meta = getSelectedMeta();
69 if (meta != null) {
70 readStory(meta);
71 }
72 }
73 });
74
75 // TODO: add the current "source/type" or filter
76 reader.setStatusBar(this, "Library");
77
78 // TODO: remove when not used anymore
79
80 // addLabel("Label (1,1)", 1, 1);
81 // addButton("&Button (35,1)", 35, 1, new TAction() {
82 // public void DO() {
83 // }
84 // });
85 // addCheckbox(1, 2, "Checky (1,2)", false);
86 // addProgressBar(1, 3, 30, 42);
87 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
88 // groupy.addRadioButton("Fanfan");
89 // groupy.addRadioButton("Tulipe");
90 // addField(1, 10, 20, false, "text not fixed.");
91 // addField(1, 11, 20, true, "text fixed.");
92 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
93 //
94 // TTreeView tree = addTreeView(30, 5, 20, 5);
95 // TTreeItem root = new TTreeItem(tree, "expended root", true);
96 // tree.setSelected(root); // needed to allow arrow navigation without
97 // // mouse-clicking before
98 //
99 // root.addChild("child");
100 // root.addChild("child 2").addChild("sub child");
101 }
102
103 @Override
104 public void onClose() {
105 setVisible(false);
106 super.onClose();
107 }
108
109 /**
110 * Change the source filter and display all stories matching this source.
111 *
112 * @param source
113 * the new source or NULL for all sources
114 */
115 public void setSource(String source) {
116 this.source = source;
117 refreshStories();
118 }
119
120 public void refreshStories() {
121 List<MetaData> metas = reader.getLibrary().getListBySource(source);
122 setMetas(metas);
123 }
124
125 /**
126 * Update the list of stories displayed in this {@link TWindow}.
127 *
128 * @param meta
129 * the new (unique) story to display
130 */
131 public void setMeta(MetaData meta) {
132 List<MetaData> metas = new ArrayList<MetaData>();
133 if (meta != null) {
134 metas.add(meta);
135 }
136
137 setMetas(metas);
138 }
139
140 /**
141 * Update the list of stories displayed in this {@link TWindow}.
142 *
143 * @param metas
144 * the new list of stories to display
145 */
146 private void setMetas(List<MetaData> metas) {
147 listKeys.clear();
148 listItems.clear();
149
150 if (metas != null) {
151 for (MetaData meta : metas) {
152 listKeys.add(meta);
153 listItems.add(desc(meta));
154 }
155 }
156
157 list.setList(listItems);
158 }
159
160 public MetaData getSelectedMeta() {
161 if (list.getSelectedIndex() >= 0) {
162 return listKeys.get(list.getSelectedIndex());
163 }
164
165 return null;
166 }
167
168 public void readStory(MetaData meta) {
169 try {
170 reader.setChapter(-1);
171 reader.setMeta(meta);
172 reader.read();
173 } catch (IOException e) {
174 Instance.getTraceHandler().error(e);
175 }
176 }
177
178 private String desc(MetaData meta) {
179 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
180 }
181
182 @Override
183 public void onCommand(TCommandEvent command) {
184 if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) {
185 TuiReaderApplication.close(this);
186 } else {
187 // Handle our own event if needed here
188 super.onCommand(command);
189 }
190 }
191
192 @Override
193 public void onMenu(TMenuEvent menu) {
194 MetaData meta = getSelectedMeta();
195 if (meta != null) {
196 switch (menu.getId()) {
197 case TuiReaderApplication.MENU_OPEN:
198 readStory(meta);
199
200 return;
201 case TuiReaderApplication.MENU_EXPORT:
202
203 try {
204 // TODO: choose type, pg, error
205 OutputType outputType = OutputType.EPUB;
206 String path = fileOpenBox(".", Type.SAVE);
207 reader.getLibrary().export(meta.getLuid(), outputType,
208 path, null);
209 } catch (IOException e) {
210 // TODO
211 e.printStackTrace();
212 }
213
214 return;
215 case -1:
216 try {
217 reader.getLibrary().delete(meta.getLuid());
218 } catch (IOException e) {
219 // TODO
220 }
221
222 return;
223 }
224 }
225
226 super.onMenu(menu);
227 }
228 }