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.
44 public TextLine getLine(final int row) {
45 ensureMaxTextLine(row);
50 * Returns the length of the specified line.
54 * @return The length of the line.
56 public int getLineLength(final int row) {
57 if (rows.size() <= row)
59 return rows.get(row).getLength();
63 * Returns the number of lines in the page.
65 * @return The number of lines in the page.
67 public int getLinesCount() {
73 * Returns the text of the page.
75 * @return The text of the page.
77 public String getText() {
80 final StringBuilder result = new StringBuilder();
81 for (final TextLine textLine : rows) {
82 if (result.length() > 0)
84 result.append(textLine.toString());
86 return result.toString();
89 public void insertCharacter(final int row, final int col, final char value) {
90 getLine(row).insertCharacter(col, value);
93 public void insertLine(final int row, final TextLine textLine) {
94 rows.add(row, textLine);
98 * Removes empty lines from the end of the page.
100 private void pack() {
103 for (int i = rows.size() - 1; i >= 0; i--)
104 if (!rows.get(i).isEmpty()) {
109 if (newLength == rows.size())
112 rows = rows.subList(0, newLength);
116 * Removes the specified character from the page.
121 * The character number.
123 public void removeCharacter(final int row, final int col) {
124 if (rows.size() <= row)
126 getLine(row).removeCharacter(col);
130 * Removes the specified line from the page.
135 public void removeLine(final int row) {
136 if (rows.size() <= row)