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
.io
.FileOutputStream
;
32 import java
.io
.IOException
;
33 import java
.io
.OutputStreamWriter
;
34 import java
.util
.ArrayList
;
35 import java
.util
.List
;
37 import jexer
.bits
.CellAttributes
;
40 * A Document represents a text file, as a collection of lines.
42 public class Document
{
47 private ArrayList
<Line
> lines
= new ArrayList
<Line
>();
50 * The current line number being edited. Note that this is 0-based, the
51 * first line is line number 0.
53 private int lineNumber
= 0;
56 * The overwrite flag. When true, characters overwrite data.
58 private boolean overwrite
= false;
61 * If true, the document has been edited.
63 private boolean dirty
= false;
66 * The default color for the TEditor class.
68 private CellAttributes defaultColor
= null;
71 * The text highlighter to use.
73 private Highlighter highlighter
= new Highlighter();
76 * Get the overwrite flag.
78 * @return true if addChar() overwrites data, false if it inserts
80 public boolean getOverwrite() {
85 * Get the dirty value.
87 * @return true if the buffer is dirty
89 public boolean isDirty() {
94 * Save contents to file.
96 * @param filename file to save to
97 * @throws IOException if a java.io operation throws
99 public void saveToFilename(final String filename
) throws IOException
{
100 OutputStreamWriter output
= null;
102 output
= new OutputStreamWriter(new FileOutputStream(filename
),
105 for (Line line
: lines
) {
106 output
.write(line
.getRawString());
113 if (output
!= null) {
120 * Set the overwrite flag.
122 * @param overwrite true if addChar() should overwrite data, false if it
125 public void setOverwrite(final boolean overwrite
) {
126 this.overwrite
= overwrite
;
130 * Get the current line number being edited.
132 * @return the line number. Note that this is 0-based: 0 is the first
135 public int getLineNumber() {
140 * Get the current editing line.
144 public Line
getCurrentLine() {
145 return lines
.get(lineNumber
);
149 * Get a specific line by number.
151 * @param lineNumber the line number. Note that this is 0-based: 0 is
155 public Line
getLine(final int lineNumber
) {
156 return lines
.get(lineNumber
);
160 * Set the current line number being edited.
162 * @param n the line number. Note that this is 0-based: 0 is the first
165 public void setLineNumber(final int n
) {
166 if ((n
< 0) || (n
> lines
.size())) {
167 throw new IndexOutOfBoundsException("Lines array size is " +
168 lines
.size() + ", requested index " + n
);
174 * Get the current cursor position of the editing line.
176 * @return the cursor position
178 public int getCursor() {
179 return lines
.get(lineNumber
).getCursor();
183 * Set the current cursor position of the editing line. 0-based.
185 * @param cursor the new cursor position
187 public void setCursor(final int cursor
) {
188 lines
.get(lineNumber
).setCursor(cursor
);
192 * Construct a new Document from an existing text string.
194 * @param str the text string
195 * @param defaultColor the color for unhighlighted text
197 public Document(final String str
, final CellAttributes defaultColor
) {
198 this.defaultColor
= defaultColor
;
200 // TODO: set different colors based on file extension
201 highlighter
.setJavaColors();
203 String
[] rawLines
= str
.split("\n");
204 for (int i
= 0; i
< rawLines
.length
; i
++) {
205 lines
.add(new Line(rawLines
[i
], this.defaultColor
, highlighter
));
210 * Increment the line number by one. If at the last line, do nothing.
212 * @return true if the editing line changed
214 public boolean down() {
215 if (lineNumber
< lines
.size() - 1) {
216 int x
= lines
.get(lineNumber
).getCursor();
218 if (x
> lines
.get(lineNumber
).getDisplayLength()) {
219 lines
.get(lineNumber
).end();
221 lines
.get(lineNumber
).setCursor(x
);
229 * Increment the line number by n. If n would go past the last line,
230 * increment only to the last line.
232 * @param n the number of lines to increment by
233 * @return true if the editing line changed
235 public boolean down(final int n
) {
236 if (lineNumber
< lines
.size() - 1) {
237 int x
= lines
.get(lineNumber
).getCursor();
239 if (lineNumber
> lines
.size() - 1) {
240 lineNumber
= lines
.size() - 1;
242 if (x
> lines
.get(lineNumber
).getDisplayLength()) {
243 lines
.get(lineNumber
).end();
245 lines
.get(lineNumber
).setCursor(x
);
253 * Decrement the line number by one. If at the first line, do nothing.
255 * @return true if the editing line changed
257 public boolean up() {
258 if (lineNumber
> 0) {
259 int x
= lines
.get(lineNumber
).getCursor();
261 if (x
> lines
.get(lineNumber
).getDisplayLength()) {
262 lines
.get(lineNumber
).end();
264 lines
.get(lineNumber
).setCursor(x
);
272 * Decrement the line number by n. If n would go past the first line,
273 * decrement only to the first line.
275 * @param n the number of lines to decrement by
276 * @return true if the editing line changed
278 public boolean up(final int n
) {
279 if (lineNumber
> 0) {
280 int x
= lines
.get(lineNumber
).getCursor();
282 if (lineNumber
< 0) {
285 if (x
> lines
.get(lineNumber
).getDisplayLength()) {
286 lines
.get(lineNumber
).end();
288 lines
.get(lineNumber
).setCursor(x
);
296 * Decrement the cursor by one. If at the first column, do nothing.
298 * @return true if the cursor position changed
300 public boolean left() {
301 return lines
.get(lineNumber
).left();
305 * Increment the cursor by one. If at the last column, do nothing.
307 * @return true if the cursor position changed
309 public boolean right() {
310 return lines
.get(lineNumber
).right();
314 * Go to the first column of this line.
316 * @return true if the cursor position changed
318 public boolean home() {
319 return lines
.get(lineNumber
).home();
323 * Go to the last column of this line.
325 * @return true if the cursor position changed
327 public boolean end() {
328 return lines
.get(lineNumber
).end();
332 * Delete the character under the cursor.
336 lines
.get(lineNumber
).del();
340 * Delete the character immediately preceeding the cursor.
342 public void backspace() {
344 lines
.get(lineNumber
).backspace();
348 * Replace or insert a character at the cursor, depending on overwrite
351 * @param ch the character to replace or insert
353 public void addChar(final char ch
) {
356 lines
.get(lineNumber
).replaceChar(ch
);
358 lines
.get(lineNumber
).addChar(ch
);
363 * Get a (shallow) copy of the list of lines.
365 * @return the list of lines
367 public List
<Line
> getLines() {
368 return new ArrayList
<Line
>(lines
);
372 * Get the number of lines.
374 * @return the number of lines
376 public int getLineCount() {
381 * Compute the maximum line length for this document.
383 * @return the number of cells needed to display the longest line
385 public int getLineLengthMax() {
387 for (Line line
: lines
) {
388 if (line
.getDisplayLength() > n
) {
389 n
= line
.getDisplayLength();