1 package be
.nikiroo
.fanfix
.reader
.tui
;
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
.data
.MetaData
;
18 import be
.nikiroo
.fanfix
.data
.Story
;
19 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
20 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
21 import be
.nikiroo
.fanfix
.reader
.Reader
;
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 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 // TODO: traces/errors?
144 Instance
.setTraceHandler(null);
146 // Add the menus TODO: i18n
147 TMenu fileMenu
= addMenu("&File");
148 fileMenu
.addItem(MENU_OPEN
, "&Open");
149 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
151 fileMenu
.addSeparator();
152 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
153 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
154 fileMenu
.addSeparator();
155 fileMenu
.addItem(MENU_EXIT
, "E&xit");
157 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management "
158 + "commands (Open, Save, Print, etc.)");
159 // TODO: doesn't actually work:
160 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
162 // TODO: Edit: re-download, delete
168 getBackend().setTitle("Fanfix");
172 protected boolean onMenu(TMenuEvent menu
) {
174 switch (menu
.getId()) {
176 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
177 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
182 case MENU_IMPORT_URL
:
183 String clipboard
= "";
185 clipboard
= ("" + Toolkit
.getDefaultToolkit()
186 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
188 } catch (Exception e
) {
189 // No data will be handled
192 if (clipboard
== null || !clipboard
.startsWith("http")) {
196 String url
= inputBox("Import story", "URL to import", clipboard
)
204 case MENU_IMPORT_FILE
:
206 String filename
= fileOpenBox(".");
207 if (!imprt(filename
)) {
210 } catch (IOException e
) {
218 return super.onMenu(menu
);
221 private boolean imprt(String url
) {
223 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
224 main
.refreshStories();
226 } catch (IOException e
) {
232 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
233 reader
.openExternal(lib
, luid
);