Changed license to Creative Commons Zero (CC0).
[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 *
6  */
7
8 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 public class Page {
14
15     public List<TextLine> lines = new ArrayList<>();
16
17     public void ensureMaxTextLine(final int row) {
18         while (lines.size() <= row)
19             lines.add(new TextLine());
20     }
21
22     public char getChar(final int row, final int column) {
23         if (lines.size() <= row)
24             return ' ';
25         return lines.get(row).getCharForLocation(column);
26     }
27
28     public TextLine getLine(final int row) {
29         ensureMaxTextLine(row);
30         return lines.get(row);
31     }
32
33     public int getLineLength(final int row) {
34         if (lines.size() <= row)
35             return 0;
36         return lines.get(row).getLength();
37     }
38
39     public int getLinesCount() {
40         pack();
41         return lines.size();
42     }
43
44     public String getText() {
45         pack();
46
47         final StringBuilder result = new StringBuilder();
48         for (final TextLine textLine : lines) {
49             if (result.length() > 0)
50                 result.append("\n");
51             result.append(textLine.toString());
52         }
53         return result.toString();
54     }
55
56     public void insertCharacter(final int row, final int col, final char value) {
57         getLine(row).insertCharacter(col, value);
58     }
59
60     public void insertLine(final int row, final TextLine textLine) {
61         lines.add(row, textLine);
62     }
63
64     private void pack() {
65         int newLength = 0;
66
67         for (int i = lines.size() - 1; i >= 0; i--)
68             if (!lines.get(i).isEmpty()) {
69                 newLength = i + 1;
70                 break;
71             }
72
73         if (newLength == lines.size())
74             return;
75
76         lines = lines.subList(0, newLength);
77     }
78
79     public void removeCharacter(final int row, final int col) {
80         if (lines.size() <= row)
81             return;
82         getLine(row).removeCharacter(col);
83     }
84
85     public void removeLine(final int row) {
86         if (lines.size() <= row)
87             return;
88         lines.remove(row);
89     }
90
91 }