TUI: code cleanup + show story info
[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.List;
6
7 import jexer.TAction;
8 import jexer.TFileOpenBox.Type;
9 import jexer.TList;
10 import jexer.TWindow;
11 import jexer.event.TMenuEvent;
12 import be.nikiroo.fanfix.Instance;
13 import be.nikiroo.fanfix.data.MetaData;
14 import be.nikiroo.fanfix.library.BasicLibrary;
15 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
16 import be.nikiroo.fanfix.reader.Reader;
17
18 /**
19 * The library window, that will list all the (filtered) stories available in
20 * this {@link BasicLibrary}.
21 *
22 * @author niki
23 */
24 class TuiReaderMainWindow extends TWindow {
25 private TList list;
26 private List<MetaData> listKeys;
27 private List<String> listItems;
28 private Reader reader;
29 private String source;
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) {
38 // Construct a demo window. X and Y don't matter because it will be
39 // centred on screen.
40 super(reader, "Library", 0, 0, 60, 18, CENTERED | RESIZABLE);
41
42 this.reader = reader;
43
44 maximize();
45
46 listKeys = new ArrayList<MetaData>();
47 listItems = new ArrayList<String>();
48 list = addList(listItems, 0, 0, getWidth(), getHeight(), new TAction() {
49 @Override
50 public void DO() {
51 MetaData meta = getSelectedMeta();
52 if (meta != null) {
53 readStory(meta);
54 }
55 }
56 });
57
58 // TODO: add the current "source/type" or filter
59 reader.setStatusBar(this, "Library");
60
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 }
85
86 @Override
87 public void onClose() {
88 setVisible(false);
89 super.onClose();
90 }
91
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
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);
118 }
119
120 setMetas(metas);
121 }
122
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 */
129 private void setMetas(List<MetaData> metas) {
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
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) {
152 try {
153 reader.setChapter(-1);
154 reader.setMeta(meta);
155 reader.read();
156 } catch (IOException e) {
157 Instance.getTraceHandler().error(e);
158 }
159 }
160
161 private String desc(MetaData meta) {
162 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
163 }
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 }