Prep for 2019 release
[fanfix.git] / src / jexer / teditor / Line.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 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 java.util.ArrayList;
32 import java.util.List;
33
34 import jexer.bits.CellAttributes;
35
36 /**
37 * A Line represents a single line of text on the screen, as a collection of
38 * words.
39 */
40 public class Line {
41
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
46 /**
47 * The list of words.
48 */
49 private ArrayList<Word> words = new ArrayList<Word>();
50
51 /**
52 * The default color for the TEditor class.
53 */
54 private CellAttributes defaultColor = null;
55
56 /**
57 * The text highlighter to use.
58 */
59 private Highlighter highlighter = null;
60
61 /**
62 * The current cursor position on this line.
63 */
64 private int cursor = 0;
65
66 /**
67 * The raw text of this line, what is passed to Word to determine
68 * highlighting behavior.
69 */
70 private StringBuilder rawText;
71
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
75
76 /**
77 * Construct a new Line from an existing text string, and highlight
78 * certain strings.
79 *
80 * @param str the text string
81 * @param defaultColor the color for unhighlighted text
82 * @param highlighter the highlighter to use
83 */
84 public Line(final String str, final CellAttributes defaultColor,
85 final Highlighter highlighter) {
86
87 this.defaultColor = defaultColor;
88 this.highlighter = highlighter;
89 this.rawText = new StringBuilder(str);
90
91 scanLine();
92 }
93
94 /**
95 * Construct a new Line from an existing text string.
96 *
97 * @param str the text string
98 * @param defaultColor the color for unhighlighted text
99 */
100 public Line(final String str, final CellAttributes defaultColor) {
101 this(str, defaultColor, null);
102 }
103
104 // ------------------------------------------------------------------------
105 // Line -------------------------------------------------------------------
106 // ------------------------------------------------------------------------
107
108 /**
109 * Get a (shallow) copy of the words in this line.
110 *
111 * @return a copy of the word list
112 */
113 public List<Word> getWords() {
114 return new ArrayList<Word>(words);
115 }
116
117 /**
118 * Get the current cursor position.
119 *
120 * @return the cursor position
121 */
122 public int getCursor() {
123 return cursor;
124 }
125
126 /**
127 * Set the current cursor position.
128 *
129 * @param cursor the new cursor position
130 */
131 public void setCursor(final int cursor) {
132 if ((cursor < 0)
133 || ((cursor >= getDisplayLength())
134 && (getDisplayLength() > 0))
135 ) {
136 throw new IndexOutOfBoundsException("Max length is " +
137 getDisplayLength() + ", requested position " + cursor);
138 }
139 this.cursor = cursor;
140 }
141
142 /**
143 * Get the on-screen display length.
144 *
145 * @return the number of cells needed to display this line
146 */
147 public int getDisplayLength() {
148 int n = rawText.length();
149
150 // For now just return the raw text length.
151 if (n > 0) {
152 // If we have any visible characters, add one to the display so
153 // that the cursor is immediately after the data.
154 return n + 1;
155 }
156 return n;
157 }
158
159 /**
160 * Get the raw string that matches this line.
161 *
162 * @return the string
163 */
164 public String getRawString() {
165 return rawText.toString();
166 }
167
168 /**
169 * Scan rawText and make words out of it.
170 */
171 private void scanLine() {
172 words.clear();
173 Word word = new Word(this.defaultColor, this.highlighter);
174 words.add(word);
175 for (int i = 0; i < rawText.length(); i++) {
176 char ch = rawText.charAt(i);
177 Word newWord = word.addChar(ch);
178 if (newWord != word) {
179 words.add(newWord);
180 word = newWord;
181 }
182 }
183 for (Word w: words) {
184 w.applyHighlight();
185 }
186 }
187
188 /**
189 * Decrement the cursor by one. If at the first column, do nothing.
190 *
191 * @return true if the cursor position changed
192 */
193 public boolean left() {
194 if (cursor == 0) {
195 return false;
196 }
197 cursor--;
198 return true;
199 }
200
201 /**
202 * Increment the cursor by one. If at the last column, do nothing.
203 *
204 * @return true if the cursor position changed
205 */
206 public boolean right() {
207 if (getDisplayLength() == 0) {
208 return false;
209 }
210 if (cursor == getDisplayLength() - 1) {
211 return false;
212 }
213 cursor++;
214 return true;
215 }
216
217 /**
218 * Go to the first column of this line.
219 *
220 * @return true if the cursor position changed
221 */
222 public boolean home() {
223 if (cursor > 0) {
224 cursor = 0;
225 return true;
226 }
227 return false;
228 }
229
230 /**
231 * Go to the last column of this line.
232 *
233 * @return true if the cursor position changed
234 */
235 public boolean end() {
236 if (cursor != getDisplayLength() - 1) {
237 cursor = getDisplayLength() - 1;
238 if (cursor < 0) {
239 cursor = 0;
240 }
241 return true;
242 }
243 return false;
244 }
245
246 /**
247 * Delete the character under the cursor.
248 */
249 public void del() {
250 assert (words.size() > 0);
251
252 if (cursor < getDisplayLength()) {
253 rawText.deleteCharAt(cursor);
254 }
255
256 // Re-scan the line to determine the new word boundaries.
257 scanLine();
258 }
259
260 /**
261 * Delete the character immediately preceeding the cursor.
262 */
263 public void backspace() {
264 if (left()) {
265 del();
266 }
267 }
268
269 /**
270 * Insert a character at the cursor.
271 *
272 * @param ch the character to insert
273 */
274 public void addChar(final char ch) {
275 if (cursor < getDisplayLength() - 1) {
276 rawText.insert(cursor, ch);
277 } else {
278 rawText.append(ch);
279 }
280 scanLine();
281 cursor++;
282 }
283
284 /**
285 * Replace a character at the cursor.
286 *
287 * @param ch the character to replace
288 */
289 public void replaceChar(final char ch) {
290 if (cursor < getDisplayLength() - 1) {
291 rawText.setCharAt(cursor, ch);
292 } else {
293 rawText.append(ch);
294 }
295 scanLine();
296 cursor++;
297 }
298
299 }