TUI: code cleanup + show story info
[fanfix.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderApplication.java
1 package be.nikiroo.fanfix.reader.tui;
2
3 import java.awt.Toolkit;
4 import java.awt.datatransfer.DataFlavor;
5 import java.io.IOException;
6 import java.net.URL;
7
8 import jexer.TApplication;
9 import jexer.TCommand;
10 import jexer.TKeypress;
11 import jexer.TMessageBox;
12 import jexer.TStatusBar;
13 import jexer.TWindow;
14 import jexer.event.TMenuEvent;
15 import jexer.menu.TMenu;
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.fanfix.library.BasicLibrary;
20 import be.nikiroo.fanfix.reader.BasicReader;
21 import be.nikiroo.fanfix.reader.Reader;
22 import be.nikiroo.utils.Progress;
23
24 /**
25 * Manages the TUI general mode and links and manages the {@link TWindow}s.
26 * <p>
27 * It will also enclose a {@link Reader} and simply handle the reading part
28 * differently (it will create the required sub-windows and display them).
29 *
30 * @author niki
31 */
32 class TuiReaderApplication extends TApplication implements Reader {
33 public static final int MENU_OPEN = 1025;
34 public static final int MENU_IMPORT_URL = 1026;
35 public static final int MENU_IMPORT_FILE = 1027;
36 public static final int MENU_EXPORT = 1028;
37 public static final int MENU_LIBRARY = 1029;
38 public static final int MENU_EXIT = 1030;
39
40 private Reader reader;
41 private TuiReaderMainWindow main;
42
43 private MetaData meta;
44 private String source;
45 private boolean useMeta;
46
47 // start reading if meta present
48 public TuiReaderApplication(Reader reader, BackendType backend)
49 throws Exception {
50 super(backend);
51 init(reader);
52
53 showMain(getMeta(), null, true);
54 }
55
56 public TuiReaderApplication(Reader reader, String source,
57 TApplication.BackendType backend) throws Exception {
58 super(backend);
59 init(reader);
60
61 showMain(null, source, false);
62 }
63
64 @Override
65 public void read() throws IOException {
66 MetaData meta = getMeta();
67
68 if (meta == null) {
69 throw new IOException("No story to read");
70 }
71
72 // TODO: open in editor + external option
73 if (!meta.isImageDocument()) {
74 @SuppressWarnings("unused")
75 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
76 getChapter());
77 } else {
78 try {
79 openExternal(getLibrary(), meta.getLuid());
80 } catch (IOException e) {
81 messageBox("Error when trying to open the story",
82 e.getMessage(), TMessageBox.Type.OK);
83 }
84 }
85 }
86
87 @Override
88 public MetaData getMeta() {
89 return reader.getMeta();
90 }
91
92 @Override
93 public Story getStory(Progress pg) {
94 return reader.getStory(pg);
95 }
96
97 @Override
98 public BasicLibrary getLibrary() {
99 return reader.getLibrary();
100 }
101
102 @Override
103 public void setLibrary(BasicLibrary lib) {
104 reader.setLibrary(lib);
105 }
106
107 @Override
108 public void setMeta(MetaData meta) throws IOException {
109 reader.setMeta(meta);
110 }
111
112 @Override
113 public void setMeta(String luid) throws IOException {
114 reader.setMeta(luid);
115 }
116
117 @Override
118 public void setMeta(URL source, Progress pg) throws IOException {
119 reader.setMeta(source, pg);
120 }
121
122 @Override
123 public void browse(String source) {
124 reader.browse(source);
125 }
126
127 @Override
128 public int getChapter() {
129 return reader.getChapter();
130 }
131
132 @Override
133 public void setChapter(int chapter) {
134 reader.setChapter(chapter);
135 }
136
137 /**
138 * Set the default status bar when this window appear.
139 * <p>
140 * Some shortcuts are always visible, and will be put here.
141 * <p>
142 * Note that shortcuts placed this way on menu won't work unless the menu
143 * also implement them.
144 *
145 * @param window
146 * the new window or menu on screen
147 * @param description
148 * the description to show on the status ba
149 */
150 public TStatusBar setStatusBar(TWindow window, String description) {
151 TStatusBar statusBar = window.newStatusBar(description);
152 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
153 return statusBar;
154
155 }
156
157 private void showMain(MetaData meta, String source, boolean useMeta)
158 throws IOException {
159 // TODO: thread-safety
160 this.meta = meta;
161 this.source = source;
162 this.useMeta = useMeta;
163
164 if (main != null && main.isVisible()) {
165 main.activate();
166 } else {
167 if (main != null) {
168 main.close();
169 }
170 main = new TuiReaderMainWindow(this);
171 if (useMeta) {
172 main.setMeta(meta);
173 if (meta != null) {
174 read();
175 }
176 } else {
177 main.setSource(source);
178 }
179 }
180 }
181
182 private void init(Reader reader) {
183 this.reader = reader;
184
185 // TODO: traces/errors?
186 Instance.setTraceHandler(null);
187
188 // Add the menus TODO: i18n
189 TMenu fileMenu = addMenu("&File");
190 fileMenu.addItem(MENU_OPEN, "&Open");
191 fileMenu.addItem(MENU_EXPORT, "&Save as...");
192 // TODO: Move to...
193 fileMenu.addSeparator();
194 fileMenu.addItem(MENU_IMPORT_URL, "Import &URL...");
195 fileMenu.addItem(MENU_IMPORT_FILE, "Import &file...");
196 fileMenu.addSeparator();
197 fileMenu.addItem(MENU_LIBRARY, "Lib&rary");
198 fileMenu.addSeparator();
199 fileMenu.addItem(MENU_EXIT, "E&xit");
200
201 setStatusBar(fileMenu, "File-management "
202 + "commands (Open, Save, Print, etc.)");
203
204 // TODO: Edit: re-download, delete
205
206 //
207
208 addWindowMenu();
209
210 getBackend().setTitle("Fanfix");
211 }
212
213 @Override
214 protected boolean onMenu(TMenuEvent menu) {
215 // TODO: i18n
216 switch (menu.getId()) {
217 case MENU_EXIT:
218 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
219 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
220 exit(); // TODO: exit(false) for "no confirm box"
221 }
222
223 return true;
224 case MENU_IMPORT_URL:
225 String clipboard = "";
226 try {
227 clipboard = ("" + Toolkit.getDefaultToolkit()
228 .getSystemClipboard().getData(DataFlavor.stringFlavor))
229 .trim();
230 } catch (Exception e) {
231 // No data will be handled
232 }
233
234 if (clipboard == null || !clipboard.startsWith("http")) {
235 clipboard = "";
236 }
237
238 String url = inputBox("Import story", "URL to import", clipboard)
239 .getText();
240
241 if (!imprt(url)) {
242 // TODO: bad import
243 }
244
245 return true;
246 case MENU_IMPORT_FILE:
247 try {
248 String filename = fileOpenBox(".");
249 if (!imprt(filename)) {
250 // TODO: bad import
251 }
252 } catch (IOException e) {
253 // TODO: bad file
254 e.printStackTrace();
255 }
256
257 return true;
258 case MENU_LIBRARY:
259 try {
260 showMain(meta, source, useMeta);
261 } catch (IOException e) {
262 e.printStackTrace();
263 }
264
265 return true;
266 }
267
268 return super.onMenu(menu);
269 }
270
271 private boolean imprt(String url) {
272 try {
273 reader.getLibrary().imprt(BasicReader.getUrl(url), null);
274 main.refreshStories();
275 return true;
276 } catch (IOException e) {
277 return false;
278 }
279 }
280
281 @Override
282 public void openExternal(BasicLibrary lib, String luid) throws IOException {
283 reader.openExternal(lib, luid);
284 }
285 }