import java.util.ArrayList;
import java.util.List;
+/**
+ * A page in a text editor.
+ */
public class Page {
+ /**
+ * The text lines.
+ */
public List<TextLine> rows = new ArrayList<>();
public void ensureMaxTextLine(final int row) {
rows.add(new TextLine());
}
+ /**
+ * Returns the character at the specified location.
+ * If the location is out of bounds, returns a space.
+ *
+ * @return The character at the specified location.
+ */
public char getChar(final int row, final int column) {
if (rows.size() <= row)
return ' ';
return rows.get(row).getCharForLocation(column);
}
+ /**
+ * Returns the specified line.
+ *
+ * @param row
+ * The line number.
+ * @return The line.
+ */
public TextLine getLine(final int row) {
ensureMaxTextLine(row);
return rows.get(row);
}
+ /**
+ * Returns the length of the specified line.
+ *
+ * @param row
+ * The line number.
+ * @return The length of the line.
+ */
public int getLineLength(final int row) {
if (rows.size() <= row)
return 0;
return rows.get(row).getLength();
}
+ /**
+ * Returns the number of lines in the page.
+ *
+ * @return The number of lines in the page.
+ */
public int getLinesCount() {
pack();
return rows.size();
}
+ /**
+ * Returns the text of the page.
+ *
+ * @return The text of the page.
+ */
public String getText() {
pack();
rows.add(row, textLine);
}
+ /**
+ * Removes empty lines from the end of the page.
+ */
private void pack() {
int newLength = 0;
rows = rows.subList(0, newLength);
}
+ /**
+ * Removes the specified character from the page.
+ *
+ * @param row
+ * The line number.
+ * @param col
+ * The character number.
+ */
public void removeCharacter(final int row, final int col) {
if (rows.size() <= row)
return;
getLine(row).removeCharacter(col);
}
+ /**
+ * Removes the specified line from the page.
+ *
+ * @param row
+ * The line number.
+ */
public void removeLine(final int row) {
if (rows.size() <= row)
return;