Code cleanup 3 and update jexer-niki :
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderApplication.java
CommitLineData
c1873e56
NR
1package be.nikiroo.fanfix.reader;
2
3import java.io.IOException;
6322ab64 4import java.net.URL;
c1873e56
NR
5import java.util.List;
6
7import jexer.TApplication;
8import jexer.TMessageBox;
6322ab64 9import jexer.TWindow;
c1873e56 10import be.nikiroo.fanfix.data.MetaData;
6322ab64
NR
11import be.nikiroo.fanfix.data.Story;
12import be.nikiroo.fanfix.library.BasicLibrary;
6322ab64 13import be.nikiroo.utils.Progress;
c1873e56 14
6322ab64 15/**
bc2ea776
NR
16 * Manages the TUI general mode and links and manages the {@link TWindow}s.
17 * <p>
18 * It will also enclose a {@link Reader} and simply handle the reading part
19 * differently (it will create the required sub-windows and display them).
6322ab64
NR
20 *
21 * @author niki
22 */
23class TuiReaderApplication extends TApplication implements Reader {
24 private Reader reader;
c1873e56 25
bc2ea776
NR
26 // start reading if meta present
27 public TuiReaderApplication(Reader reader, BackendType backend)
28 throws Exception {
29 super(backend);
30 init(reader);
c1873e56 31
bc2ea776 32 MetaData meta = getMeta();
c1873e56 33
bc2ea776 34 new TuiReaderMainWindow(this).setMeta(meta);
6322ab64 35
bc2ea776
NR
36 if (meta != null) {
37 read();
38 }
6322ab64
NR
39 }
40
bc2ea776
NR
41 public TuiReaderApplication(List<MetaData> stories, Reader reader,
42 TApplication.BackendType backend) throws Exception {
c1873e56 43 super(backend);
bc2ea776 44 init(reader);
c1873e56 45
bc2ea776 46 new TuiReaderMainWindow(this).setMetas(stories);
6322ab64 47 }
c1873e56 48
6322ab64 49 public void read() throws IOException {
bc2ea776 50 MetaData meta = getMeta();
c1873e56 51
bc2ea776
NR
52 if (meta == null) {
53 throw new IOException("No story to read");
54 }
6322ab64 55
c1873e56 56 // TODO: open in editor + external option
b0e88ebd 57 if (!meta.isImageDocument()) {
bc2ea776 58 new TuiReaderStoryWindow(this, getLibrary(), meta, getChapter());
b0e88ebd
NR
59 } else {
60 try {
6322ab64 61 BasicReader.openExternal(getLibrary(), meta.getLuid());
b0e88ebd 62 } catch (IOException e) {
c1873e56 63 messageBox("Error when trying to open the story",
b0e88ebd 64 e.getMessage(), TMessageBox.Type.OK);
c1873e56 65 }
c1873e56
NR
66 }
67 }
6322ab64 68
bc2ea776
NR
69 public MetaData getMeta() {
70 return reader.getMeta();
71 }
72
73 public Story getStory(Progress pg) {
74 return reader.getStory(pg);
6322ab64
NR
75 }
76
77 public BasicLibrary getLibrary() {
78 return reader.getLibrary();
79 }
80
bc2ea776 81 public void setLibrary(BasicLibrary lib) {
6322ab64
NR
82 reader.setLibrary(lib);
83 }
84
bc2ea776
NR
85 public void setMeta(MetaData meta) throws IOException {
86 reader.setMeta(meta);
87 }
88
89 public void setMeta(String luid) throws IOException {
90 reader.setMeta(luid);
6322ab64
NR
91 }
92
bc2ea776
NR
93 public void setMeta(URL source, Progress pg) throws IOException {
94 reader.setMeta(source, pg);
6322ab64
NR
95 }
96
97 public void browse(String source) {
98 reader.browse(source);
99 }
bc2ea776
NR
100
101 public int getChapter() {
102 return reader.getChapter();
103 }
104
105 public void setChapter(int chapter) {
106 reader.setChapter(chapter);
107 }
108
109 private void init(Reader reader) {
110 this.reader = reader;
111
112 // Add the menus
113 addFileMenu();
114 addEditMenu();
115 addWindowMenu();
116 addHelpMenu();
117
118 getBackend().setTitle("Fanfix");
119 }
c1873e56 120}