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