1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.Toolkit
;
4 import java
.awt
.datatransfer
.DataFlavor
;
5 import java
.io
.IOException
;
8 import jexer
.TApplication
;
10 import jexer
.TKeypress
;
11 import jexer
.TMessageBox
;
12 import jexer
.TStatusBar
;
14 import jexer
.event
.TMenuEvent
;
15 import jexer
.menu
.TMenu
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.Instance
.SyserrHandler
;
18 import be
.nikiroo
.fanfix
.Instance
.TraceHandler
;
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
.utils
.Progress
;
25 * Manages the TUI general mode and links and manages the {@link TWindow}s.
27 * It will also enclose a {@link Reader} and simply handle the reading part
28 * differently (it will create the required sub-windows and display them).
32 class TuiReaderApplication
extends TApplication
implements Reader
{
33 public static final int MENU_OPEN
= 1025;
34 public static final int MENU_IMPORT_URL
= 1026;
35 public static final int MENU_IMPORT_FILE
= 1027;
36 public static final int MENU_EXPORT
= 1028;
37 public static final int MENU_EXIT
= 1029;
39 private Reader reader
;
40 private TuiReaderMainWindow main
;
42 // start reading if meta present
43 public TuiReaderApplication(Reader reader
, BackendType backend
)
48 MetaData meta
= getMeta();
50 main
= new TuiReaderMainWindow(this);
57 public TuiReaderApplication(Reader reader
, String source
,
58 TApplication
.BackendType backend
) throws Exception
{
63 main
= new TuiReaderMainWindow(this);
64 main
.setSource(source
);
68 public void read() throws IOException
{
69 MetaData meta
= getMeta();
72 throw new IOException("No story to read");
75 // TODO: open in editor + external option
76 if (!meta
.isImageDocument()) {
77 @SuppressWarnings("unused")
78 Object discard
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
82 BasicReader
.openExternal(getLibrary(), meta
.getLuid());
83 } catch (IOException e
) {
84 messageBox("Error when trying to open the story",
85 e
.getMessage(), TMessageBox
.Type
.OK
);
91 public MetaData
getMeta() {
92 return reader
.getMeta();
96 public Story
getStory(Progress pg
) {
97 return reader
.getStory(pg
);
101 public BasicLibrary
getLibrary() {
102 return reader
.getLibrary();
106 public void setLibrary(BasicLibrary lib
) {
107 reader
.setLibrary(lib
);
111 public void setMeta(MetaData meta
) throws IOException
{
112 reader
.setMeta(meta
);
116 public void setMeta(String luid
) throws IOException
{
117 reader
.setMeta(luid
);
121 public void setMeta(URL source
, Progress pg
) throws IOException
{
122 reader
.setMeta(source
, pg
);
126 public void browse(String source
) {
127 reader
.browse(source
);
131 public int getChapter() {
132 return reader
.getChapter();
136 public void setChapter(int chapter
) {
137 reader
.setChapter(chapter
);
140 private void init(Reader reader
) {
141 this.reader
= reader
;
143 // Do not allow traces/debug to pollute the screen:
144 Instance
.setSyserrHandler(new SyserrHandler() {
146 public void notify(Exception e
, boolean showDetails
) {
151 Instance
.setTraceHandler(new TraceHandler() {
153 public void trace(String message
) {
159 // Add the menus TODO: i18n
160 TMenu fileMenu
= addMenu("&File");
161 fileMenu
.addItem(MENU_OPEN
, "&Open");
162 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
164 fileMenu
.addSeparator();
165 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
166 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
167 fileMenu
.addSeparator();
168 fileMenu
.addItem(MENU_EXIT
, "E&xit");
170 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management "
171 + "commands (Open, Save, Print, etc.)");
172 // TODO: doesn't actually work:
173 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
175 // TODO: Edit: re-download, delete
181 getBackend().setTitle("Fanfix");
185 protected boolean onMenu(TMenuEvent menu
) {
187 switch (menu
.getId()) {
189 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
190 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
195 case MENU_IMPORT_URL
:
196 String clipboard
= "";
198 clipboard
= ("" + Toolkit
.getDefaultToolkit()
199 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
201 } catch (Exception e
) {
202 // No data will be handled
205 if (clipboard
== null || !clipboard
.startsWith("http")) {
209 String url
= inputBox("Import story", "URL to import", clipboard
)
217 case MENU_IMPORT_FILE
:
219 String filename
= fileOpenBox(".");
220 if (!imprt(filename
)) {
223 } catch (IOException e
) {
231 return super.onMenu(menu
);
234 private boolean imprt(String url
) {
236 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
237 main
.refreshStories();
239 } catch (IOException e
) {