6fa969b50da925f0ffe1994fa0f25dbc76faec8f
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_LIBRARY
= 1029;
38 public static final int MENU_EXIT
= 1030;
40 private Reader reader
;
41 private TuiReaderMainWindow main
;
43 private MetaData meta
;
44 private String source
;
45 private boolean useMeta
;
47 // start reading if meta present
48 public TuiReaderApplication(Reader reader
, BackendType backend
)
53 MetaData meta
= getMeta();
55 showMain(meta
, null, true);
58 public TuiReaderApplication(Reader reader
, String source
,
59 TApplication
.BackendType backend
) throws Exception
{
63 showMain(null, source
, false);
66 private void showMain(MetaData meta
, String source
, boolean useMeta
)
68 // TODO: thread-safety
71 this.useMeta
= useMeta
;
73 if (main
!= null && main
.isVisible()) {
79 main
= new TuiReaderMainWindow(this);
86 main
.setSource(source
);
92 public void read() throws IOException
{
93 MetaData meta
= getMeta();
96 throw new IOException("No story to read");
99 // TODO: open in editor + external option
100 if (!meta
.isImageDocument()) {
101 @SuppressWarnings("unused")
102 Object discard
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
106 openExternal(getLibrary(), meta
.getLuid());
107 } catch (IOException e
) {
108 messageBox("Error when trying to open the story",
109 e
.getMessage(), TMessageBox
.Type
.OK
);
115 public MetaData
getMeta() {
116 return reader
.getMeta();
120 public Story
getStory(Progress pg
) {
121 return reader
.getStory(pg
);
125 public BasicLibrary
getLibrary() {
126 return reader
.getLibrary();
130 public void setLibrary(BasicLibrary lib
) {
131 reader
.setLibrary(lib
);
135 public void setMeta(MetaData meta
) throws IOException
{
136 reader
.setMeta(meta
);
140 public void setMeta(String luid
) throws IOException
{
141 reader
.setMeta(luid
);
145 public void setMeta(URL source
, Progress pg
) throws IOException
{
146 reader
.setMeta(source
, pg
);
150 public void browse(String source
) {
151 reader
.browse(source
);
155 public int getChapter() {
156 return reader
.getChapter();
160 public void setChapter(int chapter
) {
161 reader
.setChapter(chapter
);
164 private void init(Reader reader
) {
165 this.reader
= reader
;
167 // TODO: traces/errors?
168 Instance
.setTraceHandler(null);
170 // Add the menus TODO: i18n
171 TMenu fileMenu
= addMenu("&File");
172 fileMenu
.addItem(MENU_OPEN
, "&Open");
173 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
175 fileMenu
.addSeparator();
176 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
177 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
178 fileMenu
.addSeparator();
179 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
180 fileMenu
.addSeparator();
181 fileMenu
.addItem(MENU_EXIT
, "E&xit");
183 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management "
184 + "commands (Open, Save, Print, etc.)");
185 // TODO: doesn't actually work:
186 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
188 // TODO: Edit: re-download, delete
194 getBackend().setTitle("Fanfix");
198 protected boolean onMenu(TMenuEvent menu
) {
200 switch (menu
.getId()) {
202 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
203 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
204 exit(); // TODO: exit(false) for "no confirm box"
208 case MENU_IMPORT_URL
:
209 String clipboard
= "";
211 clipboard
= ("" + Toolkit
.getDefaultToolkit()
212 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
214 } catch (Exception e
) {
215 // No data will be handled
218 if (clipboard
== null || !clipboard
.startsWith("http")) {
222 String url
= inputBox("Import story", "URL to import", clipboard
)
230 case MENU_IMPORT_FILE
:
232 String filename
= fileOpenBox(".");
233 if (!imprt(filename
)) {
236 } catch (IOException e
) {
244 showMain(meta
, source
, useMeta
);
245 } catch (IOException e
) {
252 return super.onMenu(menu
);
255 private boolean imprt(String url
) {
257 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
258 main
.refreshStories();
260 } catch (IOException e
) {
266 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
267 reader
.openExternal(lib
, luid
);