Commit | Line | Data |
---|---|---|
955c55b7 | 1 | import java.util.ArrayList; |
2bc32111 KL |
2 | import jexer.TAction; |
3 | import jexer.TApplication; | |
4 | import jexer.TDesktop; | |
469c2b3c | 5 | import jexer.TPanel; |
2bc32111 | 6 | import jexer.TTerminalWidget; |
469c2b3c | 7 | import jexer.TSplitPane; |
2bc32111 KL |
8 | import jexer.TWidget; |
9 | import jexer.event.TKeypressEvent; | |
10 | import jexer.event.TMenuEvent; | |
2bc32111 KL |
11 | import jexer.menu.TMenu; |
12 | ||
13 | /** | |
14 | * Implements a simple tiling window manager. A terminal widget is added to | |
15 | * the desktop, which can be split horizontally or vertically. A close | |
16 | * action is provided to each window to remove the split when its shell | |
17 | * exits. | |
18 | * | |
19 | * This example shows what can be done with minimal changes to stock Jexer | |
20 | * widgets. | |
21 | */ | |
22 | public class JexerTilingWindowManager2 extends TApplication { | |
23 | ||
24 | /** | |
25 | * Menu item: split the terminal vertically. | |
26 | */ | |
27 | private static final int MENU_SPLIT_VERTICAL = 2000; | |
28 | ||
29 | /** | |
30 | * Menu item: split the terminal horizontally. | |
31 | */ | |
32 | private static final int MENU_SPLIT_HORIZONTAL = 2001; | |
955c55b7 KL |
33 | /** |
34 | * Menu item: recreate the root terminal. | |
35 | */ | |
36 | private static final int MENU_RESPAWN_ROOT = 2002; | |
2bc32111 KL |
37 | |
38 | /** | |
39 | * Handle to the root widget. | |
40 | */ | |
41 | private TWidget root = null; | |
469c2b3c | 42 | |
2bc32111 KL |
43 | /** |
44 | * Main entry point. | |
45 | */ | |
46 | public static void main(String [] args) throws Exception { | |
47 | // For this application, we must use ptypipe so that the terminal | |
48 | // shells can be aware of their size. | |
49 | System.setProperty("jexer.TTerminal.ptypipe", "true"); | |
50 | ||
51 | // Let's also suppress the status line. | |
52 | System.setProperty("jexer.hideStatusBar", "true"); | |
53 | ||
54 | JexerTilingWindowManager2 jtwm = new JexerTilingWindowManager2(); | |
55 | (new Thread(jtwm)).start(); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Public constructor chooses the ECMA-48 / Xterm backend. | |
60 | */ | |
61 | public JexerTilingWindowManager2() throws Exception { | |
9ad2ce4f | 62 | super(BackendType.XTERM); |
2bc32111 KL |
63 | |
64 | // The stock tool menu has items for redrawing the screen, opening | |
65 | // images, and (when using the Swing backend) setting the font. | |
66 | addToolMenu(); | |
67 | ||
68 | // We will have one menu containing a mix of new and stock commands | |
69 | TMenu tileMenu = addMenu("&Tile"); | |
70 | ||
71 | // New commands for this example: split vertical and horizontal. | |
72 | tileMenu.addItem(MENU_SPLIT_VERTICAL, "&Vertical Split"); | |
73 | tileMenu.addItem(MENU_SPLIT_HORIZONTAL, "&Horizontal Split"); | |
955c55b7 | 74 | tileMenu.addItem(MENU_RESPAWN_ROOT, "&Respawn Root Terminal"); |
2bc32111 | 75 | |
9ad2ce4f KL |
76 | // Stock commands: a new shell with resizable window, and exit |
77 | // program. | |
2bc32111 | 78 | tileMenu.addSeparator(); |
9ad2ce4f | 79 | tileMenu.addItem(TMenu.MID_SHELL, "&New Windowed Terminal"); |
2bc32111 KL |
80 | tileMenu.addSeparator(); |
81 | tileMenu.addDefaultItem(TMenu.MID_EXIT); | |
82 | ||
9ad2ce4f KL |
83 | // TTerminalWidget can request the text-block mouse pointer be |
84 | // suppressed, but the default TDesktop will ignore it. Let's set a | |
85 | // new TDesktop to pass that mouse pointer visibility option to | |
86 | // TApplication. | |
87 | setDesktop(new TDesktop(this) { | |
88 | @Override | |
89 | public boolean hasHiddenMouse() { | |
90 | TWidget active = getActiveChild(); | |
91 | if (active instanceof TTerminalWidget) { | |
92 | return ((TTerminalWidget) active).hasHiddenMouse(); | |
93 | } | |
94 | return false; | |
95 | } | |
96 | }); | |
97 | ||
2bc32111 | 98 | // Spin up the root terminal |
469c2b3c | 99 | createRootTerminal(); |
2bc32111 KL |
100 | } |
101 | ||
102 | /** | |
103 | * Process menu events. | |
104 | */ | |
105 | @Override | |
106 | protected boolean onMenu(TMenuEvent event) { | |
955c55b7 KL |
107 | TWidget active = getDesktop().getActiveChild(); |
108 | TSplitPane split = null; | |
109 | ||
110 | switch (event.getId()) { | |
111 | case MENU_RESPAWN_ROOT: | |
112 | assert (root == null); | |
113 | createRootTerminal(); | |
114 | return true; | |
115 | ||
116 | case MENU_SPLIT_VERTICAL: | |
469c2b3c | 117 | if (root == null) { |
90d87fca | 118 | assert (getDesktop().getActiveChild() == null); |
469c2b3c KL |
119 | createRootTerminal(); |
120 | return true; | |
121 | } | |
955c55b7 | 122 | split = active.splitVertical(false, createTerminal()); |
469c2b3c KL |
123 | if (active == root) { |
124 | root = split; | |
125 | } | |
2bc32111 | 126 | return true; |
955c55b7 KL |
127 | |
128 | case MENU_SPLIT_HORIZONTAL: | |
469c2b3c | 129 | if (root == null) { |
90d87fca | 130 | assert (getDesktop().getActiveChild() == null); |
469c2b3c KL |
131 | createRootTerminal(); |
132 | return true; | |
133 | } | |
955c55b7 | 134 | split = active.splitHorizontal(false, createTerminal()); |
90d87fca KL |
135 | if (active == root) { |
136 | root = split; | |
137 | } | |
2bc32111 | 138 | return true; |
955c55b7 KL |
139 | |
140 | default: | |
141 | return super.onMenu(event); | |
2bc32111 KL |
142 | } |
143 | ||
2bc32111 KL |
144 | } |
145 | ||
146 | /** | |
469c2b3c | 147 | * Create the root terminal. |
2bc32111 | 148 | */ |
469c2b3c KL |
149 | private void createRootTerminal() { |
150 | assert (root == null); | |
955c55b7 KL |
151 | disableMenuItem(MENU_RESPAWN_ROOT); |
152 | root = createTerminal(); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Create a new terminal. | |
157 | * | |
158 | * @return the new terminal | |
159 | */ | |
160 | private TWidget createTerminal() { | |
161 | return new TTerminalWidget(getDesktop(), 0, 0, | |
469c2b3c KL |
162 | getDesktop().getWidth(), getDesktop().getHeight(), |
163 | new TAction() { | |
164 | public void DO() { | |
a524aa2e | 165 | if (source.getParent() instanceof TSplitPane) { |
955c55b7 KL |
166 | ((TSplitPane) source.getParent()).removeSplit(source, |
167 | true); | |
168 | } else { | |
169 | source.getApplication().enableMenuItem( | |
170 | MENU_RESPAWN_ROOT); | |
171 | source.remove(); | |
469c2b3c KL |
172 | root = null; |
173 | } | |
174 | } | |
175 | }); | |
2bc32111 KL |
176 | } |
177 | ||
178 | } |