Code cleanup 3 and update jexer-niki :
[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.data.MetaData;
11 import be.nikiroo.fanfix.data.Story;
12 import be.nikiroo.fanfix.library.BasicLibrary;
13 import be.nikiroo.utils.Progress;
14
15 /**
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).
20 *
21 * @author niki
22 */
23 class TuiReaderApplication extends TApplication implements Reader {
24 private Reader reader;
25
26 // start reading if meta present
27 public TuiReaderApplication(Reader reader, BackendType backend)
28 throws Exception {
29 super(backend);
30 init(reader);
31
32 MetaData meta = getMeta();
33
34 new TuiReaderMainWindow(this).setMeta(meta);
35
36 if (meta != null) {
37 read();
38 }
39 }
40
41 public TuiReaderApplication(List<MetaData> stories, Reader reader,
42 TApplication.BackendType backend) throws Exception {
43 super(backend);
44 init(reader);
45
46 new TuiReaderMainWindow(this).setMetas(stories);
47 }
48
49 public void read() throws IOException {
50 MetaData meta = getMeta();
51
52 if (meta == null) {
53 throw new IOException("No story to read");
54 }
55
56 // TODO: open in editor + external option
57 if (!meta.isImageDocument()) {
58 new TuiReaderStoryWindow(this, getLibrary(), meta, getChapter());
59 } else {
60 try {
61 BasicReader.openExternal(getLibrary(), meta.getLuid());
62 } catch (IOException e) {
63 messageBox("Error when trying to open the story",
64 e.getMessage(), TMessageBox.Type.OK);
65 }
66 }
67 }
68
69 public MetaData getMeta() {
70 return reader.getMeta();
71 }
72
73 public Story getStory(Progress pg) {
74 return reader.getStory(pg);
75 }
76
77 public BasicLibrary getLibrary() {
78 return reader.getLibrary();
79 }
80
81 public void setLibrary(BasicLibrary lib) {
82 reader.setLibrary(lib);
83 }
84
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);
91 }
92
93 public void setMeta(URL source, Progress pg) throws IOException {
94 reader.setMeta(source, pg);
95 }
96
97 public void browse(String source) {
98 reader.browse(source);
99 }
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 }
120 }