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 java
.util
.ArrayList
;
32 import java
.util
.List
;
34 import jexer
.bits
.CellAttributes
;
37 * A Line represents a single line of text on the screen, as a collection of
45 private ArrayList
<Word
> words
= new ArrayList
<Word
>();
48 * The default color for the TEditor class.
50 private CellAttributes defaultColor
= null;
53 * The text highlighter to use.
55 private Highlighter highlighter
= null;
58 * The current cursor position on this line.
60 private int cursor
= 0;
63 * The current word that the cursor position is in.
65 private Word currentWord
;
68 * We use getDisplayLength() a lot, so cache the value.
70 private int displayLength
= -1;
73 * Get the current cursor position.
75 * @return the cursor position
77 public int getCursor() {
82 * Set the current cursor position.
84 * @param cursor the new cursor position
86 public void setCursor(final int cursor
) {
88 || ((cursor
>= getDisplayLength())
89 && (getDisplayLength() > 0))
91 throw new IndexOutOfBoundsException("Max length is " +
92 getDisplayLength() + ", requested position " + cursor
);
99 * Get a (shallow) copy of the list of words.
101 * @return the list of words
103 public List
<Word
> getWords() {
104 return new ArrayList
<Word
>(words
);
108 * Get the on-screen display length.
110 * @return the number of cells needed to display this line
112 public int getDisplayLength() {
113 if (displayLength
!= -1) {
114 return displayLength
;
117 for (Word word
: words
) {
118 n
+= word
.getDisplayLength();
122 // If we have any visible characters, add one to the display so that
123 // the cursor is immediately after the data.
124 if (displayLength
> 0) {
127 return displayLength
;
131 * Construct a new Line from an existing text string, and highlight
134 * @param str the text string
135 * @param defaultColor the color for unhighlighted text
136 * @param highlighter the highlighter to use
138 public Line(final String str
, final CellAttributes defaultColor
,
139 final Highlighter highlighter
) {
141 this.defaultColor
= defaultColor
;
142 this.highlighter
= highlighter
;
144 currentWord
= new Word(this.defaultColor
, this.highlighter
);
145 words
.add(currentWord
);
146 for (int i
= 0; i
< str
.length(); i
++) {
147 char ch
= str
.charAt(i
);
148 Word newWord
= currentWord
.addChar(ch
);
149 if (newWord
!= currentWord
) {
151 currentWord
= newWord
;
154 for (Word word
: words
) {
155 word
.applyHighlight();
160 * Construct a new Line from an existing text string.
162 * @param str the text string
163 * @param defaultColor the color for unhighlighted text
165 public Line(final String str
, final CellAttributes defaultColor
) {
166 this(str
, defaultColor
, null);
170 * Decrement the cursor by one. If at the first column, do nothing.
172 * @return true if the cursor position changed
174 public boolean left() {
184 * Increment the cursor by one. If at the last column, do nothing.
186 * @return true if the cursor position changed
188 public boolean right() {
189 if (getDisplayLength() == 0) {
192 if (cursor
== getDisplayLength() - 1) {
201 * Go to the first column of this line.
203 * @return true if the cursor position changed
205 public boolean home() {
208 currentWord
= words
.get(0);
215 * Go to the last column of this line.
217 * @return true if the cursor position changed
219 public boolean end() {
220 if (cursor
!= getDisplayLength() - 1) {
221 cursor
= getDisplayLength() - 1;
225 currentWord
= words
.get(words
.size() - 1);
232 * Delete the character under the cursor.
239 * Delete the character immediately preceeding the cursor.
241 public void backspace() {
246 * Insert a character at the cursor.
248 * @param ch the character to insert
250 public void addChar(final char ch
) {
255 * Replace a character at the cursor.
257 * @param ch the character to replace
259 public void replaceChar(final char ch
) {