2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
7 import java.util.ArrayList;
10 public class TextLine {
12 private List<Character> chars = new ArrayList<>();
17 public TextLine(final List<Character> value) {
22 public TextLine(final String value) {
26 public void addIdent(final int amount) {
30 for (int i = 0; i < amount; i++)
31 chars.add(0, new Character(' '));
34 public String copySubString(final int from, final int until) {
35 final StringBuilder result = new StringBuilder();
39 for (int i = from; i < until; i++)
40 result.append(chars.remove(from).value);
43 return result.toString();
47 public void cutFromBeginning(int charactersToCut) {
49 if (charactersToCut > chars.size())
50 charactersToCut = chars.size();
52 if (charactersToCut == 0)
55 chars = chars.subList(charactersToCut, chars.size());
58 public String cutSubString(final int from, final int until) {
59 final StringBuilder result = new StringBuilder();
61 final List<Character> reminder = new ArrayList<>();
65 for (int i = 0; i < chars.size(); i++)
66 if ((i >= from) && (i < until))
67 result.append(chars.get(i).value);
69 reminder.add(chars.get(i));
74 return result.toString();
77 public void cutUntilEnd(final int col) {
78 if (col >= chars.size())
81 chars = chars.subList(0, col);
84 private void ensureLength(final int length) {
85 while (chars.size() < length)
86 chars.add(new Character(' '));
89 public char getCharForLocation(final int col) {
91 if (col >= chars.size())
94 return chars.get(col).value;
97 public List<Character> getChars() {
101 public int getIdent() {
105 for (int i = 0; i < chars.size(); i++)
106 if (chars.get(i).hasValue())
109 throw new RuntimeException("This code shall never execute");
112 public int getLength() {
116 public TextLine getSubLine(final int from, final int until) {
117 final List<Character> result = new ArrayList<>();
119 for (int i = from; i < until; i++) {
120 if (i >= chars.size())
122 result.add(chars.get(i));
125 return new TextLine(result);
128 public String getSubString(final int from, final int until) {
129 final StringBuilder result = new StringBuilder();
131 for (int i = from; i < until; i++)
132 result.append(getCharForLocation(i));
134 return result.toString();
137 public void insertCharacter(final int col, final char value) {
139 chars.add(col, new Character(value));
143 public void insertString(final int col, final String value) {
146 for (final char c : value.toCharArray()) {
147 chars.add(col + i, new Character(c));
153 public void insertTextLine(final int col, final TextLine textLine) {
156 for (final Character c : textLine.getChars()) {
157 chars.add(col + i, c);
163 public boolean isEmpty() {
164 return chars.isEmpty();
167 private void pack() {
170 for (int i = chars.size() - 1; i >= 0; i--)
171 if (chars.get(i).hasValue()) {
176 if (newLength == chars.size())
179 chars = chars.subList(0, newLength);
182 public void removeCharacter(final int col) {
183 if (col >= chars.size())
189 public void setValue(final String string) {
191 for (final char c : string.toCharArray())
192 chars.add(new Character(c));
198 public String toString() {
199 final StringBuilder buffer = new StringBuilder();
201 for (final Character character : chars)
202 buffer.append(character.value);
204 return buffer.toString();