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