more TEditor stubs
[fanfix.git] / src / jexer / teditor / Document.java
CommitLineData
12b55d76
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.teditor;
30
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * A Document represents a text file, as a collection of lines.
36 */
37public class Document {
38
39 /**
40 * The list of lines.
41 */
42 private ArrayList<Line> lines = new ArrayList<Line>();
43
44 /**
45 * The current line number being edited. Note that this is 0-based, the
46 * first line is line number 0.
47 */
48 private int lineNumber = 0;
49
50 /**
51 * The overwrite flag. When true, characters overwrite data.
52 */
53 private boolean overwrite = false;
54
55 /**
56 * Get the overwrite flag.
57 *
58 * @return true if addChar() overwrites data, false if it inserts
59 */
60 public boolean getOverwrite() {
61 return overwrite;
62 }
63
64 /**
65 * Set the overwrite flag.
66 *
67 * @param overwrite true if addChar() should overwrite data, false if it
68 * should insert
69 */
70 public void setOverwrite(final boolean overwrite) {
71 this.overwrite = overwrite;
72 }
73
74 /**
75 * Get the current line number being edited.
76 *
77 * @return the line number. Note that this is 0-based: 0 is the first
78 * line.
79 */
80 public int getLineNumber() {
81 return lineNumber;
82 }
83
84 /**
85 * Get a specific line by number.
86 *
87 * @param lineNumber the line number. Note that this is 0-based: 0 is
88 * the first line.
89 * @return the line
90 */
91 public Line getLine(final int lineNumber) {
92 return lines.get(lineNumber);
93 }
94
95 /**
96 * Set the current line number being edited.
97 *
98 * @param n the line number. Note that this is 0-based: 0 is the first
99 * line.
100 */
101 public void setLineNumber(final int n) {
102 if ((n < 0) || (n > lines.size())) {
103 throw new IndexOutOfBoundsException("Line size is " + lines.size() +
104 ", requested index " + n);
105 }
106 lineNumber = n;
107 }
108
109 /**
110 * Increment the line number by one. If at the last line, do nothing.
111 */
112 public void down() {
113 if (lineNumber < lines.size() - 1) {
114 lineNumber++;
115 }
116 }
117
118 /**
119 * Increment the line number by n. If n would go past the last line,
120 * increment only to the last line.
121 *
122 * @param n the number of lines to increment by
123 */
124 public void down(final int n) {
125 lineNumber += n;
126 if (lineNumber > lines.size() - 1) {
127 lineNumber = lines.size() - 1;
128 }
129 }
130
131 /**
132 * Decrement the line number by one. If at the first line, do nothing.
133 */
134 public void up() {
135 if (lineNumber > 0) {
136 lineNumber--;
137 }
138 }
139
140 /**
141 * Decrement the line number by n. If n would go past the first line,
142 * decrement only to the first line.
143 *
144 * @param n the number of lines to decrement by
145 */
146 public void up(final int n) {
147 lineNumber -= n;
148 if (lineNumber < 0) {
149 lineNumber = 0;
150 }
151 }
152
153 /**
154 * Decrement the cursor by one. If at the first column, do nothing.
155 */
156 public void left() {
157 lines.get(lineNumber).left();
158 }
159
160 /**
161 * Increment the cursor by one. If at the last column, do nothing.
162 */
163 public void right() {
164 lines.get(lineNumber).right();
165 }
166
167 /**
168 * Go to the first column of this line.
169 */
170 public void home() {
171 lines.get(lineNumber).home();
172 }
173
174 /**
175 * Go to the last column of this line.
176 */
177 public void end() {
178 lines.get(lineNumber).end();
179 }
180
181 /**
182 * Delete the character under the cursor.
183 */
184 public void del() {
185 lines.get(lineNumber).del();
186 }
187
188 /**
189 * Delete the character immediately preceeding the cursor.
190 */
191 public void backspace() {
192 lines.get(lineNumber).backspace();
193 }
194
195 /**
196 * Replace or insert a character at the cursor, depending on overwrite
197 * flag.
198 *
199 * @param ch the character to replace or insert
200 */
201 public void addChar(final char ch) {
202 lines.get(lineNumber).addChar(ch);
203 }
204
205 /**
206 * Get a (shallow) copy of the list of lines.
207 *
208 * @return the list of lines
209 */
210 public List<Line> getLines() {
211 return new ArrayList<Line>(lines);
212 }
213
214 /**
215 * Get the number of lines.
216 *
217 * @return the number of lines
218 */
219 public int getLineCount() {
220 return lines.size();
221 }
222
223 /**
224 * Compute the maximum line length for this document.
225 *
226 * @return the number of cells needed to display the longest line
227 */
228 public int getLineLengthMax() {
229 int n = 0;
230 for (Line line : lines) {
231 if (line.getDisplayLength() > n) {
232 n = line.getDisplayLength();
233 }
234 }
235 return n;
236 }
237
238 /**
239 * Construct a new Document from an existing text string.
240 *
241 * @param str the text string
242 */
243 public Document(final String str) {
244 String [] rawLines = str.split("\n");
245 for (int i = 0; i < rawLines.length; i++) {
246 lines.add(new Line(rawLines[i]));
247 }
248 }
249
250}