Updated readability of the code.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / textEditorComponent / Page.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 /**
11  * A page in a text editor.
12  */
13 public class Page {
14
15     /**
16      * The text lines.
17      */
18     public List<TextLine> rows = new ArrayList<>();
19
20     public void ensureMaxTextLine(final int row) {
21         while (rows.size() <= row)
22             rows.add(new TextLine());
23     }
24
25     /**
26      * Returns the character at the specified location.
27      * If the location is out of bounds, returns a space.
28      *
29      * @return The character at the specified location.
30      */
31     public char getChar(final int row, final int column) {
32         if (rows.size() <= row)
33             return ' ';
34         return rows.get(row).getCharForLocation(column);
35     }
36
37     /**
38      * Returns the specified line.
39      *
40      * @param row The line number.
41      * @return The line.
42      */
43     public TextLine getLine(final int row) {
44         ensureMaxTextLine(row);
45         return rows.get(row);
46     }
47
48     /**
49      * Returns the length of the specified line.
50      *
51      * @param row The line number.
52      * @return The length of the line.
53      */
54     public int getLineLength(final int row) {
55         if (rows.size() <= row)
56             return 0;
57         return rows.get(row).getLength();
58     }
59
60     /**
61      * Returns the number of lines in the page.
62      *
63      * @return The number of lines in the page.
64      */
65     public int getLinesCount() {
66         pack();
67         return rows.size();
68     }
69
70     /**
71      * Returns the text of the page.
72      *
73      * @return The text of the page.
74      */
75     public String getText() {
76         pack();
77
78         final StringBuilder result = new StringBuilder();
79         for (final TextLine textLine : rows) {
80             if (result.length() > 0)
81                 result.append("\n");
82             result.append(textLine.toString());
83         }
84         return result.toString();
85     }
86
87     public void insertCharacter(final int row, final int col, final char value) {
88         getLine(row).insertCharacter(col, value);
89     }
90
91     public void insertLine(final int row, final TextLine textLine) {
92         rows.add(row, textLine);
93     }
94
95     /**
96      * Removes empty lines from the end of the page.
97      */
98     private void pack() {
99         int newLength = 0;
100
101         for (int i = rows.size() - 1; i >= 0; i--)
102             if (!rows.get(i).isEmpty()) {
103                 newLength = i + 1;
104                 break;
105             }
106
107         if (newLength == rows.size())
108             return;
109
110         rows = rows.subList(0, newLength);
111     }
112
113     /**
114      * Removes the specified character from the page.
115      *
116      * @param row The line number.
117      * @param col The character number.
118      */
119     public void removeCharacter(final int row, final int col) {
120         if (rows.size() <= row)
121             return;
122         getLine(row).removeCharacter(col);
123     }
124
125     /**
126      * Removes the specified line from the page.
127      *
128      * @param row The line number.
129      */
130     public void removeLine(final int row) {
131         if (rows.size() <= row)
132             return;
133         rows.remove(row);
134     }
135
136 }