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
{
62 main
= new TuiReaderMainWindow(this);
63 main
.setSource(source
);
67 public void read() throws IOException
{
68 MetaData meta
= getMeta();
71 throw new IOException("No story to read");
74 // TODO: open in editor + external option
75 if (!meta
.isImageDocument()) {
76 @SuppressWarnings("unused")
77 Object discard
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
81 openExternal(getLibrary(), meta
.getLuid());
82 } catch (IOException e
) {
83 messageBox("Error when trying to open the story",
84 e
.getMessage(), TMessageBox
.Type
.OK
);
90 public MetaData
getMeta() {
91 return reader
.getMeta();
95 public Story
getStory(Progress pg
) {
96 return reader
.getStory(pg
);
100 public BasicLibrary
getLibrary() {
101 return reader
.getLibrary();
105 public void setLibrary(BasicLibrary lib
) {
106 reader
.setLibrary(lib
);
110 public void setMeta(MetaData meta
) throws IOException
{
111 reader
.setMeta(meta
);
115 public void setMeta(String luid
) throws IOException
{
116 reader
.setMeta(luid
);
120 public void setMeta(URL source
, Progress pg
) throws IOException
{
121 reader
.setMeta(source
, pg
);
125 public void browse(String source
) {
126 reader
.browse(source
);
130 public int getChapter() {
131 return reader
.getChapter();
135 public void setChapter(int chapter
) {
136 reader
.setChapter(chapter
);
139 private void init(Reader reader
) {
140 this.reader
= reader
;
142 // TODO: traces/errors?
143 Instance
.setTraceHandler(null);
145 // Add the menus TODO: i18n
146 TMenu fileMenu
= addMenu("&File");
147 fileMenu
.addItem(MENU_OPEN
, "&Open");
148 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
150 fileMenu
.addSeparator();
151 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
152 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
153 fileMenu
.addSeparator();
154 fileMenu
.addItem(MENU_EXIT
, "E&xit");
156 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management "
157 + "commands (Open, Save, Print, etc.)");
158 // TODO: doesn't actually work:
159 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
161 // TODO: Edit: re-download, delete
167 getBackend().setTitle("Fanfix");
171 protected boolean onMenu(TMenuEvent menu
) {
173 switch (menu
.getId()) {
175 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
176 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
181 case MENU_IMPORT_URL
:
182 String clipboard
= "";
184 clipboard
= ("" + Toolkit
.getDefaultToolkit()
185 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
187 } catch (Exception e
) {
188 // No data will be handled
191 if (clipboard
== null || !clipboard
.startsWith("http")) {
195 String url
= inputBox("Import story", "URL to import", clipboard
)
203 case MENU_IMPORT_FILE
:
205 String filename
= fileOpenBox(".");
206 if (!imprt(filename
)) {
209 } catch (IOException e
) {
217 return super.onMenu(menu
);
220 private boolean imprt(String url
) {
222 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
223 main
.refreshStories();
225 } catch (IOException e
) {
231 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
232 reader
.openExternal(lib
, luid
);