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