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