1fa6d8b6e19a42b0752b8afe05c4626c89507fba
[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.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 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 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 @Override
90 public void onClose() {
91 setVisible(false);
92 super.onClose();
93 }
94
95 /**
96 * Change the source filter and display all stories matching this source.
97 *
98 * @param source
99 * the new source or NULL for all sources
100 */
101 public void setSource(String source) {
102 this.source = source;
103 refreshStories();
104 }
105
106 public void refreshStories() {
107 List<MetaData> metas = reader.getLibrary().getListBySource(source);
108 setMetas(metas);
109 }
110
111 /**
112 * Update the list of stories displayed in this {@link TWindow}.
113 *
114 * @param meta
115 * the new (unique) story to display
116 */
117 public void setMeta(MetaData meta) {
118 List<MetaData> metas = new ArrayList<MetaData>();
119 if (meta != null) {
120 metas.add(meta);
121 }
122
123 setMetas(metas);
124 }
125
126 /**
127 * Update the list of stories displayed in this {@link TWindow}.
128 *
129 * @param metas
130 * the new list of stories to display
131 */
132 private void setMetas(List<MetaData> metas) {
133 listKeys.clear();
134 listItems.clear();
135
136 if (metas != null) {
137 for (MetaData meta : metas) {
138 listKeys.add(meta);
139 listItems.add(desc(meta));
140 }
141 }
142
143 list.setList(listItems);
144 }
145
146 public MetaData getSelectedMeta() {
147 if (list.getSelectedIndex() >= 0) {
148 return listKeys.get(list.getSelectedIndex());
149 }
150
151 return null;
152 }
153
154 public void readStory(MetaData meta) {
155 try {
156 reader.setChapter(-1);
157 reader.setMeta(meta);
158 reader.read();
159 } catch (IOException e) {
160 Instance.getTraceHandler().error(e);
161 }
162 }
163
164 private String desc(MetaData meta) {
165 return String.format("%5s: %s", meta.getLuid(), meta.getTitle());
166 }
167
168 @Override
169 public void onMenu(TMenuEvent menu) {
170 MetaData meta = getSelectedMeta();
171 if (meta != null) {
172 switch (menu.getId()) {
173 case TuiReaderApplication.MENU_OPEN:
174 readStory(meta);
175
176 return;
177 case TuiReaderApplication.MENU_EXPORT:
178
179 try {
180 // TODO: choose type, pg, error
181 OutputType outputType = OutputType.EPUB;
182 String path = fileOpenBox(".", Type.SAVE);
183 reader.getLibrary().export(meta.getLuid(), outputType,
184 path, null);
185 } catch (IOException e) {
186 // TODO
187 e.printStackTrace();
188 }
189
190 return;
191 case -1:
192 try {
193 reader.getLibrary().delete(meta.getLuid());
194 } catch (IOException e) {
195 // TODO
196 }
197
198 return;
199 }
200 }
201
202 super.onMenu(menu);
203 }
204 }