a3ac7670c702677efa04a0c2587229bebdccb764
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / textEditorComponent / Page.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 public class Page {
16
17     public List<TextLine> lines = new ArrayList<>();
18
19     public void ensureMaxTextLine(final int row) {
20         while (lines.size() <= row)
21             lines.add(new TextLine());
22     }
23
24     public char getChar(final int row, final int column) {
25         if (lines.size() <= row)
26             return ' ';
27         return lines.get(row).getCharForLocation(column);
28     }
29
30     public TextLine getLine(final int row) {
31         ensureMaxTextLine(row);
32         return lines.get(row);
33     }
34
35     public int getLineLength(final int row) {
36         if (lines.size() <= row)
37             return 0;
38         return lines.get(row).getLength();
39     }
40
41     public int getLinesCount() {
42         pack();
43         return lines.size();
44     }
45
46     public String getText() {
47         pack();
48
49         final StringBuilder result = new StringBuilder();
50         for (final TextLine textLine : lines) {
51             if (result.length() > 0)
52                 result.append("\n");
53             result.append(textLine.toString());
54         }
55         return result.toString();
56     }
57
58     public void insertCharacter(final int row, final int col, final char value) {
59         getLine(row).insertCharacter(col, value);
60     }
61
62     public void insertLine(final int row, final TextLine textLine) {
63         lines.add(row, textLine);
64     }
65
66     private void pack() {
67         int newLength = 0;
68
69         for (int i = lines.size() - 1; i >= 0; i--)
70             if (!lines.get(i).isEmpty()) {
71                 newLength = i + 1;
72                 break;
73             }
74
75         if (newLength == lines.size())
76             return;
77
78         lines = lines.subList(0, newLength);
79     }
80
81     public void removeCharacter(final int row, final int col) {
82         if (lines.size() <= row)
83             return;
84         getLine(row).removeCharacter(col);
85     }
86
87     public void removeLine(final int row) {
88         if (lines.size() <= row)
89             return;
90         lines.remove(row);
91     }
92
93 }