2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.teditor
;
31 import jexer
.bits
.CellAttributes
;
34 * A Word represents text that was entered by the user. It can be either
35 * whitespace or non-whitespace.
37 * Very dumb highlighting is supported, it has no sense of parsing (not even
38 * comments). For now this only highlights some Java keywords and
44 * The color to render this word as on screen.
46 private CellAttributes color
= new CellAttributes();
49 * The default color for the TEditor class.
51 private CellAttributes defaultColor
= null;
54 * The text highlighter to use.
56 private Highlighter highlighter
= null;
59 * The actual text of this word. Average word length is 6 characters,
60 * with a lot of shorter ones, so start with 3.
62 private StringBuilder text
= new StringBuilder(3);
65 * Get the color used to display this word on screen.
69 public CellAttributes
getColor() {
70 return new CellAttributes(color
);
74 * Set the color used to display this word on screen.
76 * @param color the color
78 public void setColor(final CellAttributes color
) {
83 * Get the text to display.
87 public String
getText() {
88 return text
.toString();
92 * Get the on-screen display length.
94 * @return the number of cells needed to display this word
96 public int getDisplayLength() {
97 // For now, just use the text length. In the future, this will be a
100 // TODO: figure out how to handle the tab character. Do we have a
101 // global tab stops list and current word position?
102 return text
.length();
106 * See if this is a whitespace word. Note that empty string is
107 * considered whitespace.
109 * @return true if this word is whitespace
111 public boolean isWhitespace() {
112 if (text
.length() == 0) {
115 if (Character
.isWhitespace(text
.charAt(0))) {
122 * Construct a word with one character.
124 * @param ch the first character of the word
125 * @param defaultColor the color for unhighlighted text
126 * @param highlighter the highlighter to use
128 public Word(final char ch
, final CellAttributes defaultColor
,
129 final Highlighter highlighter
) {
131 this.defaultColor
= defaultColor
;
132 this.highlighter
= highlighter
;
137 * Construct a word with an empty string.
139 * @param defaultColor the color for unhighlighted text
140 * @param highlighter the highlighter to use
142 public Word(final CellAttributes defaultColor
,
143 final Highlighter highlighter
) {
145 this.defaultColor
= defaultColor
;
146 this.highlighter
= highlighter
;
150 * Perform highlighting.
152 public void applyHighlight() {
153 color
.setTo(defaultColor
);
154 if (highlighter
== null) {
157 String key
= text
.toString();
158 CellAttributes newColor
= highlighter
.getColor(key
);
159 if (newColor
!= null) {
160 color
.setTo(newColor
);
165 * Add a character to this word. If this is a whitespace character
166 * adding to a non-whitespace word, create a new word and return that;
167 * similarly if this a non-whitespace character adding to a whitespace
168 * word, create a new word and return that. Note package private access:
169 * this is only called by Line to figure out highlighting boundaries.
171 * @param ch the new character to add
172 * @return either this word (if it was added), or a new word that
175 public Word
addChar(final char ch
) {
176 if (text
.length() == 0) {
181 // Give the highlighter the option to split here.
182 if (highlighter
!= null) {
183 if (highlighter
.shouldSplit(ch
)
184 || highlighter
.shouldSplit(text
.charAt(0))
186 Word newWord
= new Word(ch
, defaultColor
, highlighter
);
191 // Highlighter didn't care, so split at whitespace.
192 if (Character
.isWhitespace(text
.charAt(0))
193 && Character
.isWhitespace(ch
)
195 // Adding to a whitespace word, keep at it.
199 if (!Character
.isWhitespace(text
.charAt(0))
200 && !Character
.isWhitespace(ch
)
202 // Adding to a non-whitespace word, keep at it.
207 // Switching from whitespace to non-whitespace or vice versa, so
209 Word newWord
= new Word(ch
, defaultColor
, highlighter
);