2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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
{
44 // ------------------------------------------------------------------------
45 // Variables --------------------------------------------------------------
46 // ------------------------------------------------------------------------
51 private ArrayList
<Line
> lines
= new ArrayList
<Line
>();
54 * The current line number being edited. Note that this is 0-based, the
55 * first line is line number 0.
57 private int lineNumber
= 0;
60 * The overwrite flag. When true, characters overwrite data.
62 private boolean overwrite
= false;
65 * If true, the document has been edited.
67 private boolean dirty
= false;
70 * The default color for the TEditor class.
72 private CellAttributes defaultColor
= null;
75 * The text highlighter to use.
77 private Highlighter highlighter
= new Highlighter();
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Construct a new Document from an existing text string.
86 * @param str the text string
87 * @param defaultColor the color for unhighlighted text
89 public Document(final String str
, final CellAttributes defaultColor
) {
90 this.defaultColor
= defaultColor
;
92 // TODO: set different colors based on file extension
93 highlighter
.setJavaColors();
95 String
[] rawLines
= str
.split("\n");
96 for (int i
= 0; i
< rawLines
.length
; i
++) {
97 lines
.add(new Line(rawLines
[i
], this.defaultColor
, highlighter
));
101 // ------------------------------------------------------------------------
102 // Document ---------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Get the overwrite flag.
108 * @return true if addChar() overwrites data, false if it inserts
110 public boolean getOverwrite() {
115 * Get the dirty value.
117 * @return true if the buffer is dirty
119 public boolean isDirty() {
124 * Save contents to file.
126 * @param filename file to save to
127 * @throws IOException if a java.io operation throws
129 public void saveToFilename(final String filename
) throws IOException
{
130 OutputStreamWriter output
= null;
132 output
= new OutputStreamWriter(new FileOutputStream(filename
),
135 for (Line line
: lines
) {
136 output
.write(line
.getRawString());
143 if (output
!= null) {
150 * Set the overwrite flag.
152 * @param overwrite true if addChar() should overwrite data, false if it
155 public void setOverwrite(final boolean overwrite
) {
156 this.overwrite
= overwrite
;
160 * Get the current line number being edited.
162 * @return the line number. Note that this is 0-based: 0 is the first
165 public int getLineNumber() {
170 * Get the current editing line.
174 public Line
getCurrentLine() {
175 return lines
.get(lineNumber
);
179 * Get a specific line by number.
181 * @param lineNumber the line number. Note that this is 0-based: 0 is
185 public Line
getLine(final int lineNumber
) {
186 return lines
.get(lineNumber
);
190 * Set the current line number being edited.
192 * @param n the line number. Note that this is 0-based: 0 is the first
195 public void setLineNumber(final int n
) {
196 if ((n
< 0) || (n
> lines
.size())) {
197 throw new IndexOutOfBoundsException("Lines array size is " +
198 lines
.size() + ", requested index " + n
);
204 * Get the current cursor position of the editing line.
206 * @return the cursor position
208 public int getCursor() {
209 return lines
.get(lineNumber
).getCursor();
213 * Set the current cursor position of the editing line. 0-based.
215 * @param cursor the new cursor position
217 public void setCursor(final int cursor
) {
218 if (cursor
>= lines
.get(lineNumber
).getDisplayLength()) {
219 lines
.get(lineNumber
).end();
221 lines
.get(lineNumber
).setCursor(cursor
);
226 * Increment the line number by one. If at the last line, do nothing.
228 * @return true if the editing line changed
230 public boolean down() {
231 if (lineNumber
< lines
.size() - 1) {
232 int x
= lines
.get(lineNumber
).getCursor();
234 if (x
>= lines
.get(lineNumber
).getDisplayLength()) {
235 lines
.get(lineNumber
).end();
237 lines
.get(lineNumber
).setCursor(x
);
245 * Increment the line number by n. If n would go past the last line,
246 * increment only to the last line.
248 * @param n the number of lines to increment by
249 * @return true if the editing line changed
251 public boolean down(final int n
) {
252 if (lineNumber
< lines
.size() - 1) {
253 int x
= lines
.get(lineNumber
).getCursor();
255 if (lineNumber
> lines
.size() - 1) {
256 lineNumber
= lines
.size() - 1;
258 if (x
>= lines
.get(lineNumber
).getDisplayLength()) {
259 lines
.get(lineNumber
).end();
261 lines
.get(lineNumber
).setCursor(x
);
269 * Decrement the line number by one. If at the first line, do nothing.
271 * @return true if the editing line changed
273 public boolean up() {
274 if (lineNumber
> 0) {
275 int x
= lines
.get(lineNumber
).getCursor();
277 if (x
>= lines
.get(lineNumber
).getDisplayLength()) {
278 lines
.get(lineNumber
).end();
280 lines
.get(lineNumber
).setCursor(x
);
288 * Decrement the line number by n. If n would go past the first line,
289 * decrement only to the first line.
291 * @param n the number of lines to decrement by
292 * @return true if the editing line changed
294 public boolean up(final int n
) {
295 if (lineNumber
> 0) {
296 int x
= lines
.get(lineNumber
).getCursor();
298 if (lineNumber
< 0) {
301 if (x
>= lines
.get(lineNumber
).getDisplayLength()) {
302 lines
.get(lineNumber
).end();
304 lines
.get(lineNumber
).setCursor(x
);
312 * Decrement the cursor by one. If at the first column, do nothing.
314 * @return true if the cursor position changed
316 public boolean left() {
317 if (!lines
.get(lineNumber
).left()) {
318 // We are on the leftmost column, wrap
329 * Increment the cursor by one. If at the last column, do nothing.
331 * @return true if the cursor position changed
333 public boolean right() {
334 if (!lines
.get(lineNumber
).right()) {
335 // We are on the rightmost column, wrap
346 * Go to the first column of this line.
348 * @return true if the cursor position changed
350 public boolean home() {
351 return lines
.get(lineNumber
).home();
355 * Go to the last column of this line.
357 * @return true if the cursor position changed
359 public boolean end() {
360 return lines
.get(lineNumber
).end();
364 * Delete the character under the cursor.
368 int cursor
= lines
.get(lineNumber
).getCursor();
369 if (cursor
< lines
.get(lineNumber
).getDisplayLength() - 1) {
370 lines
.get(lineNumber
).del();
371 } else if (lineNumber
< lines
.size() - 2) {
373 StringBuilder newLine
= new StringBuilder(lines
.
374 get(lineNumber
).getRawString());
375 newLine
.append(lines
.get(lineNumber
+ 1).getRawString());
376 lines
.set(lineNumber
, new Line(newLine
.toString(),
377 defaultColor
, highlighter
));
378 lines
.get(lineNumber
).setCursor(cursor
);
379 lines
.remove(lineNumber
+ 1);
384 * Delete the character immediately preceeding the cursor.
386 public void backspace() {
388 int cursor
= lines
.get(lineNumber
).getCursor();
390 lines
.get(lineNumber
).backspace();
391 } else if (lineNumber
> 0) {
394 String firstLine
= lines
.get(lineNumber
).getRawString();
395 if (firstLine
.length() > 0) {
396 // Backspacing combining two lines
397 StringBuilder newLine
= new StringBuilder(firstLine
);
398 newLine
.append(lines
.get(lineNumber
+ 1).getRawString());
399 lines
.set(lineNumber
, new Line(newLine
.toString(),
400 defaultColor
, highlighter
));
401 lines
.get(lineNumber
).setCursor(firstLine
.length());
402 lines
.remove(lineNumber
+ 1);
404 // Backspacing an empty line
405 lines
.remove(lineNumber
);
406 lines
.get(lineNumber
).setCursor(0);
412 * Split the current line into two, like pressing the enter key.
414 public void enter() {
416 int cursor
= lines
.get(lineNumber
).getRawCursor();
417 String original
= lines
.get(lineNumber
).getRawString();
418 String firstLine
= original
.substring(0, cursor
);
419 String secondLine
= original
.substring(cursor
);
420 lines
.add(lineNumber
+ 1, new Line(secondLine
, defaultColor
,
422 lines
.set(lineNumber
, new Line(firstLine
, defaultColor
, highlighter
));
424 lines
.get(lineNumber
).home();
428 * Replace or insert a character at the cursor, depending on overwrite
431 * @param ch the character to replace or insert
433 public void addChar(final int ch
) {
436 lines
.get(lineNumber
).replaceChar(ch
);
438 lines
.get(lineNumber
).addChar(ch
);
443 * Get a (shallow) copy of the list of lines.
445 * @return the list of lines
447 public List
<Line
> getLines() {
448 return new ArrayList
<Line
>(lines
);
452 * Get the number of lines.
454 * @return the number of lines
456 public int getLineCount() {
461 * Compute the maximum line length for this document.
463 * @return the number of cells needed to display the longest line
465 public int getLineLengthMax() {
467 for (Line line
: lines
) {
468 if (line
.getDisplayLength() > n
) {
469 n
= line
.getDisplayLength();
476 * Get the current line length.
478 * @return the number of cells needed to display the current line
480 public int getLineLength() {
481 return lines
.get(lineNumber
).getDisplayLength();