Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
cc99cba8 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
cc99cba8 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 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 | ||
7c870d89 KL |
31 | import static jexer.TKeypress.kbDown; |
32 | import static jexer.TKeypress.kbEnd; | |
33 | import static jexer.TKeypress.kbHome; | |
34 | import static jexer.TKeypress.kbLeft; | |
35 | import static jexer.TKeypress.kbPgDn; | |
36 | import static jexer.TKeypress.kbPgUp; | |
37 | import static jexer.TKeypress.kbRight; | |
38 | import static jexer.TKeypress.kbUp; | |
39 | ||
cc99cba8 KL |
40 | import java.util.LinkedList; |
41 | import java.util.List; | |
42 | ||
43 | import jexer.bits.CellAttributes; | |
44 | import jexer.event.TKeypressEvent; | |
45 | import jexer.event.TMouseEvent; | |
cc99cba8 KL |
46 | |
47 | /** | |
7c870d89 KL |
48 | * TText implements a simple scrollable text area. It reflows automatically on |
49 | * resize. | |
cc99cba8 | 50 | */ |
56661844 | 51 | public final class TText extends TScrollableWidget { |
cc99cba8 | 52 | |
7657ad8c KL |
53 | /** |
54 | * Available text justifications. | |
55 | */ | |
56 | public enum Justification { | |
57 | /** | |
58 | * Left-justified text. | |
59 | */ | |
60 | LEFT, | |
61 | ||
62 | /** | |
63 | * Centered text. | |
64 | */ | |
65 | CENTER, | |
66 | ||
67 | /** | |
68 | * Right-justified text. | |
69 | */ | |
70 | RIGHT, | |
71 | ||
72 | /** | |
73 | * Fully-justified text. | |
74 | */ | |
75 | FULL, | |
76 | } | |
77 | ||
78 | /** | |
79 | * How to justify the text. | |
80 | */ | |
81 | private Justification justification = Justification.LEFT; | |
82 | ||
cc99cba8 KL |
83 | /** |
84 | * Text to display. | |
85 | */ | |
86 | private String text; | |
87 | ||
88 | /** | |
89 | * Text converted to lines. | |
90 | */ | |
91 | private List<String> lines; | |
92 | ||
93 | /** | |
94 | * Text color. | |
95 | */ | |
96 | private String colorKey; | |
97 | ||
98 | /** | |
56661844 | 99 | * Maximum width of a single line. |
cc99cba8 | 100 | */ |
56661844 | 101 | private int maxLineWidth; |
cc99cba8 KL |
102 | |
103 | /** | |
56661844 | 104 | * Number of lines between each paragraph. |
cc99cba8 | 105 | */ |
56661844 | 106 | private int lineSpacing = 1; |
cc99cba8 KL |
107 | |
108 | /** | |
56661844 KL |
109 | * Set the text. |
110 | * | |
111 | * @param text new text to display | |
cc99cba8 | 112 | */ |
56661844 KL |
113 | public void setText(final String text) { |
114 | this.text = text; | |
115 | reflowData(); | |
116 | } | |
cc99cba8 KL |
117 | |
118 | /** | |
56661844 KL |
119 | * Get the text. |
120 | * | |
121 | * @return the text | |
cc99cba8 | 122 | */ |
56661844 KL |
123 | public String getText() { |
124 | return text; | |
125 | } | |
cc99cba8 KL |
126 | |
127 | /** | |
128 | * Convenience method used by TWindowLoggerOutput. | |
129 | * | |
5dfd1c11 | 130 | * @param line new line to add |
cc99cba8 KL |
131 | */ |
132 | public void addLine(final String line) { | |
133 | if (text.length() == 0) { | |
134 | text = line; | |
135 | } else { | |
136 | text += "\n\n"; | |
137 | text += line; | |
138 | } | |
56661844 | 139 | reflowData(); |
cc99cba8 KL |
140 | } |
141 | ||
142 | /** | |
143 | * Recompute the bounds for the scrollbars. | |
144 | */ | |
145 | private void computeBounds() { | |
146 | maxLineWidth = 0; | |
7c870d89 | 147 | for (String line : lines) { |
cc99cba8 KL |
148 | if (line.length() > maxLineWidth) { |
149 | maxLineWidth = line.length(); | |
150 | } | |
151 | } | |
152 | ||
56661844 | 153 | vScroller.setTopValue(0); |
7c870d89 | 154 | vScroller.setBottomValue((lines.size() - getHeight()) + 1); |
cc99cba8 KL |
155 | if (vScroller.getBottomValue() < 0) { |
156 | vScroller.setBottomValue(0); | |
157 | } | |
158 | if (vScroller.getValue() > vScroller.getBottomValue()) { | |
159 | vScroller.setValue(vScroller.getBottomValue()); | |
160 | } | |
161 | ||
56661844 | 162 | hScroller.setLeftValue(0); |
7c870d89 | 163 | hScroller.setRightValue((maxLineWidth - getWidth()) + 1); |
cc99cba8 KL |
164 | if (hScroller.getRightValue() < 0) { |
165 | hScroller.setRightValue(0); | |
166 | } | |
167 | if (hScroller.getValue() > hScroller.getRightValue()) { | |
168 | hScroller.setValue(hScroller.getRightValue()); | |
169 | } | |
170 | } | |
171 | ||
172 | /** | |
7657ad8c | 173 | * Set justification. |
cc99cba8 | 174 | * |
7657ad8c KL |
175 | * @param justification LEFT, CENTER, RIGHT, or FULL |
176 | */ | |
177 | public void setJustification(final Justification justification) { | |
178 | this.justification = justification; | |
56661844 | 179 | reflowData(); |
7657ad8c | 180 | } |
cc99cba8 | 181 | |
7657ad8c KL |
182 | /** |
183 | * Left-justify the text. | |
184 | */ | |
185 | public void leftJustify() { | |
186 | justification = Justification.LEFT; | |
56661844 | 187 | reflowData(); |
7657ad8c KL |
188 | } |
189 | ||
190 | /** | |
191 | * Center-justify the text. | |
192 | */ | |
193 | public void centerJustify() { | |
194 | justification = Justification.CENTER; | |
56661844 | 195 | reflowData(); |
7657ad8c KL |
196 | } |
197 | ||
198 | /** | |
199 | * Right-justify the text. | |
200 | */ | |
201 | public void rightJustify() { | |
202 | justification = Justification.RIGHT; | |
56661844 | 203 | reflowData(); |
7657ad8c KL |
204 | } |
205 | ||
206 | /** | |
207 | * Fully-justify the text. | |
208 | */ | |
209 | public void fullJustify() { | |
210 | justification = Justification.FULL; | |
56661844 | 211 | reflowData(); |
cc99cba8 KL |
212 | } |
213 | ||
cc99cba8 KL |
214 | /** |
215 | * Resize text and scrollbars for a new width/height. | |
216 | */ | |
56661844 KL |
217 | @Override |
218 | public void reflowData() { | |
cc99cba8 KL |
219 | // Reset the lines |
220 | lines.clear(); | |
221 | ||
222 | // Break up text into paragraphs | |
7c870d89 KL |
223 | String[] paragraphs = text.split("\n\n"); |
224 | for (String p : paragraphs) { | |
7657ad8c KL |
225 | switch (justification) { |
226 | case LEFT: | |
227 | lines.addAll(jexer.bits.StringJustifier.left(p, | |
228 | getWidth() - 1)); | |
229 | break; | |
230 | case CENTER: | |
231 | lines.addAll(jexer.bits.StringJustifier.center(p, | |
232 | getWidth() - 1)); | |
233 | break; | |
234 | case RIGHT: | |
235 | lines.addAll(jexer.bits.StringJustifier.right(p, | |
236 | getWidth() - 1)); | |
237 | break; | |
238 | case FULL: | |
239 | lines.addAll(jexer.bits.StringJustifier.full(p, | |
240 | getWidth() - 1)); | |
241 | break; | |
cc99cba8 | 242 | } |
7657ad8c | 243 | |
cc99cba8 KL |
244 | for (int i = 0; i < lineSpacing; i++) { |
245 | lines.add(""); | |
246 | } | |
247 | } | |
cc99cba8 KL |
248 | computeBounds(); |
249 | } | |
250 | ||
251 | /** | |
252 | * Public constructor. | |
253 | * | |
5dfd1c11 KL |
254 | * @param parent parent widget |
255 | * @param text text on the screen | |
256 | * @param x column relative to parent | |
257 | * @param y row relative to parent | |
258 | * @param width width of text area | |
259 | * @param height height of text area | |
cc99cba8 KL |
260 | */ |
261 | public TText(final TWidget parent, final String text, final int x, | |
7c870d89 | 262 | final int y, final int width, final int height) { |
cc99cba8 KL |
263 | |
264 | this(parent, text, x, y, width, height, "ttext"); | |
265 | } | |
266 | ||
267 | /** | |
268 | * Public constructor. | |
269 | * | |
5dfd1c11 KL |
270 | * @param parent parent widget |
271 | * @param text text on the screen | |
272 | * @param x column relative to parent | |
273 | * @param y row relative to parent | |
274 | * @param width width of text area | |
275 | * @param height height of text area | |
276 | * @param colorKey ColorTheme key color to use for foreground | |
277 | * text. Default is "ttext". | |
cc99cba8 KL |
278 | */ |
279 | public TText(final TWidget parent, final String text, final int x, | |
7c870d89 KL |
280 | final int y, final int width, final int height, |
281 | final String colorKey) { | |
cc99cba8 KL |
282 | |
283 | // Set parent and window | |
a83fea2b | 284 | super(parent, x, y, width, height); |
cc99cba8 | 285 | |
cc99cba8 KL |
286 | this.text = text; |
287 | this.colorKey = colorKey; | |
288 | ||
289 | lines = new LinkedList<String>(); | |
290 | ||
56661844 KL |
291 | vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); |
292 | hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); | |
293 | reflowData(); | |
cc99cba8 KL |
294 | } |
295 | ||
296 | /** | |
297 | * Draw the text box. | |
298 | */ | |
299 | @Override | |
300 | public void draw() { | |
301 | // Setup my color | |
302 | CellAttributes color = getTheme().getColor(colorKey); | |
303 | ||
304 | int begin = vScroller.getValue(); | |
305 | int topY = 0; | |
306 | for (int i = begin; i < lines.size(); i++) { | |
307 | String line = lines.get(i); | |
308 | if (hScroller.getValue() < line.length()) { | |
309 | line = line.substring(hScroller.getValue()); | |
310 | } else { | |
311 | line = ""; | |
312 | } | |
313 | String formatString = "%-" + Integer.toString(getWidth() - 1) + "s"; | |
0d47c546 | 314 | getScreen().putStringXY(0, topY, String.format(formatString, line), |
7c870d89 | 315 | color); |
cc99cba8 KL |
316 | topY++; |
317 | ||
7c870d89 | 318 | if (topY >= (getHeight() - 1)) { |
cc99cba8 KL |
319 | break; |
320 | } | |
321 | } | |
322 | ||
323 | // Pad the rest with blank lines | |
7c870d89 | 324 | for (int i = topY; i < (getHeight() - 1); i++) { |
cc99cba8 KL |
325 | getScreen().hLineXY(0, i, getWidth() - 1, ' ', color); |
326 | } | |
327 | ||
328 | } | |
329 | ||
330 | /** | |
331 | * Handle mouse press events. | |
332 | * | |
5dfd1c11 | 333 | * @param mouse mouse button press event |
cc99cba8 KL |
334 | */ |
335 | @Override | |
336 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 337 | if (mouse.isMouseWheelUp()) { |
cc99cba8 KL |
338 | vScroller.decrement(); |
339 | return; | |
340 | } | |
7c870d89 | 341 | if (mouse.isMouseWheelDown()) { |
cc99cba8 KL |
342 | vScroller.increment(); |
343 | return; | |
344 | } | |
345 | ||
346 | // Pass to children | |
347 | super.onMouseDown(mouse); | |
348 | } | |
349 | ||
350 | /** | |
351 | * Handle keystrokes. | |
352 | * | |
5dfd1c11 | 353 | * @param keypress keystroke event |
cc99cba8 KL |
354 | */ |
355 | @Override | |
356 | public void onKeypress(final TKeypressEvent keypress) { | |
357 | if (keypress.equals(kbLeft)) { | |
358 | hScroller.decrement(); | |
359 | } else if (keypress.equals(kbRight)) { | |
360 | hScroller.increment(); | |
361 | } else if (keypress.equals(kbUp)) { | |
362 | vScroller.decrement(); | |
363 | } else if (keypress.equals(kbDown)) { | |
364 | vScroller.increment(); | |
365 | } else if (keypress.equals(kbPgUp)) { | |
366 | vScroller.bigDecrement(); | |
367 | } else if (keypress.equals(kbPgDn)) { | |
368 | vScroller.bigIncrement(); | |
369 | } else if (keypress.equals(kbHome)) { | |
370 | vScroller.toTop(); | |
371 | } else if (keypress.equals(kbEnd)) { | |
372 | vScroller.toBottom(); | |
373 | } else { | |
374 | // Pass other keys (tab etc.) on | |
375 | super.onKeypress(keypress); | |
376 | } | |
377 | } | |
378 | ||
379 | } |