054fe4a88eb1436314e4f56d21bdb5a526ad09d3
[jvcard.git] / src / be / nikiroo / jvcard / tui / TuiLauncher.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import be.nikiroo.jvcard.tui.panes.FileList;
7
8 import com.googlecode.lanterna.TerminalSize;
9 import com.googlecode.lanterna.TextColor;
10 import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
11 import com.googlecode.lanterna.gui2.Window;
12 import com.googlecode.lanterna.screen.Screen;
13 import com.googlecode.lanterna.screen.TerminalScreen;
14 import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
15 import com.googlecode.lanterna.terminal.ResizeListener;
16 import com.googlecode.lanterna.terminal.Terminal;
17
18 /**
19 * Starting the TUI.
20 *
21 * @author niki
22 *
23 */
24 public class TuiLauncher {
25 static private Screen screen = null;
26
27 /**
28 * Start the TUI program.
29 *
30 * @param textMode
31 * TRUE to force text mode, FALSE to force the Swing terminal
32 * emulator, null to automatically determine the best choice
33 * @param files
34 * the files to show at startup
35 *
36 * @throws IOException
37 * in case of IO error
38 */
39 static public void start(Boolean textMode, List<String> files)
40 throws IOException {
41 Window win = new MainWindow(new FileList(files));
42 TuiLauncher.start(textMode, win);
43 }
44
45 /**
46 * Return the used {@link Screen}.
47 *
48 * @return the {@link Screen}
49 */
50 static public Screen getScreen() {
51 return screen;
52 }
53
54 /**
55 * Start the TUI program.
56 *
57 * @param textMode
58 * TRUE to force text mode, FALSE to force the Swing terminal
59 * emulator, null to automatically determine the best choice
60 * @param win
61 * the window to show at start
62 *
63 * @throws IOException
64 * in case of IO error
65 */
66 static public void start(Boolean textMode, Window win) throws IOException {
67 Terminal terminal = null;
68
69 DefaultTerminalFactory factory = new DefaultTerminalFactory();
70 if (textMode == null) {
71 terminal = factory.createTerminal();
72 } else if (textMode) {
73 factory.setForceTextTerminal(true);
74 terminal = factory.createTerminal();
75 } else {
76 terminal = factory.createTerminalEmulator();
77 }
78
79 if (win instanceof MainWindow) {
80 final MainWindow mwin = (MainWindow) win;
81 mwin.refresh(terminal.getTerminalSize());
82 terminal.addResizeListener(new ResizeListener() {
83 @Override
84 public void onResized(Terminal terminal, TerminalSize newSize) {
85 mwin.refresh(newSize);
86 }
87 });
88 }
89
90 screen = new TerminalScreen(terminal);
91 screen.startScreen();
92
93 // Create gui and start gui
94 MultiWindowTextGUI gui = new MultiWindowTextGUI(screen,
95 TextColor.ANSI.BLUE);
96
97 gui.setTheme(UiColors.getCustomTheme());
98
99 gui.addWindowAndWait(win);
100 screen.stopScreen();
101 }
102 }