Code cleanup and formatting.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / textEditorComponent / TextLine.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 TextLine {
16
17     private List<Character> chars = new ArrayList<>();
18
19     public TextLine() {
20     }
21
22     public TextLine(final List<Character> value) {
23         chars = value;
24         pack();
25     }
26
27     public TextLine(final String value) {
28         setValue(value);
29     }
30
31     public void addIdent(final int amount) {
32         if (isEmpty())
33             return;
34
35         for (int i = 0; i < amount; i++)
36             chars.add(0, new Character(' '));
37     }
38
39     public String copySubString(final int from, final int until) {
40         final StringBuilder result = new StringBuilder();
41
42         ensureLength(until);
43
44         for (int i = from; i < until; i++)
45             result.append(chars.remove(from).value);
46
47         pack();
48         return result.toString();
49     }
50
51     public void cutFromBeginning(int charactersToCut) {
52
53         if (charactersToCut > chars.size())
54             charactersToCut = chars.size();
55
56         if (charactersToCut == 0)
57             return;
58
59         chars = chars.subList(charactersToCut, chars.size());
60     }
61
62     public String cutSubString(final int from, final int until) {
63         final StringBuilder result = new StringBuilder();
64
65         final List<Character> reminder = new ArrayList<>();
66
67         ensureLength(until);
68
69         for (int i = 0; i < chars.size(); i++)
70             if ((i >= from) && (i < until))
71                 result.append(chars.get(i).value);
72             else
73                 reminder.add(chars.get(i));
74
75         chars = reminder;
76
77         pack();
78         return result.toString();
79     }
80
81     public void cutUntilEnd(final int col) {
82         if (col >= chars.size())
83             return;
84
85         chars = chars.subList(0, col);
86     }
87
88     private void ensureLength(final int length) {
89         while (chars.size() < length)
90             chars.add(new Character(' '));
91     }
92
93     public char getCharForLocation(final int col) {
94
95         if (col >= chars.size())
96             return ' ';
97
98         return chars.get(col).value;
99     }
100
101     public List<Character> getChars() {
102         return chars;
103     }
104
105     public int getIdent() {
106         if (isEmpty())
107             return 0;
108
109         for (int i = 0; i < chars.size(); i++)
110             if (chars.get(i).hasValue())
111                 return i;
112
113         throw new RuntimeException("This code shall never execute");
114     }
115
116     public int getLength() {
117         return chars.size();
118     }
119
120     public TextLine getSubLine(final int from, final int until) {
121         final List<Character> result = new ArrayList<>();
122
123         for (int i = from; i < until; i++) {
124             if (i >= chars.size())
125                 break;
126             result.add(chars.get(i));
127         }
128
129         return new TextLine(result);
130     }
131
132     public String getSubString(final int from, final int until) {
133         final StringBuilder result = new StringBuilder();
134
135         for (int i = from; i < until; i++)
136             result.append(getCharForLocation(i));
137
138         return result.toString();
139     }
140
141     public void insertCharacter(final int col, final char value) {
142         ensureLength(col);
143         chars.add(col, new Character(value));
144         pack();
145     }
146
147     public void insertString(final int col, final String value) {
148         ensureLength(col);
149         int i = 0;
150         for (final char c : value.toCharArray()) {
151             chars.add(col + i, new Character(c));
152             i++;
153         }
154         pack();
155     }
156
157     public void insertTextLine(final int col, final TextLine textLine) {
158         ensureLength(col);
159         int i = 0;
160         for (final Character c : textLine.getChars()) {
161             chars.add(col + i, c);
162             i++;
163         }
164         pack();
165     }
166
167     public boolean isEmpty() {
168         return chars.isEmpty();
169     }
170
171     private void pack() {
172         int newLength = 0;
173
174         for (int i = chars.size() - 1; i >= 0; i--)
175             if (chars.get(i).hasValue()) {
176                 newLength = i + 1;
177                 break;
178             }
179
180         if (newLength == chars.size())
181             return;
182
183         chars = chars.subList(0, newLength);
184     }
185
186     public void removeCharacter(final int col) {
187         if (col >= chars.size())
188             return;
189
190         chars.remove(col);
191     }
192
193     public void setValue(final String string) {
194         chars.clear();
195         for (final char c : string.toCharArray())
196             chars.add(new Character(c));
197
198         pack();
199     }
200
201     @Override
202     public String toString() {
203         final StringBuilder buffer = new StringBuilder();
204
205         for (final Character character : chars)
206             buffer.append(character.value);
207
208         return buffer.toString();
209     }
210
211 }