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
.TCommandEvent
;
17 import jexer
.event
.TMenuEvent
;
18 import jexer
.menu
.TMenu
;
19 import be
.nikiroo
.fanfix
.Instance
;
20 import be
.nikiroo
.fanfix
.data
.MetaData
;
21 import be
.nikiroo
.fanfix
.data
.Story
;
22 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
23 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
24 import be
.nikiroo
.fanfix
.reader
.Reader
;
25 import be
.nikiroo
.fanfix
.reader
.tui
.TuiReaderMainWindow
.Mode
;
26 import be
.nikiroo
.utils
.Progress
;
29 * Manages the TUI general mode and links and manages the {@link TWindow}s.
31 * It will also enclose a {@link Reader} and simply handle the reading part
32 * differently (it will create the required sub-windows and display them).
36 class TuiReaderApplication
extends TApplication
implements Reader
{
37 public static final int MENU_OPEN
= 1025;
38 public static final int MENU_IMPORT_URL
= 1026;
39 public static final int MENU_IMPORT_FILE
= 1027;
40 public static final int MENU_EXPORT
= 1028;
41 public static final int MENU_LIBRARY
= 1029;
42 public static final int MENU_EXIT
= 1030;
44 public static final TCommand CMD_EXIT
= new TCommand(MENU_EXIT
) {
47 private Reader reader
;
48 private TuiReaderMainWindow main
;
50 // start reading if meta present
51 public TuiReaderApplication(Reader reader
, BackendType backend
)
56 MetaData meta
= getMeta();
62 public TuiReaderApplication(Reader reader
, String source
,
63 TApplication
.BackendType backend
) throws Exception
{
68 main
.setMode(Mode
.SOURCE
, source
);
72 public void read() throws IOException
{
77 public MetaData
getMeta() {
78 return reader
.getMeta();
82 public Story
getStory(Progress pg
) {
83 return reader
.getStory(pg
);
87 public BasicLibrary
getLibrary() {
88 return reader
.getLibrary();
92 public void setLibrary(BasicLibrary lib
) {
93 reader
.setLibrary(lib
);
97 public void setMeta(MetaData meta
) throws IOException
{
102 public void setMeta(String luid
) throws IOException
{
103 reader
.setMeta(luid
);
107 public void setMeta(URL source
, Progress pg
) throws IOException
{
108 reader
.setMeta(source
, pg
);
112 public void browse(String source
) {
113 reader
.browse(source
);
117 public int getChapter() {
118 return reader
.getChapter();
122 public void setChapter(int chapter
) {
123 reader
.setChapter(chapter
);
126 public void read(Story story
) throws IOException
{
128 throw new IOException("No story to read");
131 // TODO: open in editor + external option
132 if (!story
.getMeta().isImageDocument()) {
133 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
137 openExternal(getLibrary(), story
.getMeta().getLuid());
138 } catch (IOException e
) {
139 messageBox("Error when trying to open the story",
140 e
.getMessage(), TMessageBox
.Type
.OK
);
146 * Set the default status bar when this window appear.
148 * Some shortcuts are always visible, and will be put here.
150 * Note that shortcuts placed this way on menu won't work unless the menu
151 * also implement them.
154 * the new window or menu on screen
156 * the description to show on the status ba
158 public TStatusBar
setStatusBar(TWindow window
, String description
) {
159 TStatusBar statusBar
= window
.newStatusBar(description
);
160 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
165 private void showMain() {
166 if (main
!= null && main
.isVisible()) {
172 main
= new TuiReaderMainWindow(this);
177 private void init(Reader reader
) {
178 this.reader
= reader
;
180 // TODO: traces/errors?
181 Instance
.setTraceHandler(null);
183 // Add the menus TODO: i18n
184 TMenu fileMenu
= addMenu("&File");
185 fileMenu
.addItem(MENU_OPEN
, "&Open");
186 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
188 fileMenu
.addSeparator();
189 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
190 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
191 fileMenu
.addSeparator();
192 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
193 fileMenu
.addSeparator();
194 fileMenu
.addItem(MENU_EXIT
, "E&xit");
196 setStatusBar(fileMenu
, "File-management "
197 + "commands (Open, Save, Print, etc.)");
199 // TODO: Edit: re-download, delete
205 getBackend().setTitle("Fanfix");
209 protected boolean onCommand(TCommandEvent command
) {
210 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
211 messageBox("title", "caption");
214 return super.onCommand(command
);
218 protected boolean onMenu(TMenuEvent menu
) {
220 switch (menu
.getId()) {
225 String openfile
= null;
227 openfile
= fileOpenBox(".");
228 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
230 } catch (IOException e
) {
232 error("Fail to open file"
233 + (openfile
== null ?
"" : ": " + openfile
),
238 case MENU_IMPORT_URL
:
239 String clipboard
= "";
241 clipboard
= ("" + Toolkit
.getDefaultToolkit()
242 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
244 } catch (Exception e
) {
245 // No data will be handled
248 if (clipboard
== null || !clipboard
.startsWith("http")) {
252 String url
= inputBox("Import story", "URL to import", clipboard
)
258 error("URK not supported: " + url
, "Import error");
260 } catch (IOException e
) {
262 error("Fail to import URL: " + url
, "Import error", e
);
266 case MENU_IMPORT_FILE
:
267 String filename
= null;
269 filename
= fileOpenBox(".");
270 if (!imprt(filename
)) {
272 error("File not supported: " + filename
, "Import error");
274 } catch (IOException e
) {
276 error("Fail to import file"
277 + (filename
== null ?
"" : ": " + filename
),
286 return super.onMenu(menu
);
290 * Import the given url.
292 * Can fail if the host is not supported.
296 * @return TRUE in case of success, FALSE if the host is not supported
298 * @throws IOException
299 * in case of I/O error
301 private boolean imprt(String url
) throws IOException
{
303 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
304 main
.refreshStories();
306 } catch (UnknownHostException e
) {
312 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
313 reader
.openExternal(lib
, luid
);
317 * Display an error message and log it.
322 * the title of the error message
324 private void error(String message
, String title
) {
325 error(message
, title
, null);
329 * Display an error message and log it, including the linked
335 * the title of the error message
337 * the exception to log if any (can be NULL)
339 private void error(String message
, String title
, Exception e
) {
340 Instance
.getTraceHandler().error(title
+ ": " + message
);
342 Instance
.getTraceHandler().error(e
);
346 messageBox(title
, message
//
347 + "\n" + e
.getMessage());
349 messageBox(title
, message
);
354 * Ask the user and, if confirmed, close the {@link TApplication} this
355 * {@link TWidget} is running on.
357 * This should result in the program terminating.
360 * the {@link TWidget}
362 static public void close(TWidget widget
) {
363 close(widget
.getApplication());
367 * Ask the user and, if confirmed, close the {@link TApplication}.
369 * This should result in the program terminating.
372 * the {@link TApplication}
374 static void close(TApplication app
) {
376 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
377 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {