Code cleanup 2 (a third one is pending)
[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.fanfix.library.LocalLibrary;
14 import be.nikiroo.utils.Progress;
15
16 /**
17 * Manages the TUI reader, links and manages the {@link TWindow}s, starts the
18 * correct output mode.
19 *
20 * @author niki
21 */
22 class TuiReaderApplication extends TApplication implements Reader {
23 private Reader reader;
24
25 public TuiReaderApplication(MetaData meta, int chapter, Reader reader,
26 BackendType backend) throws Exception {
27 this(reader, backend);
28
29 new TuiReaderMainWindow(this, meta, chapter);
30 }
31
32 public TuiReaderApplication(List<MetaData> stories, Reader reader,
33 TApplication.BackendType backend) throws Exception {
34 this(reader, backend);
35
36 new TuiReaderMainWindow(this, stories);
37 }
38
39 private TuiReaderApplication(Reader reader, TApplication.BackendType backend)
40 throws Exception {
41 super(backend);
42
43 this.reader = reader;
44
45 // Add the menus
46 addFileMenu();
47 addEditMenu();
48 addWindowMenu();
49 addHelpMenu();
50
51 getBackend().setTitle("Fanfix");
52 }
53
54 public void read() throws IOException {
55 reader.read();
56 }
57
58 public void read(int chapter) throws IOException {
59 reader.read(chapter);
60 }
61
62 public void open(MetaData meta) {
63 open(meta, -1);
64 }
65
66 public void open(MetaData meta, int chapter) {
67 // TODO: open in editor + external option
68 if (!meta.isImageDocument()) {
69 new TuiReaderStoryWindow(this, getLibrary(), meta, chapter);
70 } else {
71 try {
72 BasicReader.openExternal(getLibrary(), meta.getLuid());
73 } catch (IOException e) {
74 messageBox("Error when trying to open the story",
75 e.getMessage(), TMessageBox.Type.OK);
76 }
77 }
78 }
79
80 public Story getStory() {
81 return reader.getStory();
82 }
83
84 public BasicLibrary getLibrary() {
85 return reader.getLibrary();
86 }
87
88 public void setLibrary(LocalLibrary lib) {
89 reader.setLibrary(lib);
90 }
91
92 public void setStory(String luid, Progress pg) throws IOException {
93 reader.setStory(luid, pg);
94 }
95
96 public void setStory(URL source, Progress pg) throws IOException {
97 reader.setStory(source, pg);
98 }
99
100 public void browse(String source) {
101 reader.browse(source);
102 }
103 }