2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
7 import java.util.ArrayList;
11 * A page in a text editor.
18 public List<TextLine> rows = new ArrayList<>();
20 public void ensureMaxTextLine(final int row) {
21 while (rows.size() <= row)
22 rows.add(new TextLine());
26 * Returns the character at the specified location.
27 * If the location is out of bounds, returns a space.
29 * @return The character at the specified location.
31 public char getChar(final int row, final int column) {
32 if (rows.size() <= row)
34 return rows.get(row).getCharForLocation(column);
38 * Returns the specified line.
40 * @param row The line number.
43 public TextLine getLine(final int row) {
44 ensureMaxTextLine(row);
49 * Returns the length of the specified line.
51 * @param row The line number.
52 * @return The length of the line.
54 public int getLineLength(final int row) {
55 if (rows.size() <= row)
57 return rows.get(row).getLength();
61 * Returns the number of lines in the page.
63 * @return The number of lines in the page.
65 public int getLinesCount() {
71 * Returns the text of the page.
73 * @return The text of the page.
75 public String getText() {
78 final StringBuilder result = new StringBuilder();
79 for (final TextLine textLine : rows) {
80 if (result.length() > 0)
82 result.append(textLine.toString());
84 return result.toString();
87 public void insertCharacter(final int row, final int col, final char value) {
88 getLine(row).insertCharacter(col, value);
91 public void insertLine(final int row, final TextLine textLine) {
92 rows.add(row, textLine);
96 * Removes empty lines from the end of the page.
101 for (int i = rows.size() - 1; i >= 0; i--)
102 if (!rows.get(i).isEmpty()) {
107 if (newLength == rows.size())
110 rows = rows.subList(0, newLength);
114 * Removes the specified character from the page.
116 * @param row The line number.
117 * @param col The character number.
119 public void removeCharacter(final int row, final int col) {
120 if (rows.size() <= row)
122 getLine(row).removeCharacter(col);
126 * Removes the specified line from the page.
128 * @param row The line number.
130 public void removeLine(final int row) {
131 if (rows.size() <= row)