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
.TMessageBox
.Result
;
14 import jexer
.TMessageBox
.Type
;
15 import jexer
.TStatusBar
;
18 import jexer
.event
.TCommandEvent
;
19 import jexer
.event
.TMenuEvent
;
20 import jexer
.menu
.TMenu
;
21 import be
.nikiroo
.fanfix
.Instance
;
22 import be
.nikiroo
.fanfix
.data
.MetaData
;
23 import be
.nikiroo
.fanfix
.data
.Story
;
24 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
25 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
26 import be
.nikiroo
.fanfix
.reader
.Reader
;
27 import be
.nikiroo
.fanfix
.reader
.tui
.TuiReaderMainWindow
.Mode
;
28 import be
.nikiroo
.utils
.Progress
;
31 * Manages the TUI general mode and links and manages the {@link TWindow}s.
33 * It will also enclose a {@link Reader} and simply handle the reading part
34 * differently (it will create the required sub-windows and display them).
38 class TuiReaderApplication
extends TApplication
implements Reader
{
39 public static final int MENU_OPEN
= 1025;
40 public static final int MENU_IMPORT_URL
= 1026;
41 public static final int MENU_IMPORT_FILE
= 1027;
42 public static final int MENU_EXPORT
= 1028;
43 public static final int MENU_DELETE
= 1029;
44 public static final int MENU_LIBRARY
= 1030;
45 public static final int MENU_EXIT
= 1031;
47 public static final TCommand CMD_EXIT
= new TCommand(MENU_EXIT
) {
50 private Reader reader
;
51 private TuiReaderMainWindow main
;
53 // start reading if meta present
54 public TuiReaderApplication(Reader reader
, BackendType backend
)
59 if (getMeta() != null) {
64 public TuiReaderApplication(Reader reader
, String source
,
65 TApplication
.BackendType backend
) throws Exception
{
70 main
.setMode(Mode
.SOURCE
, source
);
74 public void read(boolean sync
) throws IOException
{
75 read(getStory(null), sync
);
79 public MetaData
getMeta() {
80 return reader
.getMeta();
84 public Story
getStory(Progress pg
) {
85 return reader
.getStory(pg
);
89 public BasicLibrary
getLibrary() {
90 return reader
.getLibrary();
94 public void setLibrary(BasicLibrary lib
) {
95 reader
.setLibrary(lib
);
99 public void setMeta(MetaData meta
) throws IOException
{
100 reader
.setMeta(meta
);
104 public void setMeta(String luid
) throws IOException
{
105 reader
.setMeta(luid
);
109 public void setMeta(URL source
, Progress pg
) throws IOException
{
110 reader
.setMeta(source
, pg
);
114 public void browse(String source
) {
115 reader
.browse(source
);
119 public int getChapter() {
120 return reader
.getChapter();
124 public void setChapter(int chapter
) {
125 reader
.setChapter(chapter
);
129 * Open the given {@link Story} for reading. This may or may not start an
130 * external program to read said {@link Story}.
133 * the {@link Story} to read
135 * execute the process synchronously (wait until it is terminated
138 * @throws IOException
139 * in case of I/O errors
141 public void read(Story story
, boolean sync
) throws IOException
{
143 throw new IOException("No story to read");
146 // TODO: open in editor + external option
147 if (!story
.getMeta().isImageDocument()) {
148 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
152 openExternal(getLibrary(), story
.getMeta().getLuid(), sync
);
153 } catch (IOException e
) {
154 messageBox("Error when trying to open the story",
155 e
.getMessage(), TMessageBox
.Type
.OK
);
161 * Set the default status bar when this window appear.
163 * Some shortcuts are always visible, and will be put here.
165 * Note that shortcuts placed this way on menu won't work unless the menu
166 * also implement them.
169 * the new window or menu on screen
171 * the description to show on the status ba
173 public TStatusBar
setStatusBar(TWindow window
, String description
) {
174 TStatusBar statusBar
= window
.newStatusBar(description
);
175 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
180 private void showMain() {
181 if (main
!= null && main
.isVisible()) {
187 main
= new TuiReaderMainWindow(this);
192 private void init(Reader reader
) {
193 this.reader
= reader
;
195 // TODO: traces/errors?
196 Instance
.setTraceHandler(null);
198 // Add the menus TODO: i18n
199 TMenu fileMenu
= addMenu("&File");
200 fileMenu
.addItem(MENU_OPEN
, "&Open...");
201 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
202 fileMenu
.addItem(MENU_DELETE
, "&Delete...");
204 fileMenu
.addSeparator();
205 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
206 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
207 fileMenu
.addSeparator();
208 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
209 fileMenu
.addSeparator();
210 fileMenu
.addItem(MENU_EXIT
, "E&xit");
212 setStatusBar(fileMenu
, "File-management "
213 + "commands (Open, Save, Print, etc.)");
215 // TODO: Edit: re-download, delete
221 getBackend().setTitle("Fanfix");
225 protected boolean onCommand(TCommandEvent command
) {
226 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
227 messageBox("title", "caption");
230 return super.onCommand(command
);
234 protected boolean onMenu(TMenuEvent menu
) {
236 switch (menu
.getId()) {
241 String openfile
= null;
243 openfile
= fileOpenBox(".");
244 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
246 } catch (IOException e
) {
248 error("Fail to open file"
249 + (openfile
== null ?
"" : ": " + openfile
),
257 MetaData meta
= null;
259 meta
= main
.getSelectedMeta();
262 luid
= meta
.getLuid();
263 story
= luid
+ ": " + meta
.getTitle();
267 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
268 + story
+ "\"", Type
.OKCANCEL
);
269 if (mbox
.getResult() == Result
.OK
) {
271 reader
.getLibrary().delete(luid
);
273 main
.refreshStories();
275 } catch (IOException e
) {
277 error("Fail to delete the story: \"" + story
+ "\"",
283 case MENU_IMPORT_URL
:
284 String clipboard
= "";
286 clipboard
= ("" + Toolkit
.getDefaultToolkit()
287 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
289 } catch (Exception e
) {
290 // No data will be handled
293 if (clipboard
== null || !clipboard
.startsWith("http")) {
297 String url
= inputBox("Import story", "URL to import", clipboard
)
303 error("URK not supported: " + url
, "Import error");
305 } catch (IOException e
) {
307 error("Fail to import URL: " + url
, "Import error", e
);
311 case MENU_IMPORT_FILE
:
312 String filename
= null;
314 filename
= fileOpenBox(".");
315 if (!imprt(filename
)) {
317 error("File not supported: " + filename
, "Import error");
319 } catch (IOException e
) {
321 error("Fail to import file"
322 + (filename
== null ?
"" : ": " + filename
),
331 return super.onMenu(menu
);
335 * Import the given url.
337 * Can fail if the host is not supported.
341 * @return TRUE in case of success, FALSE if the host is not supported
343 * @throws IOException
344 * in case of I/O error
346 private boolean imprt(String url
) throws IOException
{
348 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
349 main
.refreshStories();
351 } catch (UnknownHostException e
) {
357 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
359 reader
.openExternal(lib
, luid
, sync
);
363 * Display an error message and log it.
368 * the title of the error message
370 private void error(String message
, String title
) {
371 error(message
, title
, null);
375 * Display an error message and log it, including the linked
381 * the title of the error message
383 * the exception to log if any (can be NULL)
385 private void error(String message
, String title
, Exception e
) {
386 Instance
.getTraceHandler().error(title
+ ": " + message
);
388 Instance
.getTraceHandler().error(e
);
392 messageBox(title
, message
//
393 + "\n" + e
.getMessage());
395 messageBox(title
, message
);
400 * Ask the user and, if confirmed, close the {@link TApplication} this
401 * {@link TWidget} is running on.
403 * This should result in the program terminating.
406 * the {@link TWidget}
408 static public void close(TWidget widget
) {
409 close(widget
.getApplication());
413 * Ask the user and, if confirmed, close the {@link TApplication}.
415 * This should result in the program terminating.
418 * the {@link TApplication}
420 static void close(TApplication app
) {
422 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
423 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {