d7a65760e628e9295e7932d6b0b38b690a1258bb
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.
40 * The color to render this word as on screen.
42 private CellAttributes color
= new CellAttributes();
45 * The actual text of this word. Average word length is 6 characters,
46 * with a lot of shorter ones, so start with 3.
48 private StringBuilder text
= new StringBuilder(3);
51 * Get the color used to display this word on screen.
55 public CellAttributes
getColor() {
56 return new CellAttributes(color
);
60 * Set the color used to display this word on screen.
62 * @param color the color
64 public void setColor(final CellAttributes color
) {
69 * Get the text to display.
73 public String
getText() {
74 return text
.toString();
78 * Get the on-screen display length.
80 * @return the number of cells needed to display this word
82 public int getDisplayLength() {
83 // For now, just use the text length. In the future, this will be a
86 // TODO: figure out how to handle the tab character. Do we have a
87 // global tab stops list and current word position?
92 * See if this is a whitespace word. Note that empty string is
93 * considered whitespace.
95 * @return true if this word is whitespace
97 public boolean isWhitespace() {
98 if (text
.length() == 0) {
101 if (Character
.isWhitespace(text
.charAt(0))) {
108 * Construct a word with one character.
110 * @param ch the first character of the word
112 public Word(final char ch
) {
117 * Construct a word with an empty string.
122 * Add a character to this word. If this is a whitespace character
123 * adding to a non-whitespace word, create a new word and return that;
124 * similarly if this a non-whitespace character adding to a whitespace
125 * word, create a new word and return that.
127 * @param ch the new character to add
128 * @return either this word (if it was added), or a new word that
131 public Word
addChar(final char ch
) {
132 if (text
.length() == 0) {
136 if (Character
.isWhitespace(text
.charAt(0))
137 && Character
.isWhitespace(ch
)
142 if (!Character
.isWhitespace(text
.charAt(0))
143 && !Character
.isWhitespace(ch
)
149 // We will be splitting here.
150 Word newWord
= new Word(ch
);