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