Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
cc99cba8 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
cc99cba8 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
cc99cba8 | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
cc99cba8 | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
cc99cba8 | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
cc99cba8 KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
a69ed767 | 31 | import java.util.Arrays; |
cc99cba8 KL |
32 | import java.util.LinkedList; |
33 | import java.util.List; | |
34 | ||
35 | import jexer.bits.CellAttributes; | |
36 | import jexer.event.TKeypressEvent; | |
37 | import jexer.event.TMouseEvent; | |
615a0d99 KL |
38 | import static jexer.TKeypress.kbDown; |
39 | import static jexer.TKeypress.kbEnd; | |
40 | import static jexer.TKeypress.kbHome; | |
41 | import static jexer.TKeypress.kbLeft; | |
42 | import static jexer.TKeypress.kbPgDn; | |
43 | import static jexer.TKeypress.kbPgUp; | |
44 | import static jexer.TKeypress.kbRight; | |
45 | import static jexer.TKeypress.kbUp; | |
cc99cba8 KL |
46 | |
47 | /** | |
7c870d89 KL |
48 | * TText implements a simple scrollable text area. It reflows automatically on |
49 | * resize. | |
cc99cba8 | 50 | */ |
051e2913 | 51 | public class TText extends TScrollableWidget { |
cc99cba8 | 52 | |
615a0d99 KL |
53 | // ------------------------------------------------------------------------ |
54 | // Constants -------------------------------------------------------------- | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
7657ad8c KL |
57 | /** |
58 | * Available text justifications. | |
59 | */ | |
60 | public enum Justification { | |
a69ed767 KL |
61 | |
62 | /** | |
63 | * Not justified at all, use spacing as provided by the client. | |
64 | */ | |
65 | NONE, | |
66 | ||
7657ad8c KL |
67 | /** |
68 | * Left-justified text. | |
69 | */ | |
70 | LEFT, | |
71 | ||
72 | /** | |
73 | * Centered text. | |
74 | */ | |
75 | CENTER, | |
76 | ||
77 | /** | |
78 | * Right-justified text. | |
79 | */ | |
80 | RIGHT, | |
81 | ||
82 | /** | |
83 | * Fully-justified text. | |
84 | */ | |
85 | FULL, | |
86 | } | |
87 | ||
615a0d99 KL |
88 | // ------------------------------------------------------------------------ |
89 | // Variables -------------------------------------------------------------- | |
90 | // ------------------------------------------------------------------------ | |
91 | ||
7657ad8c KL |
92 | /** |
93 | * How to justify the text. | |
94 | */ | |
95 | private Justification justification = Justification.LEFT; | |
96 | ||
cc99cba8 KL |
97 | /** |
98 | * Text to display. | |
99 | */ | |
100 | private String text; | |
101 | ||
102 | /** | |
103 | * Text converted to lines. | |
104 | */ | |
105 | private List<String> lines; | |
106 | ||
107 | /** | |
108 | * Text color. | |
109 | */ | |
110 | private String colorKey; | |
111 | ||
112 | /** | |
56661844 | 113 | * Maximum width of a single line. |
cc99cba8 | 114 | */ |
56661844 | 115 | private int maxLineWidth; |
cc99cba8 KL |
116 | |
117 | /** | |
56661844 | 118 | * Number of lines between each paragraph. |
cc99cba8 | 119 | */ |
56661844 | 120 | private int lineSpacing = 1; |
cc99cba8 | 121 | |
615a0d99 KL |
122 | // ------------------------------------------------------------------------ |
123 | // Constructors ----------------------------------------------------------- | |
124 | // ------------------------------------------------------------------------ | |
cc99cba8 KL |
125 | |
126 | /** | |
127 | * Public constructor. | |
128 | * | |
5dfd1c11 KL |
129 | * @param parent parent widget |
130 | * @param text text on the screen | |
131 | * @param x column relative to parent | |
132 | * @param y row relative to parent | |
133 | * @param width width of text area | |
134 | * @param height height of text area | |
cc99cba8 KL |
135 | */ |
136 | public TText(final TWidget parent, final String text, final int x, | |
7c870d89 | 137 | final int y, final int width, final int height) { |
cc99cba8 KL |
138 | |
139 | this(parent, text, x, y, width, height, "ttext"); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Public constructor. | |
144 | * | |
5dfd1c11 KL |
145 | * @param parent parent widget |
146 | * @param text text on the screen | |
147 | * @param x column relative to parent | |
148 | * @param y row relative to parent | |
149 | * @param width width of text area | |
150 | * @param height height of text area | |
151 | * @param colorKey ColorTheme key color to use for foreground | |
152 | * text. Default is "ttext". | |
cc99cba8 KL |
153 | */ |
154 | public TText(final TWidget parent, final String text, final int x, | |
7c870d89 KL |
155 | final int y, final int width, final int height, |
156 | final String colorKey) { | |
cc99cba8 KL |
157 | |
158 | // Set parent and window | |
a83fea2b | 159 | super(parent, x, y, width, height); |
cc99cba8 | 160 | |
cc99cba8 KL |
161 | this.text = text; |
162 | this.colorKey = colorKey; | |
163 | ||
164 | lines = new LinkedList<String>(); | |
165 | ||
56661844 KL |
166 | vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); |
167 | hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); | |
168 | reflowData(); | |
cc99cba8 KL |
169 | } |
170 | ||
615a0d99 KL |
171 | // ------------------------------------------------------------------------ |
172 | // TScrollableWidget ------------------------------------------------------ | |
173 | // ------------------------------------------------------------------------ | |
174 | ||
cc99cba8 KL |
175 | /** |
176 | * Draw the text box. | |
177 | */ | |
178 | @Override | |
179 | public void draw() { | |
180 | // Setup my color | |
181 | CellAttributes color = getTheme().getColor(colorKey); | |
182 | ||
183 | int begin = vScroller.getValue(); | |
184 | int topY = 0; | |
185 | for (int i = begin; i < lines.size(); i++) { | |
186 | String line = lines.get(i); | |
187 | if (hScroller.getValue() < line.length()) { | |
188 | line = line.substring(hScroller.getValue()); | |
189 | } else { | |
190 | line = ""; | |
191 | } | |
192 | String formatString = "%-" + Integer.toString(getWidth() - 1) + "s"; | |
a69ed767 | 193 | putStringXY(0, topY, String.format(formatString, line), color); |
cc99cba8 KL |
194 | topY++; |
195 | ||
7c870d89 | 196 | if (topY >= (getHeight() - 1)) { |
cc99cba8 KL |
197 | break; |
198 | } | |
199 | } | |
200 | ||
201 | // Pad the rest with blank lines | |
7c870d89 | 202 | for (int i = topY; i < (getHeight() - 1); i++) { |
a69ed767 | 203 | hLineXY(0, i, getWidth() - 1, ' ', color); |
cc99cba8 KL |
204 | } |
205 | ||
206 | } | |
207 | ||
208 | /** | |
209 | * Handle mouse press events. | |
210 | * | |
5dfd1c11 | 211 | * @param mouse mouse button press event |
cc99cba8 KL |
212 | */ |
213 | @Override | |
214 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 215 | if (mouse.isMouseWheelUp()) { |
cc99cba8 KL |
216 | vScroller.decrement(); |
217 | return; | |
218 | } | |
7c870d89 | 219 | if (mouse.isMouseWheelDown()) { |
cc99cba8 KL |
220 | vScroller.increment(); |
221 | return; | |
222 | } | |
223 | ||
224 | // Pass to children | |
225 | super.onMouseDown(mouse); | |
226 | } | |
227 | ||
228 | /** | |
229 | * Handle keystrokes. | |
230 | * | |
5dfd1c11 | 231 | * @param keypress keystroke event |
cc99cba8 KL |
232 | */ |
233 | @Override | |
234 | public void onKeypress(final TKeypressEvent keypress) { | |
235 | if (keypress.equals(kbLeft)) { | |
236 | hScroller.decrement(); | |
237 | } else if (keypress.equals(kbRight)) { | |
238 | hScroller.increment(); | |
239 | } else if (keypress.equals(kbUp)) { | |
240 | vScroller.decrement(); | |
241 | } else if (keypress.equals(kbDown)) { | |
242 | vScroller.increment(); | |
243 | } else if (keypress.equals(kbPgUp)) { | |
244 | vScroller.bigDecrement(); | |
245 | } else if (keypress.equals(kbPgDn)) { | |
246 | vScroller.bigIncrement(); | |
247 | } else if (keypress.equals(kbHome)) { | |
248 | vScroller.toTop(); | |
249 | } else if (keypress.equals(kbEnd)) { | |
250 | vScroller.toBottom(); | |
251 | } else { | |
252 | // Pass other keys (tab etc.) on | |
253 | super.onKeypress(keypress); | |
254 | } | |
255 | } | |
256 | ||
615a0d99 KL |
257 | /** |
258 | * Resize text and scrollbars for a new width/height. | |
259 | */ | |
260 | @Override | |
261 | public void reflowData() { | |
262 | // Reset the lines | |
263 | lines.clear(); | |
264 | ||
265 | // Break up text into paragraphs | |
266 | String[] paragraphs = text.split("\n\n"); | |
267 | for (String p : paragraphs) { | |
268 | switch (justification) { | |
a69ed767 KL |
269 | case NONE: |
270 | lines.addAll(Arrays.asList(p.split("\n"))); | |
271 | break; | |
615a0d99 KL |
272 | case LEFT: |
273 | lines.addAll(jexer.bits.StringUtils.left(p, | |
274 | getWidth() - 1)); | |
275 | break; | |
276 | case CENTER: | |
277 | lines.addAll(jexer.bits.StringUtils.center(p, | |
278 | getWidth() - 1)); | |
279 | break; | |
280 | case RIGHT: | |
281 | lines.addAll(jexer.bits.StringUtils.right(p, | |
282 | getWidth() - 1)); | |
283 | break; | |
284 | case FULL: | |
285 | lines.addAll(jexer.bits.StringUtils.full(p, | |
286 | getWidth() - 1)); | |
287 | break; | |
288 | } | |
289 | ||
290 | for (int i = 0; i < lineSpacing; i++) { | |
291 | lines.add(""); | |
292 | } | |
293 | } | |
294 | computeBounds(); | |
295 | } | |
296 | ||
297 | // ------------------------------------------------------------------------ | |
298 | // TText ------------------------------------------------------------------ | |
299 | // ------------------------------------------------------------------------ | |
300 | ||
301 | /** | |
302 | * Set the text. | |
303 | * | |
304 | * @param text new text to display | |
305 | */ | |
306 | public void setText(final String text) { | |
307 | this.text = text; | |
308 | reflowData(); | |
309 | } | |
310 | ||
311 | /** | |
312 | * Get the text. | |
313 | * | |
314 | * @return the text | |
315 | */ | |
316 | public String getText() { | |
317 | return text; | |
318 | } | |
319 | ||
320 | /** | |
321 | * Convenience method used by TWindowLoggerOutput. | |
322 | * | |
323 | * @param line new line to add | |
324 | */ | |
325 | public void addLine(final String line) { | |
326 | if (text.length() == 0) { | |
327 | text = line; | |
328 | } else { | |
329 | text += "\n\n"; | |
330 | text += line; | |
331 | } | |
332 | reflowData(); | |
333 | } | |
334 | ||
335 | /** | |
336 | * Recompute the bounds for the scrollbars. | |
337 | */ | |
338 | private void computeBounds() { | |
339 | maxLineWidth = 0; | |
340 | for (String line : lines) { | |
341 | if (line.length() > maxLineWidth) { | |
342 | maxLineWidth = line.length(); | |
343 | } | |
344 | } | |
345 | ||
346 | vScroller.setTopValue(0); | |
347 | vScroller.setBottomValue((lines.size() - getHeight()) + 1); | |
348 | if (vScroller.getBottomValue() < 0) { | |
349 | vScroller.setBottomValue(0); | |
350 | } | |
351 | if (vScroller.getValue() > vScroller.getBottomValue()) { | |
352 | vScroller.setValue(vScroller.getBottomValue()); | |
353 | } | |
354 | ||
355 | hScroller.setLeftValue(0); | |
356 | hScroller.setRightValue((maxLineWidth - getWidth()) + 1); | |
357 | if (hScroller.getRightValue() < 0) { | |
358 | hScroller.setRightValue(0); | |
359 | } | |
360 | if (hScroller.getValue() > hScroller.getRightValue()) { | |
361 | hScroller.setValue(hScroller.getRightValue()); | |
362 | } | |
363 | } | |
364 | ||
365 | /** | |
366 | * Set justification. | |
367 | * | |
368 | * @param justification LEFT, CENTER, RIGHT, or FULL | |
369 | */ | |
370 | public void setJustification(final Justification justification) { | |
371 | this.justification = justification; | |
372 | reflowData(); | |
373 | } | |
374 | ||
375 | /** | |
376 | * Left-justify the text. | |
377 | */ | |
378 | public void leftJustify() { | |
379 | justification = Justification.LEFT; | |
380 | reflowData(); | |
381 | } | |
382 | ||
383 | /** | |
384 | * Center-justify the text. | |
385 | */ | |
386 | public void centerJustify() { | |
387 | justification = Justification.CENTER; | |
388 | reflowData(); | |
389 | } | |
390 | ||
391 | /** | |
392 | * Right-justify the text. | |
393 | */ | |
394 | public void rightJustify() { | |
395 | justification = Justification.RIGHT; | |
396 | reflowData(); | |
397 | } | |
398 | ||
399 | /** | |
400 | * Fully-justify the text. | |
401 | */ | |
402 | public void fullJustify() { | |
403 | justification = Justification.FULL; | |
404 | reflowData(); | |
405 | } | |
406 | ||
cc99cba8 | 407 | } |