wip
[fanfix.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderMainWindow.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.tui;
c1873e56 2
bc2ea776 3import java.io.IOException;
c1873e56 4import java.util.ArrayList;
e9d43c7c 5import java.util.Arrays;
c1873e56
NR
6import java.util.List;
7
5dd985cf 8import jexer.TAction;
bce88a00
NR
9import jexer.TCommand;
10import jexer.TField;
e2d017a3 11import jexer.TFileOpenBox.Type;
bce88a00 12import jexer.TKeypress;
5dd985cf 13import jexer.TList;
bce88a00 14import jexer.TStatusBar;
5dd985cf 15import jexer.TWindow;
cf9c5ed1 16import jexer.event.TCommandEvent;
bce88a00 17import jexer.event.TKeypressEvent;
e2d017a3 18import jexer.event.TMenuEvent;
bc2ea776 19import be.nikiroo.fanfix.Instance;
c1873e56 20import be.nikiroo.fanfix.data.MetaData;
bc2ea776 21import be.nikiroo.fanfix.library.BasicLibrary;
e2d017a3 22import be.nikiroo.fanfix.output.BasicOutput.OutputType;
16a81ef7 23import be.nikiroo.fanfix.reader.Reader;
c1873e56 24
6322ab64
NR
25/**
26 * The library window, that will list all the (filtered) stories available in
bc2ea776 27 * this {@link BasicLibrary}.
6322ab64
NR
28 *
29 * @author niki
30 */
5dd985cf 31class TuiReaderMainWindow extends TWindow {
bce88a00
NR
32 public static final int MENU_SEARCH = 1100;
33 public static final TCommand CMD_SEARCH = new TCommand(MENU_SEARCH) {
34 };
35
c1873e56
NR
36 private TList list;
37 private List<MetaData> listKeys;
38 private List<String> listItems;
bc2ea776 39 private Reader reader;
e2d017a3 40 private String source;
bce88a00 41 private String filter = "";
6322ab64
NR
42
43 /**
44 * Create a new {@link TuiReaderMainWindow} without any stories in the list.
45 *
46 * @param reader
47 * the reader and main application
48 */
49 public TuiReaderMainWindow(TuiReaderApplication reader) {
c1873e56 50 // Construct a demo window. X and Y don't matter because it will be
bc2ea776 51 // centred on screen.
4f66bfa8 52 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE);
c1873e56
NR
53
54 this.reader = reader;
55
c1873e56
NR
56 listKeys = new ArrayList<MetaData>();
57 listItems = new ArrayList<String>();
e9d43c7c
NR
58
59 // TODO size + onResize
60
11758a0f 61 addLabel("Search: ", 5, 3);
bce88a00
NR
62 @SuppressWarnings("unused")
63 TField field = new TField(this, 15, 3, 5, true) {
64 @Override
65 public void onKeypress(TKeypressEvent keypress) {
66 super.onKeypress(keypress);
67 TKeypress key = keypress.getKey();
68 if (key.isFnKey() && key.getKeyCode() == TKeypress.ENTER) {
69 TuiReaderMainWindow.this.filter = getText();
70 TuiReaderMainWindow.this.refreshStories();
71 }
72 }
73 };
74
e9d43c7c
NR
75 addLabel("Sort by: ", 5, 1);
76 // -1 = no default index (0 means first,...) 1=height when visible, null
77 // = action
bce88a00
NR
78 List<String> data = Arrays.asList("(show all)", "Source", "Name",
79 "Author");
11758a0f
N
80 // must be last so to be able to draw over the rest
81 // TODO: make it so we cannot add manual entries
bce88a00
NR
82 // TODO: how to select the item via keyboard? why double-click via
83 // mouse?
84 addComboBox(15, 1, 12, data, 0,
85 Math.min(data.size() + 1, getHeight() - 1 - 1), null);
e9d43c7c 86
11758a0f 87 list = addList(listItems, 0, 7, getWidth(), getHeight(), new TAction() {
c1873e56
NR
88 @Override
89 public void DO() {
e2d017a3
NR
90 MetaData meta = getSelectedMeta();
91 if (meta != null) {
92 readStory(meta);
c1873e56
NR
93 }
94 }
95 });
5dd985cf 96
bce88a00
NR
97 TStatusBar statusBar = reader.setStatusBar(this, "Library");
98 statusBar.addShortcutKeypress(TKeypress.kbCtrlF, CMD_SEARCH, "Search");
c1873e56 99
bc2ea776
NR
100 // TODO: remove when not used anymore
101
102 // addLabel("Label (1,1)", 1, 1);
103 // addButton("&Button (35,1)", 35, 1, new TAction() {
104 // public void DO() {
105 // }
106 // });
107 // addCheckbox(1, 2, "Checky (1,2)", false);
108 // addProgressBar(1, 3, 30, 42);
109 // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy");
110 // groupy.addRadioButton("Fanfan");
111 // groupy.addRadioButton("Tulipe");
112 // addField(1, 10, 20, false, "text not fixed.");
113 // addField(1, 11, 20, true, "text fixed.");
114 // addText("20x4 Text in (12,20)", 1, 12, 20, 4);
115 //
116 // TTreeView tree = addTreeView(30, 5, 20, 5);
117 // TTreeItem root = new TTreeItem(tree, "expended root", true);
118 // tree.setSelected(root); // needed to allow arrow navigation without
119 // // mouse-clicking before
120 //
121 // root.addChild("child");
122 // root.addChild("child 2").addChild("sub child");
123 }
c1873e56 124
4f66bfa8
NR
125 @Override
126 public void onClose() {
127 setVisible(false);
128 super.onClose();
129 }
130
e2d017a3 131 /**
bce88a00
NR
132 * Refresh the list of stories displayed in this library.
133 * <p>
134 * Will take the current settings into account (filter, source...).
e2d017a3 135 */
e2d017a3
NR
136 public void refreshStories() {
137 List<MetaData> metas = reader.getLibrary().getListBySource(source);
138 setMetas(metas);
139 }
140
bc2ea776 141 /**
bce88a00 142 * Change the source filter and display all stories matching this source.
bc2ea776 143 *
bce88a00
NR
144 * @param source
145 * the new source or NULL for all sources
bc2ea776 146 */
bce88a00
NR
147 public void setSource(String source) {
148 this.source = source;
149 refreshStories();
c1873e56
NR
150 }
151
6322ab64
NR
152 /**
153 * Update the list of stories displayed in this {@link TWindow}.
bce88a00
NR
154 * <p>
155 * If a filter is set, only the stories which pass the filter will be
156 * displayed.
6322ab64
NR
157 *
158 * @param metas
159 * the new list of stories to display
160 */
e2d017a3 161 private void setMetas(List<MetaData> metas) {
6322ab64
NR
162 listKeys.clear();
163 listItems.clear();
164
165 if (metas != null) {
166 for (MetaData meta : metas) {
bce88a00
NR
167 String desc = desc(meta);
168 if (filter.isEmpty()
169 || desc.toLowerCase().contains(filter.toLowerCase())) {
170 listKeys.add(meta);
171 listItems.add(desc);
172 }
6322ab64
NR
173 }
174 }
175
176 list.setList(listItems);
177 }
178
e2d017a3
NR
179 public MetaData getSelectedMeta() {
180 if (list.getSelectedIndex() >= 0) {
181 return listKeys.get(list.getSelectedIndex());
182 }
183
184 return null;
185 }
186
187 public void readStory(MetaData meta) {
bc2ea776
NR
188 try {
189 reader.setChapter(-1);
190 reader.setMeta(meta);
191 reader.read();
192 } catch (IOException e) {
62c63b07 193 Instance.getTraceHandler().error(e);
bc2ea776 194 }
c1873e56
NR
195 }
196
197 private String desc(MetaData meta) {
198 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
199 }
e2d017a3 200
cf9c5ed1
NR
201 @Override
202 public void onCommand(TCommandEvent command) {
203 if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) {
204 TuiReaderApplication.close(this);
205 } else {
206 // Handle our own event if needed here
207 super.onCommand(command);
208 }
209 }
210
e2d017a3
NR
211 @Override
212 public void onMenu(TMenuEvent menu) {
213 MetaData meta = getSelectedMeta();
214 if (meta != null) {
215 switch (menu.getId()) {
216 case TuiReaderApplication.MENU_OPEN:
217 readStory(meta);
218
219 return;
220 case TuiReaderApplication.MENU_EXPORT:
221
222 try {
223 // TODO: choose type, pg, error
224 OutputType outputType = OutputType.EPUB;
225 String path = fileOpenBox(".", Type.SAVE);
226 reader.getLibrary().export(meta.getLuid(), outputType,
227 path, null);
228 } catch (IOException e) {
229 // TODO
230 e.printStackTrace();
231 }
232
233 return;
bce88a00 234
e2d017a3
NR
235 case -1:
236 try {
237 reader.getLibrary().delete(meta.getLuid());
238 } catch (IOException e) {
239 // TODO
240 }
241
242 return;
243 }
244 }
245
246 super.onMenu(menu);
247 }
248}