Code cleanup 2 (a third one is pending)
[fanfix.git] / src / be / nikiroo / fanfix / reader / TuiReader.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import jexer.TApplication;
7 import jexer.TApplication.BackendType;
8 import be.nikiroo.fanfix.Instance;
9 import be.nikiroo.fanfix.data.MetaData;
10
11 /**
12 * This {@link Reader}is based upon the TUI widget library 'jexer'
13 * (https://github.com/klamonte/jexer/) and offer, as its name suggest, a Text
14 * User Interface.
15 * <p>
16 * It is expected to be on par with the GUI version.
17 *
18 * @author niki
19 */
20 class TuiReader extends BasicReader {
21 /**
22 * Will detect the backend to use.
23 * <p>
24 * Swing is the default backend on Windows and MacOS while evreything else
25 * will use XTERM unless explicitly overridden by <tt>jexer.Swing</tt> =
26 * <tt>true</tt> or <tt>false</tt>.
27 *
28 * @return the backend to use
29 */
30 private static BackendType guessBackendType() {
31 // TODO: allow a config option to force one or the other?
32 TApplication.BackendType backendType = TApplication.BackendType.XTERM;
33 if (System.getProperty("os.name").startsWith("Windows")) {
34 backendType = TApplication.BackendType.SWING;
35 }
36
37 if (System.getProperty("os.name").startsWith("Mac")) {
38 backendType = TApplication.BackendType.SWING;
39 }
40
41 if (System.getProperty("jexer.Swing") != null) {
42 if (System.getProperty("jexer.Swing", "false").equals("true")) {
43 backendType = TApplication.BackendType.SWING;
44 } else {
45 backendType = TApplication.BackendType.XTERM;
46 }
47 }
48
49 return backendType;
50 }
51
52 public void read(int chapter) throws IOException {
53 if (getStory() == null) {
54 throw new IOException("No story to read");
55 }
56
57 start(getStory().getMeta(), chapter);
58 }
59
60 public void browse(String source) {
61 start(getLibrary().getListBySource(source));
62 }
63
64 /**
65 * Start the application with the given stories.
66 *
67 * @param metas
68 * the stories to display
69 */
70 private void start(List<MetaData> metas) {
71 try {
72 TuiReaderApplication app = new TuiReaderApplication(metas, this,
73 guessBackendType());
74 new Thread(app).start();
75 } catch (Exception e) {
76 Instance.syserr(e);
77 }
78 }
79
80 /**
81 * Start the application with the given {@link MetaData} at the given open
82 * chapter.
83 *
84 * @param meta
85 * the story to display
86 * @param chapter
87 * the chapter to open
88 */
89 private void start(MetaData meta, int chapter) {
90 try {
91 TuiReaderApplication app = new TuiReaderApplication(meta, chapter,
92 this, guessBackendType());
93 new Thread(app).start();
94 } catch (Exception e) {
95 Instance.syserr(e);
96 }
97 }
98 }