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
);
187 private void init(Reader reader
) {
188 this.reader
= reader
;
190 // TODO: traces/errors?
191 Instance
.setTraceHandler(null);
193 // Add the menus TODO: i18n
194 TMenu fileMenu
= addMenu("&File");
195 fileMenu
.addItem(MENU_OPEN
, "&Open");
196 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
198 fileMenu
.addSeparator();
199 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
200 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
201 fileMenu
.addSeparator();
202 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
203 fileMenu
.addSeparator();
204 fileMenu
.addItem(MENU_EXIT
, "E&xit");
206 setStatusBar(fileMenu
, "File-management "
207 + "commands (Open, Save, Print, etc.)");
209 // TODO: Edit: re-download, delete
215 getBackend().setTitle("Fanfix");
219 protected boolean onMenu(TMenuEvent menu
) {
221 switch (menu
.getId()) {
225 case MENU_IMPORT_URL
:
226 String clipboard
= "";
228 clipboard
= ("" + Toolkit
.getDefaultToolkit()
229 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
231 } catch (Exception e
) {
232 // No data will be handled
235 if (clipboard
== null || !clipboard
.startsWith("http")) {
239 String url
= inputBox("Import story", "URL to import", clipboard
)
247 case MENU_IMPORT_FILE
:
249 String filename
= fileOpenBox(".");
250 if (!imprt(filename
)) {
253 } catch (IOException e
) {
261 showMain(meta
, source
, useMeta
);
262 } catch (IOException e
) {
269 return super.onMenu(menu
);
272 private boolean imprt(String url
) {
274 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
275 main
.refreshStories();
277 } catch (IOException e
) {
283 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
284 reader
.openExternal(lib
, luid
);
288 * Ask the user and, if confirmed, close the {@link TApplication} this
289 * {@link TWidget} is running on.
291 * This should result in the program terminating.
294 * the {@link TWidget}
296 static public void close(TWidget widget
) {
297 close(widget
.getApplication());
301 * Ask the user and, if confirmed, close the {@link TApplication}.
303 * This should result in the program terminating.
306 * the {@link TApplication}
308 static void close(TApplication app
) {
310 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
311 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {