Merge branch 'subtree'
[fanfix.git] / src / jexer / TText.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import java.util.Arrays;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import jexer.bits.CellAttributes;
36 import jexer.bits.StringUtils;
37 import jexer.event.TKeypressEvent;
38 import jexer.event.TMouseEvent;
39 import static jexer.TKeypress.kbDown;
40 import static jexer.TKeypress.kbEnd;
41 import static jexer.TKeypress.kbHome;
42 import static jexer.TKeypress.kbLeft;
43 import static jexer.TKeypress.kbPgDn;
44 import static jexer.TKeypress.kbPgUp;
45 import static jexer.TKeypress.kbRight;
46 import static jexer.TKeypress.kbUp;
47
48 /**
49 * TText implements a simple scrollable text area. It reflows automatically on
50 * resize.
51 */
52 public class TText extends TScrollableWidget {
53
54 // ------------------------------------------------------------------------
55 // Constants --------------------------------------------------------------
56 // ------------------------------------------------------------------------
57
58 /**
59 * Available text justifications.
60 */
61 public enum Justification {
62
63 /**
64 * Not justified at all, use spacing as provided by the client.
65 */
66 NONE,
67
68 /**
69 * Left-justified text.
70 */
71 LEFT,
72
73 /**
74 * Centered text.
75 */
76 CENTER,
77
78 /**
79 * Right-justified text.
80 */
81 RIGHT,
82
83 /**
84 * Fully-justified text.
85 */
86 FULL,
87 }
88
89 // ------------------------------------------------------------------------
90 // Variables --------------------------------------------------------------
91 // ------------------------------------------------------------------------
92
93 /**
94 * How to justify the text.
95 */
96 private Justification justification = Justification.LEFT;
97
98 /**
99 * Text to display.
100 */
101 private String text;
102
103 /**
104 * Text converted to lines.
105 */
106 private List<String> lines;
107
108 /**
109 * Text color.
110 */
111 private String colorKey;
112
113 /**
114 * Maximum width of a single line.
115 */
116 private int maxLineWidth;
117
118 /**
119 * Number of lines between each paragraph.
120 */
121 private int lineSpacing = 1;
122
123 // ------------------------------------------------------------------------
124 // Constructors -----------------------------------------------------------
125 // ------------------------------------------------------------------------
126
127 /**
128 * Public constructor.
129 *
130 * @param parent parent widget
131 * @param text text on the screen
132 * @param x column relative to parent
133 * @param y row relative to parent
134 * @param width width of text area
135 * @param height height of text area
136 */
137 public TText(final TWidget parent, final String text, final int x,
138 final int y, final int width, final int height) {
139
140 this(parent, text, x, y, width, height, "ttext");
141 }
142
143 /**
144 * Public constructor.
145 *
146 * @param parent parent widget
147 * @param text text on the screen
148 * @param x column relative to parent
149 * @param y row relative to parent
150 * @param width width of text area
151 * @param height height of text area
152 * @param colorKey ColorTheme key color to use for foreground
153 * text. Default is "ttext".
154 */
155 public TText(final TWidget parent, final String text, final int x,
156 final int y, final int width, final int height,
157 final String colorKey) {
158
159 // Set parent and window
160 super(parent, x, y, width, height);
161
162 this.text = text;
163 this.colorKey = colorKey;
164
165 lines = new ArrayList<String>();
166
167 vScroller = new TVScroller(this, getWidth() - 1, 0,
168 Math.max(1, getHeight() - 1));
169 hScroller = new THScroller(this, 0, getHeight() - 1,
170 Math.max(1, getWidth() - 1));
171 reflowData();
172 }
173
174 // ------------------------------------------------------------------------
175 // TScrollableWidget ------------------------------------------------------
176 // ------------------------------------------------------------------------
177
178 /**
179 * Override TWidget's width: we need to set child widget widths.
180 *
181 * @param width new widget width
182 */
183 @Override
184 public void setWidth(final int width) {
185 super.setWidth(width);
186 if (hScroller != null) {
187 hScroller.setWidth(getWidth() - 1);
188 }
189 if (vScroller != null) {
190 vScroller.setX(getWidth() - 1);
191 }
192 }
193
194 /**
195 * Override TWidget's height: we need to set child widget heights.
196 * time.
197 *
198 * @param height new widget height
199 */
200 @Override
201 public void setHeight(final int height) {
202 super.setHeight(height);
203 if (hScroller != null) {
204 hScroller.setY(getHeight() - 1);
205 }
206 if (vScroller != null) {
207 vScroller.setHeight(getHeight() - 1);
208 }
209 }
210
211 /**
212 * Draw the text box.
213 */
214 @Override
215 public void draw() {
216 // Setup my color
217 CellAttributes color = getTheme().getColor(colorKey);
218
219 int begin = vScroller.getValue();
220 int topY = 0;
221 for (int i = begin; i < lines.size(); i++) {
222 String line = lines.get(i);
223 if (hScroller.getValue() < StringUtils.width(line)) {
224 line = line.substring(hScroller.getValue());
225 } else {
226 line = "";
227 }
228 if (getWidth() > 3) {
229 String formatString = "%-" + Integer.toString(getWidth() - 1) + "s";
230 putStringXY(0, topY, String.format(formatString, line), color);
231 }
232 topY++;
233
234 if (topY >= (getHeight() - 1)) {
235 break;
236 }
237 }
238
239 // Pad the rest with blank lines
240 for (int i = topY; i < (getHeight() - 1); i++) {
241 hLineXY(0, i, getWidth() - 1, ' ', color);
242 }
243
244 }
245
246 /**
247 * Handle mouse press events.
248 *
249 * @param mouse mouse button press event
250 */
251 @Override
252 public void onMouseDown(final TMouseEvent mouse) {
253 if (mouse.isMouseWheelUp()) {
254 vScroller.decrement();
255 return;
256 }
257 if (mouse.isMouseWheelDown()) {
258 vScroller.increment();
259 return;
260 }
261
262 // Pass to children
263 super.onMouseDown(mouse);
264 }
265
266 /**
267 * Handle keystrokes.
268 *
269 * @param keypress keystroke event
270 */
271 @Override
272 public void onKeypress(final TKeypressEvent keypress) {
273 if (keypress.equals(kbLeft)) {
274 hScroller.decrement();
275 } else if (keypress.equals(kbRight)) {
276 hScroller.increment();
277 } else if (keypress.equals(kbUp)) {
278 vScroller.decrement();
279 } else if (keypress.equals(kbDown)) {
280 vScroller.increment();
281 } else if (keypress.equals(kbPgUp)) {
282 vScroller.bigDecrement();
283 } else if (keypress.equals(kbPgDn)) {
284 vScroller.bigIncrement();
285 } else if (keypress.equals(kbHome)) {
286 vScroller.toTop();
287 } else if (keypress.equals(kbEnd)) {
288 vScroller.toBottom();
289 } else {
290 // Pass other keys (tab etc.) on
291 super.onKeypress(keypress);
292 }
293 }
294
295 /**
296 * Resize text and scrollbars for a new width/height.
297 */
298 @Override
299 public void reflowData() {
300 // Reset the lines
301 lines.clear();
302
303 // Break up text into paragraphs
304 String[] paragraphs = text.split("\n\n");
305 for (String p : paragraphs) {
306 switch (justification) {
307 case NONE:
308 lines.addAll(Arrays.asList(p.split("\n")));
309 break;
310 case LEFT:
311 lines.addAll(jexer.bits.StringUtils.left(p,
312 getWidth() - 1));
313 break;
314 case CENTER:
315 lines.addAll(jexer.bits.StringUtils.center(p,
316 getWidth() - 1));
317 break;
318 case RIGHT:
319 lines.addAll(jexer.bits.StringUtils.right(p,
320 getWidth() - 1));
321 break;
322 case FULL:
323 lines.addAll(jexer.bits.StringUtils.full(p,
324 getWidth() - 1));
325 break;
326 }
327
328 for (int i = 0; i < lineSpacing; i++) {
329 lines.add("");
330 }
331 }
332 computeBounds();
333 }
334
335 // ------------------------------------------------------------------------
336 // TText ------------------------------------------------------------------
337 // ------------------------------------------------------------------------
338
339 /**
340 * Set the text.
341 *
342 * @param text new text to display
343 */
344 public void setText(final String text) {
345 this.text = text;
346 reflowData();
347 }
348
349 /**
350 * Get the text.
351 *
352 * @return the text
353 */
354 public String getText() {
355 return text;
356 }
357
358 /**
359 * Convenience method used by TWindowLoggerOutput.
360 *
361 * @param line new line to add
362 */
363 public void addLine(final String line) {
364 if (StringUtils.width(text) == 0) {
365 text = line;
366 } else {
367 text += "\n\n";
368 text += line;
369 }
370 reflowData();
371 }
372
373 /**
374 * Recompute the bounds for the scrollbars.
375 */
376 private void computeBounds() {
377 maxLineWidth = 0;
378 for (String line : lines) {
379 if (StringUtils.width(line) > maxLineWidth) {
380 maxLineWidth = StringUtils.width(line);
381 }
382 }
383
384 vScroller.setTopValue(0);
385 vScroller.setBottomValue((lines.size() - getHeight()) + 1);
386 if (vScroller.getBottomValue() < 0) {
387 vScroller.setBottomValue(0);
388 }
389 if (vScroller.getValue() > vScroller.getBottomValue()) {
390 vScroller.setValue(vScroller.getBottomValue());
391 }
392
393 hScroller.setLeftValue(0);
394 hScroller.setRightValue((maxLineWidth - getWidth()) + 1);
395 if (hScroller.getRightValue() < 0) {
396 hScroller.setRightValue(0);
397 }
398 if (hScroller.getValue() > hScroller.getRightValue()) {
399 hScroller.setValue(hScroller.getRightValue());
400 }
401 }
402
403 /**
404 * Set justification.
405 *
406 * @param justification NONE, LEFT, CENTER, RIGHT, or FULL
407 */
408 public void setJustification(final Justification justification) {
409 this.justification = justification;
410 reflowData();
411 }
412
413 /**
414 * Left-justify the text.
415 */
416 public void leftJustify() {
417 justification = Justification.LEFT;
418 reflowData();
419 }
420
421 /**
422 * Center-justify the text.
423 */
424 public void centerJustify() {
425 justification = Justification.CENTER;
426 reflowData();
427 }
428
429 /**
430 * Right-justify the text.
431 */
432 public void rightJustify() {
433 justification = Justification.RIGHT;
434 reflowData();
435 }
436
437 /**
438 * Fully-justify the text.
439 */
440 public void fullJustify() {
441 justification = Justification.FULL;
442 reflowData();
443 }
444
445 /**
446 * Un-justify the text.
447 */
448 public void unJustify() {
449 justification = Justification.NONE;
450 reflowData();
451 }
452
453 }