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