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