2 * Sixth 3D engine. Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.sixth.e3d.gui.textEditorComponent;
12 import java.util.ArrayList;
13 import java.util.List;
15 public class TextLine {
17 private List<Character> chars = new ArrayList<>();
22 public TextLine(final List<Character> value) {
27 public TextLine(final String value) {
31 public void addIdent(final int amount) {
35 for (int i = 0; i < amount; i++)
36 chars.add(0, new Character(' '));
39 public String copySubString(final int from, final int until) {
40 final StringBuilder result = new StringBuilder();
44 for (int i = from; i < until; i++)
45 result.append(chars.remove(from).value);
48 return result.toString();
51 public void cutFromBeginning(int charactersToCut) {
53 if (charactersToCut > chars.size())
54 charactersToCut = chars.size();
56 if (charactersToCut == 0)
59 chars = chars.subList(charactersToCut, chars.size());
62 public String cutSubString(final int from, final int until) {
63 final StringBuilder result = new StringBuilder();
65 final List<Character> reminder = new ArrayList<>();
69 for (int i = 0; i < chars.size(); i++)
70 if ((i >= from) && (i < until))
71 result.append(chars.get(i).value);
73 reminder.add(chars.get(i));
78 return result.toString();
81 public void cutUntilEnd(final int col) {
82 if (col >= chars.size())
85 chars = chars.subList(0, col);
88 private void ensureLength(final int length) {
89 while (chars.size() < length)
90 chars.add(new Character(' '));
93 public char getCharForLocation(final int col) {
95 if (col >= chars.size())
98 return chars.get(col).value;
101 public List<Character> getChars() {
105 public int getIdent() {
109 for (int i = 0; i < chars.size(); i++)
110 if (!chars.get(i).isEmpty())
113 throw new RuntimeException("This code shall never execute");
116 public int getLength() {
120 public TextLine getSubLine(final int from, final int until) {
121 final List<Character> result = new ArrayList<>();
123 for (int i = from; i < until; i++) {
124 if (i >= chars.size())
126 result.add(chars.get(i));
129 return new TextLine(result);
132 public String getSubString(final int from, final int until) {
133 final StringBuilder result = new StringBuilder();
135 for (int i = from; i < until; i++)
136 result.append(getCharForLocation(i));
138 return result.toString();
141 public void insertCharacter(final int col, final char value) {
143 chars.add(col, new Character(value));
147 public void insertString(final int col, final String value) {
150 for (final char c : value.toCharArray()) {
151 chars.add(col + i, new Character(c));
157 public void insertTextLine(final int col, final TextLine textLine) {
160 for (final Character c : textLine.getChars()) {
161 chars.add(col + i, c);
167 public boolean isEmpty() {
168 return chars.isEmpty();
171 private void pack() {
174 for (int i = chars.size() - 1; i >= 0; i--)
175 if (!chars.get(i).isEmpty()) {
180 if (newLength == chars.size())
183 chars = chars.subList(0, newLength);
186 public void removeCharacter(final int col) {
187 if (col >= chars.size())
193 public void setValue(final String string) {
195 for (final char c : string.toCharArray())
196 chars.add(new Character(c));
202 public String toString() {
203 final StringBuilder buffer = new StringBuilder();
205 for (final Character character : chars)
206 buffer.append(character.value);
208 return buffer.toString();