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
;
35 * A Document represents a text file, as a collection of lines.
37 public class Document
{
42 private ArrayList
<Line
> lines
= new ArrayList
<Line
>();
45 * The current line number being edited. Note that this is 0-based, the
46 * first line is line number 0.
48 private int lineNumber
= 0;
51 * The overwrite flag. When true, characters overwrite data.
53 private boolean overwrite
= false;
56 * Get the overwrite flag.
58 * @return true if addChar() overwrites data, false if it inserts
60 public boolean getOverwrite() {
65 * Set the overwrite flag.
67 * @param overwrite true if addChar() should overwrite data, false if it
70 public void setOverwrite(final boolean overwrite
) {
71 this.overwrite
= overwrite
;
75 * Get the current line number being edited.
77 * @return the line number. Note that this is 0-based: 0 is the first
80 public int getLineNumber() {
85 * Get a specific line by number.
87 * @param lineNumber the line number. Note that this is 0-based: 0 is
91 public Line
getLine(final int lineNumber
) {
92 return lines
.get(lineNumber
);
96 * Set the current line number being edited.
98 * @param n the line number. Note that this is 0-based: 0 is the first
101 public void setLineNumber(final int n
) {
102 if ((n
< 0) || (n
> lines
.size())) {
103 throw new IndexOutOfBoundsException("Line size is " + lines
.size() +
104 ", requested index " + n
);
110 * Increment the line number by one. If at the last line, do nothing.
113 if (lineNumber
< lines
.size() - 1) {
119 * Increment the line number by n. If n would go past the last line,
120 * increment only to the last line.
122 * @param n the number of lines to increment by
124 public void down(final int n
) {
126 if (lineNumber
> lines
.size() - 1) {
127 lineNumber
= lines
.size() - 1;
132 * Decrement the line number by one. If at the first line, do nothing.
135 if (lineNumber
> 0) {
141 * Decrement the line number by n. If n would go past the first line,
142 * decrement only to the first line.
144 * @param n the number of lines to decrement by
146 public void up(final int n
) {
148 if (lineNumber
< 0) {
154 * Decrement the cursor by one. If at the first column, do nothing.
157 lines
.get(lineNumber
).left();
161 * Increment the cursor by one. If at the last column, do nothing.
163 public void right() {
164 lines
.get(lineNumber
).right();
168 * Go to the first column of this line.
171 lines
.get(lineNumber
).home();
175 * Go to the last column of this line.
178 lines
.get(lineNumber
).end();
182 * Delete the character under the cursor.
185 lines
.get(lineNumber
).del();
189 * Delete the character immediately preceeding the cursor.
191 public void backspace() {
192 lines
.get(lineNumber
).backspace();
196 * Replace or insert a character at the cursor, depending on overwrite
199 * @param ch the character to replace or insert
201 public void addChar(final char ch
) {
202 lines
.get(lineNumber
).addChar(ch
);
206 * Get a (shallow) copy of the list of lines.
208 * @return the list of lines
210 public List
<Line
> getLines() {
211 return new ArrayList
<Line
>(lines
);
215 * Get the number of lines.
217 * @return the number of lines
219 public int getLineCount() {
224 * Compute the maximum line length for this document.
226 * @return the number of cells needed to display the longest line
228 public int getLineLengthMax() {
230 for (Line line
: lines
) {
231 if (line
.getDisplayLength() > n
) {
232 n
= line
.getDisplayLength();
239 * Construct a new Document from an existing text string.
241 * @param str the text string
243 public Document(final String str
) {
244 String
[] rawLines
= str
.split("\n");
245 for (int i
= 0; i
< rawLines
.length
; i
++) {
246 lines
.add(new Line(rawLines
[i
]));