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 MetaData meta
= getMeta();
65 public TuiReaderApplication(Reader reader
, String source
,
66 TApplication
.BackendType backend
) throws Exception
{
71 main
.setMode(Mode
.SOURCE
, source
);
75 public void read() throws IOException
{
80 public MetaData
getMeta() {
81 return reader
.getMeta();
85 public Story
getStory(Progress pg
) {
86 return reader
.getStory(pg
);
90 public BasicLibrary
getLibrary() {
91 return reader
.getLibrary();
95 public void setLibrary(BasicLibrary lib
) {
96 reader
.setLibrary(lib
);
100 public void setMeta(MetaData meta
) throws IOException
{
101 reader
.setMeta(meta
);
105 public void setMeta(String luid
) throws IOException
{
106 reader
.setMeta(luid
);
110 public void setMeta(URL source
, Progress pg
) throws IOException
{
111 reader
.setMeta(source
, pg
);
115 public void browse(String source
) {
116 reader
.browse(source
);
120 public int getChapter() {
121 return reader
.getChapter();
125 public void setChapter(int chapter
) {
126 reader
.setChapter(chapter
);
129 public void read(Story story
) throws IOException
{
131 throw new IOException("No story to read");
134 // TODO: open in editor + external option
135 if (!story
.getMeta().isImageDocument()) {
136 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
140 openExternal(getLibrary(), story
.getMeta().getLuid());
141 } catch (IOException e
) {
142 messageBox("Error when trying to open the story",
143 e
.getMessage(), TMessageBox
.Type
.OK
);
149 * Set the default status bar when this window appear.
151 * Some shortcuts are always visible, and will be put here.
153 * Note that shortcuts placed this way on menu won't work unless the menu
154 * also implement them.
157 * the new window or menu on screen
159 * the description to show on the status ba
161 public TStatusBar
setStatusBar(TWindow window
, String description
) {
162 TStatusBar statusBar
= window
.newStatusBar(description
);
163 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
168 private void showMain() {
169 if (main
!= null && main
.isVisible()) {
175 main
= new TuiReaderMainWindow(this);
180 private void init(Reader reader
) {
181 this.reader
= reader
;
183 // TODO: traces/errors?
184 Instance
.setTraceHandler(null);
186 // Add the menus TODO: i18n
187 TMenu fileMenu
= addMenu("&File");
188 fileMenu
.addItem(MENU_OPEN
, "&Open...");
189 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
190 fileMenu
.addItem(MENU_DELETE
, "&Delete...");
192 fileMenu
.addSeparator();
193 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
194 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
195 fileMenu
.addSeparator();
196 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
197 fileMenu
.addSeparator();
198 fileMenu
.addItem(MENU_EXIT
, "E&xit");
200 setStatusBar(fileMenu
, "File-management "
201 + "commands (Open, Save, Print, etc.)");
203 // TODO: Edit: re-download, delete
209 getBackend().setTitle("Fanfix");
213 protected boolean onCommand(TCommandEvent command
) {
214 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
215 messageBox("title", "caption");
218 return super.onCommand(command
);
222 protected boolean onMenu(TMenuEvent menu
) {
224 switch (menu
.getId()) {
229 String openfile
= null;
231 openfile
= fileOpenBox(".");
232 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
234 } catch (IOException e
) {
236 error("Fail to open file"
237 + (openfile
== null ?
"" : ": " + openfile
),
245 MetaData meta
= null;
247 meta
= main
.getSelectedMeta();
250 luid
= meta
.getLuid();
251 story
= luid
+ ": " + meta
.getTitle();
255 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
256 + story
+ "\"", Type
.OKCANCEL
);
257 if (mbox
.getResult() == Result
.OK
) {
259 reader
.getLibrary().delete(luid
);
261 main
.refreshStories();
263 } catch (IOException e
) {
265 error("Fail to delete the story: \"" + story
+ "\"",
271 case MENU_IMPORT_URL
:
272 String clipboard
= "";
274 clipboard
= ("" + Toolkit
.getDefaultToolkit()
275 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
277 } catch (Exception e
) {
278 // No data will be handled
281 if (clipboard
== null || !clipboard
.startsWith("http")) {
285 String url
= inputBox("Import story", "URL to import", clipboard
)
291 error("URK not supported: " + url
, "Import error");
293 } catch (IOException e
) {
295 error("Fail to import URL: " + url
, "Import error", e
);
299 case MENU_IMPORT_FILE
:
300 String filename
= null;
302 filename
= fileOpenBox(".");
303 if (!imprt(filename
)) {
305 error("File not supported: " + filename
, "Import error");
307 } catch (IOException e
) {
309 error("Fail to import file"
310 + (filename
== null ?
"" : ": " + filename
),
319 return super.onMenu(menu
);
323 * Import the given url.
325 * Can fail if the host is not supported.
329 * @return TRUE in case of success, FALSE if the host is not supported
331 * @throws IOException
332 * in case of I/O error
334 private boolean imprt(String url
) throws IOException
{
336 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
337 main
.refreshStories();
339 } catch (UnknownHostException e
) {
345 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
346 reader
.openExternal(lib
, luid
);
350 * Display an error message and log it.
355 * the title of the error message
357 private void error(String message
, String title
) {
358 error(message
, title
, null);
362 * Display an error message and log it, including the linked
368 * the title of the error message
370 * the exception to log if any (can be NULL)
372 private void error(String message
, String title
, Exception e
) {
373 Instance
.getTraceHandler().error(title
+ ": " + message
);
375 Instance
.getTraceHandler().error(e
);
379 messageBox(title
, message
//
380 + "\n" + e
.getMessage());
382 messageBox(title
, message
);
387 * Ask the user and, if confirmed, close the {@link TApplication} this
388 * {@link TWidget} is running on.
390 * This should result in the program terminating.
393 * the {@link TWidget}
395 static public void close(TWidget widget
) {
396 close(widget
.getApplication());
400 * Ask the user and, if confirmed, close the {@link TApplication}.
402 * This should result in the program terminating.
405 * the {@link TApplication}
407 static void close(TApplication app
) {
409 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
410 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {