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 | ||
c1873e56 NR |
46 | listKeys = new ArrayList<MetaData>(); |
47 | listItems = new ArrayList<String>(); | |
e9d43c7c NR |
48 | |
49 | // TODO size + onResize | |
50 | ||
11758a0f N |
51 | |
52 | ||
53 | addLabel("Search: ", 5, 3); | |
54 | addField(15, 3, 5, true); | |
55 | ||
e9d43c7c NR |
56 | addLabel("Sort by: ", 5, 1); |
57 | // -1 = no default index (0 means first,...) 1=height when visible, null | |
58 | // = action | |
11758a0f N |
59 | List<String> data = Arrays.asList("(show all)", "Source", "Name", "Author"); |
60 | // must be last so to be able to draw over the rest | |
61 | // TODO: make it so we cannot add manual entries | |
62 | // TODO: how to select the item via keyboard? why double-click via mouse? | |
e9d43c7c | 63 | addComboBox(15, 1, 12, |
11758a0f | 64 | data, 0, Math.min(data.size()+1,getHeight()-1-1), |
e9d43c7c NR |
65 | null); |
66 | ||
11758a0f | 67 | list = addList(listItems, 0, 7, getWidth(), getHeight(), new TAction() { |
c1873e56 NR |
68 | @Override |
69 | public void DO() { | |
e2d017a3 NR |
70 | MetaData meta = getSelectedMeta(); |
71 | if (meta != null) { | |
72 | readStory(meta); | |
c1873e56 NR |
73 | } |
74 | } | |
75 | }); | |
5dd985cf | 76 | |
6322ab64 | 77 | // TODO: add the current "source/type" or filter |
5b00c122 | 78 | reader.setStatusBar(this, "Library"); |
c1873e56 | 79 | |
bc2ea776 NR |
80 | // TODO: remove when not used anymore |
81 | ||
82 | // addLabel("Label (1,1)", 1, 1); | |
83 | // addButton("&Button (35,1)", 35, 1, new TAction() { | |
84 | // public void DO() { | |
85 | // } | |
86 | // }); | |
87 | // addCheckbox(1, 2, "Checky (1,2)", false); | |
88 | // addProgressBar(1, 3, 30, 42); | |
89 | // TRadioGroup groupy = addRadioGroup(1, 4, "Radio groupy"); | |
90 | // groupy.addRadioButton("Fanfan"); | |
91 | // groupy.addRadioButton("Tulipe"); | |
92 | // addField(1, 10, 20, false, "text not fixed."); | |
93 | // addField(1, 11, 20, true, "text fixed."); | |
94 | // addText("20x4 Text in (12,20)", 1, 12, 20, 4); | |
95 | // | |
96 | // TTreeView tree = addTreeView(30, 5, 20, 5); | |
97 | // TTreeItem root = new TTreeItem(tree, "expended root", true); | |
98 | // tree.setSelected(root); // needed to allow arrow navigation without | |
99 | // // mouse-clicking before | |
100 | // | |
101 | // root.addChild("child"); | |
102 | // root.addChild("child 2").addChild("sub child"); | |
103 | } | |
c1873e56 | 104 | |
4f66bfa8 NR |
105 | @Override |
106 | public void onClose() { | |
107 | setVisible(false); | |
108 | super.onClose(); | |
109 | } | |
110 | ||
e2d017a3 NR |
111 | /** |
112 | * Change the source filter and display all stories matching this source. | |
113 | * | |
114 | * @param source | |
115 | * the new source or NULL for all sources | |
116 | */ | |
117 | public void setSource(String source) { | |
118 | this.source = source; | |
119 | refreshStories(); | |
120 | } | |
121 | ||
122 | public void refreshStories() { | |
123 | List<MetaData> metas = reader.getLibrary().getListBySource(source); | |
124 | setMetas(metas); | |
125 | } | |
126 | ||
bc2ea776 NR |
127 | /** |
128 | * Update the list of stories displayed in this {@link TWindow}. | |
129 | * | |
130 | * @param meta | |
131 | * the new (unique) story to display | |
132 | */ | |
133 | public void setMeta(MetaData meta) { | |
134 | List<MetaData> metas = new ArrayList<MetaData>(); | |
135 | if (meta != null) { | |
136 | metas.add(meta); | |
c1873e56 | 137 | } |
bc2ea776 NR |
138 | |
139 | setMetas(metas); | |
c1873e56 NR |
140 | } |
141 | ||
6322ab64 NR |
142 | /** |
143 | * Update the list of stories displayed in this {@link TWindow}. | |
144 | * | |
145 | * @param metas | |
146 | * the new list of stories to display | |
147 | */ | |
e2d017a3 | 148 | private void setMetas(List<MetaData> metas) { |
6322ab64 NR |
149 | listKeys.clear(); |
150 | listItems.clear(); | |
151 | ||
152 | if (metas != null) { | |
153 | for (MetaData meta : metas) { | |
154 | listKeys.add(meta); | |
155 | listItems.add(desc(meta)); | |
156 | } | |
157 | } | |
158 | ||
159 | list.setList(listItems); | |
160 | } | |
161 | ||
e2d017a3 NR |
162 | public MetaData getSelectedMeta() { |
163 | if (list.getSelectedIndex() >= 0) { | |
164 | return listKeys.get(list.getSelectedIndex()); | |
165 | } | |
166 | ||
167 | return null; | |
168 | } | |
169 | ||
170 | public void readStory(MetaData meta) { | |
bc2ea776 NR |
171 | try { |
172 | reader.setChapter(-1); | |
173 | reader.setMeta(meta); | |
174 | reader.read(); | |
175 | } catch (IOException e) { | |
62c63b07 | 176 | Instance.getTraceHandler().error(e); |
bc2ea776 | 177 | } |
c1873e56 NR |
178 | } |
179 | ||
180 | private String desc(MetaData meta) { | |
181 | return String.format("%5s: %s", meta.getLuid(), meta.getTitle()); | |
182 | } | |
e2d017a3 | 183 | |
cf9c5ed1 NR |
184 | @Override |
185 | public void onCommand(TCommandEvent command) { | |
186 | if (command.getCmd().equals(TuiReaderApplication.CMD_EXIT)) { | |
187 | TuiReaderApplication.close(this); | |
188 | } else { | |
189 | // Handle our own event if needed here | |
190 | super.onCommand(command); | |
191 | } | |
192 | } | |
193 | ||
e2d017a3 NR |
194 | @Override |
195 | public void onMenu(TMenuEvent menu) { | |
196 | MetaData meta = getSelectedMeta(); | |
197 | if (meta != null) { | |
198 | switch (menu.getId()) { | |
199 | case TuiReaderApplication.MENU_OPEN: | |
200 | readStory(meta); | |
201 | ||
202 | return; | |
203 | case TuiReaderApplication.MENU_EXPORT: | |
204 | ||
205 | try { | |
206 | // TODO: choose type, pg, error | |
207 | OutputType outputType = OutputType.EPUB; | |
208 | String path = fileOpenBox(".", Type.SAVE); | |
209 | reader.getLibrary().export(meta.getLuid(), outputType, | |
210 | path, null); | |
211 | } catch (IOException e) { | |
212 | // TODO | |
213 | e.printStackTrace(); | |
214 | } | |
215 | ||
216 | return; | |
217 | case -1: | |
218 | try { | |
219 | reader.getLibrary().delete(meta.getLuid()); | |
220 | } catch (IOException e) { | |
221 | // TODO | |
222 | } | |
223 | ||
224 | return; | |
225 | } | |
226 | } | |
227 | ||
228 | super.onMenu(menu); | |
229 | } | |
230 | } |