2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 * The color to render this word as on screen.
50 private CellAttributes color
= new CellAttributes();
53 * The default color for the TEditor class.
55 private CellAttributes defaultColor
= null;
58 * The text highlighter to use.
60 private Highlighter highlighter
= null;
63 * The actual text of this word. Average word length is 6 characters,
64 * with a lot of shorter ones, so start with 3.
66 private StringBuilder text
= new StringBuilder(3);
68 // ------------------------------------------------------------------------
69 // Constructors -----------------------------------------------------------
70 // ------------------------------------------------------------------------
73 * Construct a word with one character.
75 * @param ch the first character of the word
76 * @param defaultColor the color for unhighlighted text
77 * @param highlighter the highlighter to use
79 public Word(final char ch
, final CellAttributes defaultColor
,
80 final Highlighter highlighter
) {
82 this.defaultColor
= defaultColor
;
83 this.highlighter
= highlighter
;
88 * Construct a word with an empty string.
90 * @param defaultColor the color for unhighlighted text
91 * @param highlighter the highlighter to use
93 public Word(final CellAttributes defaultColor
,
94 final Highlighter highlighter
) {
96 this.defaultColor
= defaultColor
;
97 this.highlighter
= highlighter
;
100 // ------------------------------------------------------------------------
101 // Word -------------------------------------------------------------------
102 // ------------------------------------------------------------------------
105 * Get the color used to display this word on screen.
109 public CellAttributes
getColor() {
110 return new CellAttributes(color
);
114 * Set the color used to display this word on screen.
116 * @param color the color
118 public void setColor(final CellAttributes color
) {
123 * Get the text to display.
127 public String
getText() {
128 return text
.toString();
132 * Get the on-screen display length.
134 * @return the number of cells needed to display this word
136 public int getDisplayLength() {
137 // For now, just use the text length. In the future, this will be a
140 // TODO: figure out how to handle the tab character. Do we have a
141 // global tab stops list and current word position?
142 return text
.length();
146 * See if this is a whitespace word. Note that empty string is
147 * considered whitespace.
149 * @return true if this word is whitespace
151 public boolean isWhitespace() {
152 if (text
.length() == 0) {
155 if (Character
.isWhitespace(text
.charAt(0))) {
162 * Perform highlighting.
164 public void applyHighlight() {
165 color
.setTo(defaultColor
);
166 if (highlighter
== null) {
169 String key
= text
.toString();
170 CellAttributes newColor
= highlighter
.getColor(key
);
171 if (newColor
!= null) {
172 color
.setTo(newColor
);
177 * Add a character to this word. If this is a whitespace character
178 * adding to a non-whitespace word, create a new word and return that;
179 * similarly if this a non-whitespace character adding to a whitespace
180 * word, create a new word and return that. Note package private access:
181 * this is only called by Line to figure out highlighting boundaries.
183 * @param ch the new character to add
184 * @return either this word (if it was added), or a new word that
187 public Word
addChar(final char ch
) {
188 if (text
.length() == 0) {
193 // Give the highlighter the option to split here.
194 if (highlighter
!= null) {
195 if (highlighter
.shouldSplit(ch
)
196 || highlighter
.shouldSplit(text
.charAt(0))
198 Word newWord
= new Word(ch
, defaultColor
, highlighter
);
203 // Highlighter didn't care, so split at whitespace.
204 if (Character
.isWhitespace(text
.charAt(0))
205 && Character
.isWhitespace(ch
)
207 // Adding to a whitespace word, keep at it.
211 if (!Character
.isWhitespace(text
.charAt(0))
212 && !Character
.isWhitespace(ch
)
214 // Adding to a non-whitespace word, keep at it.
219 // Switching from whitespace to non-whitespace or vice versa, so
221 Word newWord
= new Word(ch
, defaultColor
, highlighter
);