FimfictionApi: improve bbcode description handling
[nikiroo-utils.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
211f7ddb
NR
49 @SuppressWarnings("unused")
50 @Override
6322ab64 51 public void read() throws IOException {
bc2ea776 52 MetaData meta = getMeta();
c1873e56 53
bc2ea776
NR
54 if (meta == null) {
55 throw new IOException("No story to read");
56 }
6322ab64 57
c1873e56 58 // TODO: open in editor + external option
b0e88ebd 59 if (!meta.isImageDocument()) {
bc2ea776 60 new TuiReaderStoryWindow(this, getLibrary(), meta, getChapter());
b0e88ebd
NR
61 } else {
62 try {
6322ab64 63 BasicReader.openExternal(getLibrary(), meta.getLuid());
b0e88ebd 64 } catch (IOException e) {
c1873e56 65 messageBox("Error when trying to open the story",
b0e88ebd 66 e.getMessage(), TMessageBox.Type.OK);
c1873e56 67 }
c1873e56
NR
68 }
69 }
6322ab64 70
211f7ddb 71 @Override
bc2ea776
NR
72 public MetaData getMeta() {
73 return reader.getMeta();
74 }
75
211f7ddb 76 @Override
bc2ea776
NR
77 public Story getStory(Progress pg) {
78 return reader.getStory(pg);
6322ab64
NR
79 }
80
211f7ddb 81 @Override
6322ab64
NR
82 public BasicLibrary getLibrary() {
83 return reader.getLibrary();
84 }
85
211f7ddb 86 @Override
bc2ea776 87 public void setLibrary(BasicLibrary lib) {
6322ab64
NR
88 reader.setLibrary(lib);
89 }
90
211f7ddb 91 @Override
bc2ea776
NR
92 public void setMeta(MetaData meta) throws IOException {
93 reader.setMeta(meta);
94 }
95
211f7ddb 96 @Override
bc2ea776
NR
97 public void setMeta(String luid) throws IOException {
98 reader.setMeta(luid);
6322ab64
NR
99 }
100
211f7ddb 101 @Override
bc2ea776
NR
102 public void setMeta(URL source, Progress pg) throws IOException {
103 reader.setMeta(source, pg);
6322ab64
NR
104 }
105
211f7ddb 106 @Override
6322ab64
NR
107 public void browse(String source) {
108 reader.browse(source);
109 }
bc2ea776 110
211f7ddb 111 @Override
bc2ea776
NR
112 public int getChapter() {
113 return reader.getChapter();
114 }
115
211f7ddb 116 @Override
bc2ea776
NR
117 public void setChapter(int chapter) {
118 reader.setChapter(chapter);
119 }
120
121 private void init(Reader reader) {
122 this.reader = reader;
123
124 // Add the menus
125 addFileMenu();
126 addEditMenu();
127 addWindowMenu();
128 addHelpMenu();
129
130 getBackend().setTitle("Fanfix");
131 }
c1873e56 132}