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