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