2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
10 import java.util.ArrayList;
11 import java.util.List;
13 public class TextLine {
15 private List<Character> chars = new ArrayList<>();
20 public TextLine(final List<Character> value) {
25 public TextLine(final String value) {
29 public void addIdent(final int amount) {
33 for (int i = 0; i < amount; i++)
34 chars.add(0, new Character(' '));
37 public String copySubString(final int from, final int until) {
38 final StringBuilder result = new StringBuilder();
42 for (int i = from; i < until; i++)
43 result.append(chars.remove(from).value);
46 return result.toString();
49 public void cutFromBeginning(int charactersToCut) {
51 if (charactersToCut > chars.size())
52 charactersToCut = chars.size();
54 if (charactersToCut == 0)
57 chars = chars.subList(charactersToCut, chars.size());
60 public String cutSubString(final int from, final int until) {
61 final StringBuilder result = new StringBuilder();
63 final List<Character> reminder = new ArrayList<>();
67 for (int i = 0; i < chars.size(); i++)
68 if ((i >= from) && (i < until))
69 result.append(chars.get(i).value);
71 reminder.add(chars.get(i));
76 return result.toString();
79 public void cutUntilEnd(final int col) {
80 if (col >= chars.size())
83 chars = chars.subList(0, col);
86 private void ensureLength(final int length) {
87 while (chars.size() < length)
88 chars.add(new Character(' '));
91 public char getCharForLocation(final int col) {
93 if (col >= chars.size())
96 return chars.get(col).value;
99 public List<Character> getChars() {
103 public int getIdent() {
107 for (int i = 0; i < chars.size(); i++)
108 if (chars.get(i).hasValue())
111 throw new RuntimeException("This code shall never execute");
114 public int getLength() {
118 public TextLine getSubLine(final int from, final int until) {
119 final List<Character> result = new ArrayList<>();
121 for (int i = from; i < until; i++) {
122 if (i >= chars.size())
124 result.add(chars.get(i));
127 return new TextLine(result);
130 public String getSubString(final int from, final int until) {
131 final StringBuilder result = new StringBuilder();
133 for (int i = from; i < until; i++)
134 result.append(getCharForLocation(i));
136 return result.toString();
139 public void insertCharacter(final int col, final char value) {
141 chars.add(col, new Character(value));
145 public void insertString(final int col, final String value) {
148 for (final char c : value.toCharArray()) {
149 chars.add(col + i, new Character(c));
155 public void insertTextLine(final int col, final TextLine textLine) {
158 for (final Character c : textLine.getChars()) {
159 chars.add(col + i, c);
165 public boolean isEmpty() {
166 return chars.isEmpty();
169 private void pack() {
172 for (int i = chars.size() - 1; i >= 0; i--)
173 if (chars.get(i).hasValue()) {
178 if (newLength == chars.size())
181 chars = chars.subList(0, newLength);
184 public void removeCharacter(final int col) {
185 if (col >= chars.size())
191 public void setValue(final String string) {
193 for (final char c : string.toCharArray())
194 chars.add(new Character(c));
200 public String toString() {
201 final StringBuilder buffer = new StringBuilder();
203 for (final Character character : chars)
204 buffer.append(character.value);
206 return buffer.toString();