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
;
47 private String source
;
49 // start reading if meta present
50 public TuiReaderApplication(Reader reader
, BackendType backend
)
55 MetaData meta
= getMeta();
61 public TuiReaderApplication(Reader reader
, String source
,
62 TApplication
.BackendType backend
) throws Exception
{
71 public void read() throws IOException
{
76 public MetaData
getMeta() {
77 return reader
.getMeta();
81 public Story
getStory(Progress pg
) {
82 return reader
.getStory(pg
);
86 public BasicLibrary
getLibrary() {
87 return reader
.getLibrary();
91 public void setLibrary(BasicLibrary lib
) {
92 reader
.setLibrary(lib
);
96 public void setMeta(MetaData meta
) throws IOException
{
101 public void setMeta(String luid
) throws IOException
{
102 reader
.setMeta(luid
);
106 public void setMeta(URL source
, Progress pg
) throws IOException
{
107 reader
.setMeta(source
, pg
);
111 public void browse(String source
) {
112 reader
.browse(source
);
116 public int getChapter() {
117 return reader
.getChapter();
121 public void setChapter(int chapter
) {
122 reader
.setChapter(chapter
);
125 public void read(Story story
) throws IOException
{
127 throw new IOException("No story to read");
130 // TODO: open in editor + external option
131 if (!story
.getMeta().isImageDocument()) {
132 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
136 openExternal(getLibrary(), story
.getMeta().getLuid());
137 } catch (IOException e
) {
138 messageBox("Error when trying to open the story",
139 e
.getMessage(), TMessageBox
.Type
.OK
);
145 * Set the default status bar when this window appear.
147 * Some shortcuts are always visible, and will be put here.
149 * Note that shortcuts placed this way on menu won't work unless the menu
150 * also implement them.
153 * the new window or menu on screen
155 * the description to show on the status ba
157 public TStatusBar
setStatusBar(TWindow window
, String description
) {
158 TStatusBar statusBar
= window
.newStatusBar(description
);
159 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
164 private void showMain() {
165 if (main
!= null && main
.isVisible()) {
171 main
= new TuiReaderMainWindow(this);
176 private void setSource(String source
) {
177 this.source
= source
;
179 main
.setSource(source
);
182 private void init(Reader reader
) {
183 this.reader
= reader
;
185 // TODO: traces/errors?
186 Instance
.setTraceHandler(null);
188 // Add the menus TODO: i18n
189 TMenu fileMenu
= addMenu("&File");
190 fileMenu
.addItem(MENU_OPEN
, "&Open");
191 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
193 fileMenu
.addSeparator();
194 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
195 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
196 fileMenu
.addSeparator();
197 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
198 fileMenu
.addSeparator();
199 fileMenu
.addItem(MENU_EXIT
, "E&xit");
201 setStatusBar(fileMenu
, "File-management "
202 + "commands (Open, Save, Print, etc.)");
204 // TODO: Edit: re-download, delete
210 getBackend().setTitle("Fanfix");
214 protected boolean onMenu(TMenuEvent menu
) {
216 switch (menu
.getId()) {
221 String openfile
= null;
223 openfile
= fileOpenBox(".");
224 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
226 } catch (IOException e
) {
228 error("Fail to open file"
229 + (openfile
== null ?
"" : ": " + openfile
),
234 case MENU_IMPORT_URL
:
235 String clipboard
= "";
237 clipboard
= ("" + Toolkit
.getDefaultToolkit()
238 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
240 } catch (Exception e
) {
241 // No data will be handled
244 if (clipboard
== null || !clipboard
.startsWith("http")) {
248 String url
= inputBox("Import story", "URL to import", clipboard
)
254 error("URK not supported: " + url
, "Import error");
256 } catch (IOException e
) {
258 error("Fail to import URL: " + url
, "Import error", e
);
262 case MENU_IMPORT_FILE
:
263 String filename
= null;
265 filename
= fileOpenBox(".");
266 if (!imprt(filename
)) {
268 error("File not supported: " + filename
, "Import error");
270 } catch (IOException e
) {
272 error("Fail to import file"
273 + (filename
== null ?
"" : ": " + filename
),
283 return super.onMenu(menu
);
287 * Import the given url.
289 * Can fail if the host is not supported.
293 * @return TRUE in case of success, FALSE if the host is not supported
295 * @throws IOException
296 * in case of I/O error
298 private boolean imprt(String url
) throws IOException
{
300 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
301 main
.refreshStories();
303 } catch (UnknownHostException e
) {
309 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
310 reader
.openExternal(lib
, luid
);
314 * Display an error message and log it.
319 * the title of the error message
321 private void error(String message
, String title
) {
322 error(message
, title
, null);
326 * Display an error message and log it, including the linked
332 * the title of the error message
334 * the exception to log if any (can be NULL)
336 private void error(String message
, String title
, Exception e
) {
337 Instance
.getTraceHandler().error(title
+ ": " + message
);
339 Instance
.getTraceHandler().error(e
);
343 messageBox(title
, message
//
344 + "\n" + e
.getMessage());
346 messageBox(title
, message
);
351 * Ask the user and, if confirmed, close the {@link TApplication} this
352 * {@link TWidget} is running on.
354 * This should result in the program terminating.
357 * the {@link TWidget}
359 static public void close(TWidget widget
) {
360 close(widget
.getApplication());
364 * Ask the user and, if confirmed, close the {@link TApplication}.
366 * This should result in the program terminating.
369 * the {@link TApplication}
371 static void close(TApplication app
) {
373 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
374 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {