Commit | Line | Data |
---|---|---|
12b55d76 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * The MIT License (MIT) | |
5 | * | |
6 | * Copyright (C) 2017 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.teditor; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
32 | ||
33 | /** | |
34 | * A Word represents text that was entered by the user. It can be either | |
35 | * whitespace or non-whitespace. | |
e8a11f98 KL |
36 | * |
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 | |
39 | * puctuation. | |
12b55d76 KL |
40 | */ |
41 | public class Word { | |
42 | ||
43 | /** | |
44 | * The color to render this word as on screen. | |
45 | */ | |
46 | private CellAttributes color = new CellAttributes(); | |
47 | ||
e8a11f98 KL |
48 | /** |
49 | * The default color for the TEditor class. | |
50 | */ | |
51 | private CellAttributes defaultColor = null; | |
52 | ||
53 | /** | |
54 | * The text highlighter to use. | |
55 | */ | |
56 | private Highlighter highlighter = null; | |
57 | ||
12b55d76 KL |
58 | /** |
59 | * The actual text of this word. Average word length is 6 characters, | |
60 | * with a lot of shorter ones, so start with 3. | |
61 | */ | |
62 | private StringBuilder text = new StringBuilder(3); | |
63 | ||
64 | /** | |
65 | * Get the color used to display this word on screen. | |
66 | * | |
67 | * @return the color | |
68 | */ | |
69 | public CellAttributes getColor() { | |
70 | return new CellAttributes(color); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Set the color used to display this word on screen. | |
75 | * | |
76 | * @param color the color | |
77 | */ | |
78 | public void setColor(final CellAttributes color) { | |
79 | color.setTo(color); | |
80 | } | |
81 | ||
82 | /** | |
83 | * Get the text to display. | |
84 | * | |
85 | * @return the text | |
86 | */ | |
87 | public String getText() { | |
88 | return text.toString(); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Get the on-screen display length. | |
93 | * | |
94 | * @return the number of cells needed to display this word | |
95 | */ | |
96 | public int getDisplayLength() { | |
97 | // For now, just use the text length. In the future, this will be a | |
98 | // grapheme count. | |
99 | ||
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(); | |
103 | } | |
104 | ||
105 | /** | |
106 | * See if this is a whitespace word. Note that empty string is | |
107 | * considered whitespace. | |
108 | * | |
109 | * @return true if this word is whitespace | |
110 | */ | |
111 | public boolean isWhitespace() { | |
112 | if (text.length() == 0) { | |
113 | return true; | |
114 | } | |
115 | if (Character.isWhitespace(text.charAt(0))) { | |
116 | return true; | |
117 | } | |
118 | return false; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Construct a word with one character. | |
123 | * | |
124 | * @param ch the first character of the word | |
e8a11f98 KL |
125 | * @param defaultColor the color for unhighlighted text |
126 | * @param highlighter the highlighter to use | |
12b55d76 | 127 | */ |
e8a11f98 KL |
128 | public Word(final char ch, final CellAttributes defaultColor, |
129 | final Highlighter highlighter) { | |
130 | ||
131 | this.defaultColor = defaultColor; | |
132 | this.highlighter = highlighter; | |
12b55d76 KL |
133 | text.append(ch); |
134 | } | |
135 | ||
136 | /** | |
137 | * Construct a word with an empty string. | |
e8a11f98 KL |
138 | * |
139 | * @param defaultColor the color for unhighlighted text | |
140 | * @param highlighter the highlighter to use | |
141 | */ | |
142 | public Word(final CellAttributes defaultColor, | |
143 | final Highlighter highlighter) { | |
144 | ||
145 | this.defaultColor = defaultColor; | |
146 | this.highlighter = highlighter; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Perform highlighting. | |
12b55d76 | 151 | */ |
e8a11f98 KL |
152 | public void applyHighlight() { |
153 | color.setTo(defaultColor); | |
154 | if (highlighter == null) { | |
155 | return; | |
156 | } | |
157 | String key = text.toString(); | |
158 | CellAttributes newColor = highlighter.getColor(key); | |
159 | if (newColor != null) { | |
160 | color.setTo(newColor); | |
161 | } | |
162 | } | |
12b55d76 KL |
163 | |
164 | /** | |
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 | |
71a389c9 KL |
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. | |
12b55d76 KL |
170 | * |
171 | * @param ch the new character to add | |
172 | * @return either this word (if it was added), or a new word that | |
173 | * contains ch | |
174 | */ | |
175 | public Word addChar(final char ch) { | |
176 | if (text.length() == 0) { | |
177 | text.append(ch); | |
178 | return this; | |
179 | } | |
e8a11f98 KL |
180 | |
181 | // Give the highlighter the option to split here. | |
182 | if (highlighter != null) { | |
183 | if (highlighter.shouldSplit(ch) | |
184 | || highlighter.shouldSplit(text.charAt(0)) | |
185 | ) { | |
186 | Word newWord = new Word(ch, defaultColor, highlighter); | |
187 | return newWord; | |
188 | } | |
189 | } | |
190 | ||
191 | // Highlighter didn't care, so split at whitespace. | |
12b55d76 KL |
192 | if (Character.isWhitespace(text.charAt(0)) |
193 | && Character.isWhitespace(ch) | |
194 | ) { | |
e8a11f98 | 195 | // Adding to a whitespace word, keep at it. |
12b55d76 KL |
196 | text.append(ch); |
197 | return this; | |
198 | } | |
199 | if (!Character.isWhitespace(text.charAt(0)) | |
200 | && !Character.isWhitespace(ch) | |
201 | ) { | |
e8a11f98 | 202 | // Adding to a non-whitespace word, keep at it. |
12b55d76 KL |
203 | text.append(ch); |
204 | return this; | |
205 | } | |
206 | ||
e8a11f98 KL |
207 | // Switching from whitespace to non-whitespace or vice versa, so |
208 | // split here. | |
209 | Word newWord = new Word(ch, defaultColor, highlighter); | |
12b55d76 KL |
210 | return newWord; |
211 | } | |
212 | ||
213 | } |