3d6a949528630e4733f945eb19724977788c6859
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReaderApplication.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.List;
6
7 import jexer.TApplication;
8 import jexer.TMessageBox;
9 import jexer.TWindow;
10 import be.nikiroo.fanfix.Instance;
11 import be.nikiroo.fanfix.Instance.SyserrHandler;
12 import be.nikiroo.fanfix.Instance.TraceHandler;
13 import be.nikiroo.fanfix.data.MetaData;
14 import be.nikiroo.fanfix.data.Story;
15 import be.nikiroo.fanfix.library.BasicLibrary;
16 import be.nikiroo.utils.Progress;
17
18 /**
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).
23 *
24 * @author niki
25 */
26 class TuiReaderApplication extends TApplication implements Reader {
27 private Reader reader;
28
29 // start reading if meta present
30 public TuiReaderApplication(Reader reader, BackendType backend)
31 throws Exception {
32 super(backend);
33 init(reader);
34
35 MetaData meta = getMeta();
36
37 new TuiReaderMainWindow(this).setMeta(meta);
38
39 if (meta != null) {
40 read();
41 }
42 }
43
44 public TuiReaderApplication(List<MetaData> stories, Reader reader,
45 TApplication.BackendType backend) throws Exception {
46 super(backend);
47 init(reader);
48
49 new TuiReaderMainWindow(this).setMetas(stories);
50 }
51
52 @SuppressWarnings("unused")
53 @Override
54 public void read() throws IOException {
55 MetaData meta = getMeta();
56
57 if (meta == null) {
58 throw new IOException("No story to read");
59 }
60
61 // TODO: open in editor + external option
62 if (!meta.isImageDocument()) {
63 new TuiReaderStoryWindow(this, getLibrary(), meta, getChapter());
64 } else {
65 try {
66 BasicReader.openExternal(getLibrary(), meta.getLuid());
67 } catch (IOException e) {
68 messageBox("Error when trying to open the story",
69 e.getMessage(), TMessageBox.Type.OK);
70 }
71 }
72 }
73
74 @Override
75 public MetaData getMeta() {
76 return reader.getMeta();
77 }
78
79 @Override
80 public Story getStory(Progress pg) {
81 return reader.getStory(pg);
82 }
83
84 @Override
85 public BasicLibrary getLibrary() {
86 return reader.getLibrary();
87 }
88
89 @Override
90 public void setLibrary(BasicLibrary lib) {
91 reader.setLibrary(lib);
92 }
93
94 @Override
95 public void setMeta(MetaData meta) throws IOException {
96 reader.setMeta(meta);
97 }
98
99 @Override
100 public void setMeta(String luid) throws IOException {
101 reader.setMeta(luid);
102 }
103
104 @Override
105 public void setMeta(URL source, Progress pg) throws IOException {
106 reader.setMeta(source, pg);
107 }
108
109 @Override
110 public void browse(String source) {
111 reader.browse(source);
112 }
113
114 @Override
115 public int getChapter() {
116 return reader.getChapter();
117 }
118
119 @Override
120 public void setChapter(int chapter) {
121 reader.setChapter(chapter);
122 }
123
124 private void init(Reader reader) {
125 this.reader = reader;
126
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
143 // Add the menus
144 addFileMenu();
145 addEditMenu();
146 addWindowMenu();
147 addHelpMenu();
148
149 getBackend().setTitle("Fanfix");
150 }
151 }