c1bba5a8220b49d0dfacdef1cca777a2e31ac5e8
1 package be
.nikiroo
.fanfix
.reader
;
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
.utils
.Progress
;
23 * Manages the TUI general mode and links and manages the {@link TWindow}s.
25 * It will also enclose a {@link Reader} and simply handle the reading part
26 * differently (it will create the required sub-windows and display them).
30 class TuiReaderApplication
extends TApplication
implements Reader
{
31 public static final int MENU_OPEN
= 1025;
32 public static final int MENU_IMPORT_URL
= 1026;
33 public static final int MENU_IMPORT_FILE
= 1027;
34 public static final int MENU_EXPORT
= 1028;
35 public static final int MENU_EXIT
= 1029;
37 private Reader reader
;
38 private TuiReaderMainWindow main
;
40 // start reading if meta present
41 public TuiReaderApplication(Reader reader
, BackendType backend
)
46 MetaData meta
= getMeta();
48 main
= new TuiReaderMainWindow(this);
55 public TuiReaderApplication(Reader reader
, String source
,
56 TApplication
.BackendType backend
) throws Exception
{
61 main
= new TuiReaderMainWindow(this);
62 main
.setSource(source
);
66 public void read() throws IOException
{
67 MetaData meta
= getMeta();
70 throw new IOException("No story to read");
73 // TODO: open in editor + external option
74 if (!meta
.isImageDocument()) {
75 @SuppressWarnings("unused")
76 Object discard
= new TuiReaderStoryWindow(this, getLibrary(), meta
,
80 BasicReader
.openExternal(getLibrary(), meta
.getLuid());
81 } catch (IOException e
) {
82 messageBox("Error when trying to open the story",
83 e
.getMessage(), TMessageBox
.Type
.OK
);
89 public MetaData
getMeta() {
90 return reader
.getMeta();
94 public Story
getStory(Progress pg
) {
95 return reader
.getStory(pg
);
99 public BasicLibrary
getLibrary() {
100 return reader
.getLibrary();
104 public void setLibrary(BasicLibrary lib
) {
105 reader
.setLibrary(lib
);
109 public void setMeta(MetaData meta
) throws IOException
{
110 reader
.setMeta(meta
);
114 public void setMeta(String luid
) throws IOException
{
115 reader
.setMeta(luid
);
119 public void setMeta(URL source
, Progress pg
) throws IOException
{
120 reader
.setMeta(source
, pg
);
124 public void browse(String source
) {
125 reader
.browse(source
);
129 public int getChapter() {
130 return reader
.getChapter();
134 public void setChapter(int chapter
) {
135 reader
.setChapter(chapter
);
138 private void init(Reader reader
) {
139 this.reader
= reader
;
141 // TODO: traces/errors?
142 Instance
.setTraceHandler(null);
144 // Add the menus TODO: i18n
145 TMenu fileMenu
= addMenu("&File");
146 fileMenu
.addItem(MENU_OPEN
, "&Open");
147 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
149 fileMenu
.addSeparator();
150 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
151 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
152 fileMenu
.addSeparator();
153 fileMenu
.addItem(MENU_EXIT
, "E&xit");
155 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management "
156 + "commands (Open, Save, Print, etc.)");
157 // TODO: doesn't actually work:
158 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, TCommand
.cmExit
, "Exit");
160 // TODO: Edit: re-download, delete
166 getBackend().setTitle("Fanfix");
170 protected boolean onMenu(TMenuEvent menu
) {
172 switch (menu
.getId()) {
174 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
175 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
180 case MENU_IMPORT_URL
:
181 String clipboard
= "";
183 clipboard
= ("" + Toolkit
.getDefaultToolkit()
184 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
186 } catch (Exception e
) {
187 // No data will be handled
190 if (clipboard
== null || !clipboard
.startsWith("http")) {
194 String url
= inputBox("Import story", "URL to import", clipboard
)
202 case MENU_IMPORT_FILE
:
204 String filename
= fileOpenBox(".");
205 if (!imprt(filename
)) {
208 } catch (IOException e
) {
216 return super.onMenu(menu
);
219 private boolean imprt(String url
) {
221 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
222 main
.refreshStories();
224 } catch (IOException e
) {