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
;
15 import jexer
.event
.TMenuEvent
;
16 import jexer
.menu
.TMenu
;
17 import be
.nikiroo
.fanfix
.Instance
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Story
;
20 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
21 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
22 import be
.nikiroo
.fanfix
.reader
.Reader
;
23 import be
.nikiroo
.utils
.Progress
;
26 * Manages the TUI general mode and links and manages the {@link TWindow}s.
28 * It will also enclose a {@link Reader} and simply handle the reading part
29 * differently (it will create the required sub-windows and display them).
33 class TuiReaderApplication
extends TApplication
implements Reader
{
34 public static final int MENU_OPEN
= 1025;
35 public static final int MENU_IMPORT_URL
= 1026;
36 public static final int MENU_IMPORT_FILE
= 1027;
37 public static final int MENU_EXPORT
= 1028;
38 public static final int MENU_LIBRARY
= 1029;
39 public static final int MENU_EXIT
= 1030;
41 public static final TCommand CMD_EXIT
= new TCommand(MENU_EXIT
) {
44 private Reader reader
;
45 private TuiReaderMainWindow main
;
47 private MetaData meta
;
48 private String source
;
49 private boolean useMeta
;
51 // start reading if meta present
52 public TuiReaderApplication(Reader reader
, BackendType backend
)
57 showMain(getMeta(), null, true);
60 public TuiReaderApplication(Reader reader
, String source
,
61 TApplication
.BackendType backend
) throws Exception
{
65 showMain(null, source
, false);
69 public void read() throws IOException
{
70 MetaData meta
= getMeta();
73 throw new IOException("No story to read");
76 // TODO: open in editor + external option
77 if (!meta
.isImageDocument()) {
78 TWindow window
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
83 openExternal(getLibrary(), meta
.getLuid());
84 } catch (IOException e
) {
85 messageBox("Error when trying to open the story",
86 e
.getMessage(), TMessageBox
.Type
.OK
);
92 public MetaData
getMeta() {
93 return reader
.getMeta();
97 public Story
getStory(Progress pg
) {
98 return reader
.getStory(pg
);
102 public BasicLibrary
getLibrary() {
103 return reader
.getLibrary();
107 public void setLibrary(BasicLibrary lib
) {
108 reader
.setLibrary(lib
);
112 public void setMeta(MetaData meta
) throws IOException
{
113 reader
.setMeta(meta
);
117 public void setMeta(String luid
) throws IOException
{
118 reader
.setMeta(luid
);
122 public void setMeta(URL source
, Progress pg
) throws IOException
{
123 reader
.setMeta(source
, pg
);
127 public void browse(String source
) {
128 reader
.browse(source
);
132 public int getChapter() {
133 return reader
.getChapter();
137 public void setChapter(int chapter
) {
138 reader
.setChapter(chapter
);
142 * Set the default status bar when this window appear.
144 * Some shortcuts are always visible, and will be put here.
146 * Note that shortcuts placed this way on menu won't work unless the menu
147 * also implement them.
150 * the new window or menu on screen
152 * the description to show on the status ba
154 public TStatusBar
setStatusBar(TWindow window
, String description
) {
155 TStatusBar statusBar
= window
.newStatusBar(description
);
156 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
161 private void showMain(MetaData meta
, String source
, boolean useMeta
)
163 // TODO: thread-safety
165 this.source
= source
;
166 this.useMeta
= useMeta
;
168 if (main
!= null && main
.isVisible()) {
174 main
= new TuiReaderMainWindow(this);
181 main
.setSource(source
);
186 private void init(Reader reader
) {
187 this.reader
= reader
;
189 // TODO: traces/errors?
190 Instance
.setTraceHandler(null);
192 // Add the menus TODO: i18n
193 TMenu fileMenu
= addMenu("&File");
194 fileMenu
.addItem(MENU_OPEN
, "&Open");
195 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
197 fileMenu
.addSeparator();
198 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
199 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
200 fileMenu
.addSeparator();
201 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
202 fileMenu
.addSeparator();
203 fileMenu
.addItem(MENU_EXIT
, "E&xit");
205 setStatusBar(fileMenu
, "File-management "
206 + "commands (Open, Save, Print, etc.)");
208 // TODO: Edit: re-download, delete
214 getBackend().setTitle("Fanfix");
218 protected boolean onMenu(TMenuEvent menu
) {
220 switch (menu
.getId()) {
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
);
287 * Ask the user and, if confirmed, close the {@link TApplication} this
288 * {@link TWidget} is running on.
290 * This should result in the program terminating.
293 * the {@link TWidget}
295 static public void close(TWidget widget
) {
296 close(widget
.getApplication());
300 * Ask the user and, if confirmed, close the {@link TApplication}.
302 * This should result in the program terminating.
305 * the {@link TApplication}
307 static void close(TApplication app
) {
309 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
310 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {