Update support code for Jexer
[jvcard.git] / src / be / nikiroo / jvcard / tui / TuiLauncherJexer.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.List;
5
6 import jexer.TAction;
7 import jexer.TApplication;
8 import be.nikiroo.jvcard.tui.windows.TuiBrowserWindow;
9 import be.nikiroo.jvcard.tui.windows.TuiFileListWindow;
10
11 /**
12 * Starting the TUI.
13 *
14 * @author niki
15 */
16 public class TuiLauncherJexer extends TApplication {
17 /**
18 * Application is in fullscreen mode, no windows.
19 *
20 * TODO: make it an option
21 */
22 static public final boolean FULLSCREEN = true;
23
24 /**
25 * @param textMode
26 * TRUE to force text mode, FALSE to force the Swing terminal
27 * emulator, null to automatically determine the best choice
28 * @param files
29 * the files to show at startup
30 *
31 * @throws UnsupportedEncodingException
32 * if an exception is thrown when creating the InputStreamReader
33 */
34 public TuiLauncherJexer(final Boolean textMode, final List<String> files)
35 throws UnsupportedEncodingException {
36 super(backend(textMode));
37
38 addFileMenu();
39 addWindowMenu();
40
41 int width = getBackend().getScreen().getWidth();
42 int height = getBackend().getScreen().getHeight() - 2;
43
44 if (backend(textMode) == BackendType.SWING) {
45 // TODO: why does the size change after the FIRST window has been
46 // created (SWING mode only?) ?
47 // A problem with the graphical size not an exact number of
48 // cols/lines?
49 width--;
50 height--;
51 }
52
53 width = Math.max(1, width);
54 height = Math.max(1, height);
55
56 TuiBrowserWindow main = new TuiFileListWindow(TuiLauncherJexer.this,
57 width, height, files);
58
59 main.addCloseListener(new TAction() {
60 @Override
61 public void DO() {
62 TuiLauncherJexer.this.exit(false);
63 }
64 });
65 }
66
67 /**
68 * Start the TUI program.
69 */
70 public void start() {
71 (new Thread(this)).start();
72 }
73
74 /**
75 * Select the most appropriate backend.
76 *
77 * @param textMode
78 * NULL for auto-detection
79 * @return the backend type to use
80 */
81 private static BackendType backend(Boolean textMode) {
82 if (textMode == null) {
83 boolean isMsWindows = System.getProperty("os.name", "")
84 .toLowerCase().startsWith("windows");
85 boolean forceSwing = System.getProperty("jexer.Swing", "false")
86 .equals("true");
87 boolean noConsole = System.console() == null;
88 if (isMsWindows || forceSwing || noConsole) {
89 return BackendType.SWING;
90 }
91
92 return BackendType.XTERM;
93 }
94
95 if (textMode) {
96 return BackendType.XTERM;
97 }
98
99 return BackendType.SWING;
100 }
101 }