TUI: update (most menu functions working)
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderApplication.java
1 package be.nikiroo.fanfix.reader;
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.Instance.SyserrHandler;
18 import be.nikiroo.fanfix.Instance.TraceHandler;
19 import be.nikiroo.fanfix.data.MetaData;
20 import be.nikiroo.fanfix.data.Story;
21 import be.nikiroo.fanfix.library.BasicLibrary;
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_EXIT = 1029;
38
39 private Reader reader;
40 private TuiReaderMainWindow main;
41
42 // start reading if meta present
43 public TuiReaderApplication(Reader reader, BackendType backend)
44 throws Exception {
45 super(backend);
46 init(reader);
47
48 MetaData meta = getMeta();
49
50 main = new TuiReaderMainWindow(this);
51 main.setMeta(meta);
52 if (meta != null) {
53 read();
54 }
55 }
56
57 public TuiReaderApplication(Reader reader, String source,
58 TApplication.BackendType backend) throws Exception {
59 super(backend);
60
61 init(reader);
62
63 main = new TuiReaderMainWindow(this);
64 main.setSource(source);
65 }
66
67 @Override
68 public void read() throws IOException {
69 MetaData meta = getMeta();
70
71 if (meta == null) {
72 throw new IOException("No story to read");
73 }
74
75 // TODO: open in editor + external option
76 if (!meta.isImageDocument()) {
77 @SuppressWarnings("unused")
78 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
79 getChapter());
80 } else {
81 try {
82 BasicReader.openExternal(getLibrary(), meta.getLuid());
83 } catch (IOException e) {
84 messageBox("Error when trying to open the story",
85 e.getMessage(), TMessageBox.Type.OK);
86 }
87 }
88 }
89
90 @Override
91 public MetaData getMeta() {
92 return reader.getMeta();
93 }
94
95 @Override
96 public Story getStory(Progress pg) {
97 return reader.getStory(pg);
98 }
99
100 @Override
101 public BasicLibrary getLibrary() {
102 return reader.getLibrary();
103 }
104
105 @Override
106 public void setLibrary(BasicLibrary lib) {
107 reader.setLibrary(lib);
108 }
109
110 @Override
111 public void setMeta(MetaData meta) throws IOException {
112 reader.setMeta(meta);
113 }
114
115 @Override
116 public void setMeta(String luid) throws IOException {
117 reader.setMeta(luid);
118 }
119
120 @Override
121 public void setMeta(URL source, Progress pg) throws IOException {
122 reader.setMeta(source, pg);
123 }
124
125 @Override
126 public void browse(String source) {
127 reader.browse(source);
128 }
129
130 @Override
131 public int getChapter() {
132 return reader.getChapter();
133 }
134
135 @Override
136 public void setChapter(int chapter) {
137 reader.setChapter(chapter);
138 }
139
140 private void init(Reader reader) {
141 this.reader = reader;
142
143 // Do not allow traces/debug to pollute the screen:
144 Instance.setSyserrHandler(new SyserrHandler() {
145 @Override
146 public void notify(Exception e, boolean showDetails) {
147 // TODO
148 }
149 });
150
151 Instance.setTraceHandler(new TraceHandler() {
152 @Override
153 public void trace(String message) {
154 // TODO
155 }
156 });
157 //
158
159 // Add the menus TODO: i18n
160 TMenu fileMenu = addMenu("&File");
161 fileMenu.addItem(MENU_OPEN, "&Open");
162 fileMenu.addItem(MENU_EXPORT, "&Save as...");
163 // TODO: Move to...
164 fileMenu.addSeparator();
165 fileMenu.addItem(MENU_IMPORT_URL, "Import &URL...");
166 fileMenu.addItem(MENU_IMPORT_FILE, "Import &file...");
167 fileMenu.addSeparator();
168 fileMenu.addItem(MENU_EXIT, "E&xit");
169
170 TStatusBar statusBar = fileMenu.newStatusBar("File-management "
171 + "commands (Open, Save, Print, etc.)");
172 // TODO: doesn't actually work:
173 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
174
175 // TODO: Edit: re-download, delete
176
177 //
178
179 addWindowMenu();
180
181 getBackend().setTitle("Fanfix");
182 }
183
184 @Override
185 protected boolean onMenu(TMenuEvent menu) {
186 // TODO: i18n
187 switch (menu.getId()) {
188 case MENU_EXIT:
189 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
190 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
191 exit(false);
192 }
193
194 return true;
195 case MENU_IMPORT_URL:
196 String clipboard = "";
197 try {
198 clipboard = ("" + Toolkit.getDefaultToolkit()
199 .getSystemClipboard().getData(DataFlavor.stringFlavor))
200 .trim();
201 } catch (Exception e) {
202 // No data will be handled
203 }
204
205 if (clipboard == null || !clipboard.startsWith("http")) {
206 clipboard = "";
207 }
208
209 String url = inputBox("Import story", "URL to import", clipboard)
210 .getText();
211
212 if (!imprt(url)) {
213 // TODO: bad import
214 }
215
216 return true;
217 case MENU_IMPORT_FILE:
218 try {
219 String filename = fileOpenBox(".");
220 if (!imprt(filename)) {
221 // TODO: bad import
222 }
223 } catch (IOException e) {
224 // TODO: bad file
225 e.printStackTrace();
226 }
227
228 return true;
229 }
230
231 return super.onMenu(menu);
232 }
233
234 private boolean imprt(String url) {
235 try {
236 reader.getLibrary().imprt(BasicReader.getUrl(url), null);
237 main.refreshStories();
238 return true;
239 } catch (IOException e) {
240 return false;
241 }
242 }
243 }