7dc1fd5919609b64e33e88466318f668ee9ba336
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 showMain(getMeta(), null, true);
56 public TuiReaderApplication(Reader reader
, String source
,
57 TApplication
.BackendType backend
) throws Exception
{
61 showMain(null, source
, false);
65 public void read() throws IOException
{
66 MetaData meta
= getMeta();
69 throw new IOException("No story to read");
72 // TODO: open in editor + external option
73 if (!meta
.isImageDocument()) {
74 @SuppressWarnings("unused")
75 Object discard
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
79 openExternal(getLibrary(), meta
.getLuid());
80 } catch (IOException e
) {
81 messageBox("Error when trying to open the story",
82 e
.getMessage(), TMessageBox
.Type
.OK
);
88 public MetaData
getMeta() {
89 return reader
.getMeta();
93 public Story
getStory(Progress pg
) {
94 return reader
.getStory(pg
);
98 public BasicLibrary
getLibrary() {
99 return reader
.getLibrary();
103 public void setLibrary(BasicLibrary lib
) {
104 reader
.setLibrary(lib
);
108 public void setMeta(MetaData meta
) throws IOException
{
109 reader
.setMeta(meta
);
113 public void setMeta(String luid
) throws IOException
{
114 reader
.setMeta(luid
);
118 public void setMeta(URL source
, Progress pg
) throws IOException
{
119 reader
.setMeta(source
, pg
);
123 public void browse(String source
) {
124 reader
.browse(source
);
128 public int getChapter() {
129 return reader
.getChapter();
133 public void setChapter(int chapter
) {
134 reader
.setChapter(chapter
);
138 * Set the default status bar when this window appear.
140 * Some shortcuts are always visible, and will be put here.
142 * Note that shortcuts placed this way on menu won't work unless the menu
143 * also implement them.
146 * the new window or menu on screen
148 * the description to show on the status ba
150 public TStatusBar
setStatusBar(TWindow window
, String description
) {
151 TStatusBar statusBar
= window
.newStatusBar(description
);
152 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
157 private void showMain(MetaData meta
, String source
, boolean useMeta
)
159 // TODO: thread-safety
161 this.source
= source
;
162 this.useMeta
= useMeta
;
164 if (main
!= null && main
.isVisible()) {
170 main
= new TuiReaderMainWindow(this);
177 main
.setSource(source
);
182 private void init(Reader reader
) {
183 this.reader
= reader
;
185 // TODO: traces/errors?
186 Instance
.setTraceHandler(null);
188 // Add the menus TODO: i18n
189 TMenu fileMenu
= addMenu("&File");
190 fileMenu
.addItem(MENU_OPEN
, "&Open");
191 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
193 fileMenu
.addSeparator();
194 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
195 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
196 fileMenu
.addSeparator();
197 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
198 fileMenu
.addSeparator();
199 fileMenu
.addItem(MENU_EXIT
, "E&xit");
201 setStatusBar(fileMenu
, "File-management "
202 + "commands (Open, Save, Print, etc.)");
204 // TODO: Edit: re-download, delete
210 getBackend().setTitle("Fanfix");
214 protected boolean onMenu(TMenuEvent menu
) {
216 switch (menu
.getId()) {
218 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
219 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
220 exit(); // TODO: exit(false) for "no confirm box"
224 case MENU_IMPORT_URL
:
225 String clipboard
= "";
227 clipboard
= ("" + Toolkit
.getDefaultToolkit()
228 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
230 } catch (Exception e
) {
231 // No data will be handled
234 if (clipboard
== null || !clipboard
.startsWith("http")) {
238 String url
= inputBox("Import story", "URL to import", clipboard
)
246 case MENU_IMPORT_FILE
:
248 String filename
= fileOpenBox(".");
249 if (!imprt(filename
)) {
252 } catch (IOException e
) {
260 showMain(meta
, source
, useMeta
);
261 } catch (IOException e
) {
268 return super.onMenu(menu
);
271 private boolean imprt(String url
) {
273 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
274 main
.refreshStories();
276 } catch (IOException e
) {
282 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
283 reader
.openExternal(lib
, luid
);