0f606186bb5882d1a9a821ec01ed5a85fb8c17e4
[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
41      *            The line number.
42      * @return The line.
43      */
44     public TextLine getLine(final int row) {
45         ensureMaxTextLine(row);
46         return rows.get(row);
47     }
48
49     /**
50      * Returns the length of the specified line.
51      *
52      * @param row
53      *            The line number.
54      * @return The length of the line.
55      */
56     public int getLineLength(final int row) {
57         if (rows.size() <= row)
58             return 0;
59         return rows.get(row).getLength();
60     }
61
62     /**
63      * Returns the number of lines in the page.
64      *
65      * @return The number of lines in the page.
66      */
67     public int getLinesCount() {
68         pack();
69         return rows.size();
70     }
71
72     /**
73      * Returns the text of the page.
74      *
75      * @return The text of the page.
76      */
77     public String getText() {
78         pack();
79
80         final StringBuilder result = new StringBuilder();
81         for (final TextLine textLine : rows) {
82             if (result.length() > 0)
83                 result.append("\n");
84             result.append(textLine.toString());
85         }
86         return result.toString();
87     }
88
89     public void insertCharacter(final int row, final int col, final char value) {
90         getLine(row).insertCharacter(col, value);
91     }
92
93     public void insertLine(final int row, final TextLine textLine) {
94         rows.add(row, textLine);
95     }
96
97     /**
98      * Removes empty lines from the end of the page.
99      */
100     private void pack() {
101         int newLength = 0;
102
103         for (int i = rows.size() - 1; i >= 0; i--)
104             if (!rows.get(i).isEmpty()) {
105                 newLength = i + 1;
106                 break;
107             }
108
109         if (newLength == rows.size())
110             return;
111
112         rows = rows.subList(0, newLength);
113     }
114
115     /**
116      * Removes the specified character from the page.
117      *
118      * @param row
119      *            The line number.
120      * @param col
121      *            The character number.
122      */
123     public void removeCharacter(final int row, final int col) {
124         if (rows.size() <= row)
125             return;
126         getLine(row).removeCharacter(col);
127     }
128
129     /**
130      * Removes the specified line from the page.
131      *
132      * @param row
133      *            The line number.
134      */
135     public void removeLine(final int row) {
136         if (rows.size() <= row)
137             return;
138         rows.remove(row);
139     }
140
141 }