Commit | Line | Data |
---|---|---|
16a81ef7 | 1 | package be.nikiroo.fanfix.reader.tui; |
c1873e56 | 2 | |
bc2ea776 | 3 | import java.io.IOException; |
c1873e56 | 4 | import java.util.ArrayList; |
e9d43c7c | 5 | import java.util.Arrays; |
c1873e56 NR |
6 | import java.util.List; |
7 | ||
5dd985cf | 8 | import jexer.TAction; |
e2d017a3 | 9 | import jexer.TFileOpenBox.Type; |
5dd985cf | 10 | import jexer.TList; |
5dd985cf | 11 | import jexer.TWindow; |
cf9c5ed1 | 12 | import jexer.event.TCommandEvent; |
e2d017a3 | 13 | import jexer.event.TMenuEvent; |
bc2ea776 | 14 | import be.nikiroo.fanfix.Instance; |
c1873e56 | 15 | import be.nikiroo.fanfix.data.MetaData; |
bc2ea776 | 16 | import be.nikiroo.fanfix.library.BasicLibrary; |
e2d017a3 | 17 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
16a81ef7 | 18 | import be.nikiroo.fanfix.reader.Reader; |
c1873e56 | 19 | |
6322ab64 NR |
20 | /** |
21 | * The library window, that will list all the (filtered) stories available in | |
bc2ea776 | 22 | * this {@link BasicLibrary}. |
6322ab64 NR |
23 | * |
24 | * @author niki | |
25 | */ | |
5dd985cf | 26 | class TuiReaderMainWindow extends TWindow { |
c1873e56 NR |
27 | private TList list; |
28 | private List<MetaData> listKeys; | |
29 | private List<String> listItems; | |
bc2ea776 | 30 | private Reader reader; |
e2d017a3 | 31 | private String source; |
6322ab64 NR |
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) { | |
c1873e56 | 40 | // Construct a demo window. X and Y don't matter because it will be |
bc2ea776 | 41 | // centred on screen. |
4f66bfa8 | 42 | super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE); |
c1873e56 NR |
43 | |
44 | this.reader = reader; | |
45 | ||
46 | maximize(); | |
47 | ||
48 | listKeys = new ArrayList<MetaData>(); | |
49 | listItems = new ArrayList<String>(); | |
e9d43c7c NR |
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() { | |
c1873e56 NR |
66 | @Override |
67 | public void DO() { | |
e2d017a3 NR |
68 | MetaData meta = getSelectedMeta(); |
69 | if (meta != null) { | |
70 | readStory(meta); | |
c1873e56 NR |
71 | } |
72 | } | |
73 | }); | |
5dd985cf | 74 | |
6322ab64 | 75 | // TODO: add the current "source/type" or filter |
5b00c122 | 76 | reader.setStatusBar(this, "Library"); |
c1873e56 | 77 | |
bc2ea776 NR |
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 | } | |
c1873e56 | 102 | |
4f66bfa8 NR |
103 | @Override |
104 | public void onClose() { | |
105 | setVisible(false); | |
106 | super.onClose(); | |
107 | } | |
108 | ||
e2d017a3 NR |
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 | ||
bc2ea776 NR |
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); | |
c1873e56 | 135 | } |
bc2ea776 NR |
136 | |
137 | setMetas(metas); | |
c1873e56 NR |
138 | } |
139 | ||
6322ab64 NR |
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 | */ | |
e2d017a3 | 146 | private void setMetas(List<MetaData> metas) { |
6322ab64 NR |
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 | ||
e2d017a3 NR |
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) { | |
bc2ea776 NR |
169 | try { |
170 | reader.setChapter(-1); | |
171 | reader.setMeta(meta); | |
172 | reader.read(); | |
173 | } catch (IOException e) { | |
62c63b07 | 174 | Instance.getTraceHandler().error(e); |
bc2ea776 | 175 | } |
c1873e56 NR |
176 | } |
177 | ||
178 | private String desc(MetaData meta) { | |
179 | return String.format("%5s: %s", meta.getLuid(), meta.getTitle()); | |
180 | } | |
e2d017a3 | 181 | |
cf9c5ed1 NR |
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 | ||
e2d017a3 NR |
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 | } |