1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.awt
.Toolkit
;
4 import java
.awt
.datatransfer
.DataFlavor
;
5 import java
.io
.IOException
;
7 import java
.net
.UnknownHostException
;
9 import jexer
.TApplication
;
10 import jexer
.TCommand
;
11 import jexer
.TKeypress
;
12 import jexer
.TMessageBox
;
13 import jexer
.TStatusBar
;
16 import jexer
.event
.TMenuEvent
;
17 import jexer
.menu
.TMenu
;
18 import be
.nikiroo
.fanfix
.Instance
;
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
.fanfix
.reader
.BasicReader
;
23 import be
.nikiroo
.fanfix
.reader
.Reader
;
24 import be
.nikiroo
.utils
.Progress
;
27 * Manages the TUI general mode and links and manages the {@link TWindow}s.
29 * It will also enclose a {@link Reader} and simply handle the reading part
30 * differently (it will create the required sub-windows and display them).
34 class TuiReaderApplication
extends TApplication
implements Reader
{
35 public static final int MENU_OPEN
= 1025;
36 public static final int MENU_IMPORT_URL
= 1026;
37 public static final int MENU_IMPORT_FILE
= 1027;
38 public static final int MENU_EXPORT
= 1028;
39 public static final int MENU_LIBRARY
= 1029;
40 public static final int MENU_EXIT
= 1030;
42 public static final TCommand CMD_EXIT
= new TCommand(MENU_EXIT
) {
45 private Reader reader
;
46 private TuiReaderMainWindow main
;
48 private MetaData meta
;
49 private String source
;
50 private boolean useMeta
;
52 // start reading if meta present
53 public TuiReaderApplication(Reader reader
, BackendType backend
)
58 showMain(getMeta(), null, true);
61 public TuiReaderApplication(Reader reader
, String source
,
62 TApplication
.BackendType backend
) throws Exception
{
66 showMain(null, source
, false);
70 public void read() throws IOException
{
71 MetaData meta
= getMeta();
74 throw new IOException("No story to read");
77 // TODO: open in editor + external option
78 if (!meta
.isImageDocument()) {
79 TWindow window
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
84 openExternal(getLibrary(), meta
.getLuid());
85 } catch (IOException e
) {
86 messageBox("Error when trying to open the story",
87 e
.getMessage(), TMessageBox
.Type
.OK
);
93 public MetaData
getMeta() {
94 return reader
.getMeta();
98 public Story
getStory(Progress pg
) {
99 return reader
.getStory(pg
);
103 public BasicLibrary
getLibrary() {
104 return reader
.getLibrary();
108 public void setLibrary(BasicLibrary lib
) {
109 reader
.setLibrary(lib
);
113 public void setMeta(MetaData meta
) throws IOException
{
114 reader
.setMeta(meta
);
118 public void setMeta(String luid
) throws IOException
{
119 reader
.setMeta(luid
);
123 public void setMeta(URL source
, Progress pg
) throws IOException
{
124 reader
.setMeta(source
, pg
);
128 public void browse(String source
) {
129 reader
.browse(source
);
133 public int getChapter() {
134 return reader
.getChapter();
138 public void setChapter(int chapter
) {
139 reader
.setChapter(chapter
);
143 * Set the default status bar when this window appear.
145 * Some shortcuts are always visible, and will be put here.
147 * Note that shortcuts placed this way on menu won't work unless the menu
148 * also implement them.
151 * the new window or menu on screen
153 * the description to show on the status ba
155 public TStatusBar
setStatusBar(TWindow window
, String description
) {
156 TStatusBar statusBar
= window
.newStatusBar(description
);
157 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
162 private void showMain(MetaData meta
, String source
, boolean useMeta
)
164 // TODO: thread-safety
166 this.source
= source
;
167 this.useMeta
= useMeta
;
169 if (main
!= null && main
.isVisible()) {
175 main
= new TuiReaderMainWindow(this);
182 main
.setSource(source
);
188 private void init(Reader reader
) {
189 this.reader
= reader
;
191 // TODO: traces/errors?
192 Instance
.setTraceHandler(null);
194 // Add the menus TODO: i18n
195 TMenu fileMenu
= addMenu("&File");
196 fileMenu
.addItem(MENU_OPEN
, "&Open");
197 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
199 fileMenu
.addSeparator();
200 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
201 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
202 fileMenu
.addSeparator();
203 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
204 fileMenu
.addSeparator();
205 fileMenu
.addItem(MENU_EXIT
, "E&xit");
207 setStatusBar(fileMenu
, "File-management "
208 + "commands (Open, Save, Print, etc.)");
210 // TODO: Edit: re-download, delete
216 getBackend().setTitle("Fanfix");
220 protected boolean onMenu(TMenuEvent menu
) {
222 switch (menu
.getId()) {
226 case MENU_IMPORT_URL
:
227 String clipboard
= "";
229 clipboard
= ("" + Toolkit
.getDefaultToolkit()
230 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
232 } catch (Exception e
) {
233 // No data will be handled
236 if (clipboard
== null || !clipboard
.startsWith("http")) {
240 String url
= inputBox("Import story", "URL to import", clipboard
)
245 // TODO: i18n + error
246 error("URK not supported: " + url
, "Import error");
248 } catch (IOException e
) {
249 // TODO: i18n + error
250 error("Fail to import URL: " + url
, "Import error", e
);
254 case MENU_IMPORT_FILE
:
255 String filename
= null;
257 filename
= fileOpenBox(".");
258 if (!imprt(filename
)) {
259 // TODO: i18n + error
260 error("File not supported: " + filename
, "Import error");
262 } catch (IOException e
) {
263 // TODO: i18n + error
264 error("Fail to import file"
265 + (filename
== null ?
"" : ": " + filename
),
271 showMain(meta
, source
, useMeta
);
272 } catch (IOException e
) {
274 error("Cannot show the library", "ERROR", e
);
280 return super.onMenu(menu
);
284 * Import the given url.
286 * Can fail if the host is not supported.
290 * @return TRUE in case of success, FALSE if the host is not supported
292 * @throws IOException
293 * in case of I/O error
295 private boolean imprt(String url
) throws IOException
{
297 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
298 main
.refreshStories();
300 } catch (UnknownHostException e
) {
306 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
307 reader
.openExternal(lib
, luid
);
311 * Display an error message and log it.
316 * the title of the error message
318 private void error(String message
, String title
) {
319 error(message
, title
, null);
323 * Display an error message and log it, including the linked
329 * the title of the error message
331 * the exception to log if any (can be NULL)
333 private void error(String message
, String title
, Exception e
) {
334 Instance
.getTraceHandler().error(title
+ ": " + message
);
336 Instance
.getTraceHandler().error(e
);
340 messageBox(title
, message
//
341 + "\n" + e
.getMessage());
343 messageBox(title
, message
);
348 * Ask the user and, if confirmed, close the {@link TApplication} this
349 * {@link TWidget} is running on.
351 * This should result in the program terminating.
354 * the {@link TWidget}
356 static public void close(TWidget widget
) {
357 close(widget
.getApplication());
361 * Ask the user and, if confirmed, close the {@link TApplication}.
363 * This should result in the program terminating.
366 * the {@link TApplication}
368 static void close(TApplication app
) {
370 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
371 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {