import java.util.ArrayList;
import java.util.List;
+import jexer.bits.CellAttributes;
+
/**
* A Document represents a text file, as a collection of lines.
*/
*/
private boolean overwrite = false;
+ /**
+ * The default color for the TEditor class.
+ */
+ private CellAttributes defaultColor = null;
+
+ /**
+ * The text highlighter to use.
+ */
+ private Highlighter highlighter = new Highlighter();
+
/**
* Get the overwrite flag.
*
return lineNumber;
}
+ /**
+ * Get the current editing line.
+ *
+ * @return the line
+ */
+ public Line getCurrentLine() {
+ return lines.get(lineNumber);
+ }
+
/**
* Get a specific line by number.
*
*/
public void setLineNumber(final int n) {
if ((n < 0) || (n > lines.size())) {
- throw new IndexOutOfBoundsException("Line size is " + lines.size() +
- ", requested index " + n);
+ throw new IndexOutOfBoundsException("Lines array size is " +
+ lines.size() + ", requested index " + n);
}
lineNumber = n;
}
+ /**
+ * Get the current cursor position of the editing line.
+ *
+ * @return the cursor position
+ */
+ public int getCursor() {
+ return lines.get(lineNumber).getCursor();
+ }
+
+ /**
+ * Construct a new Document from an existing text string.
+ *
+ * @param str the text string
+ * @param defaultColor the color for unhighlighted text
+ */
+ public Document(final String str, final CellAttributes defaultColor) {
+ this.defaultColor = defaultColor;
+
+ // TODO: set different colors based on file extension
+ highlighter.setJavaColors();
+
+ String [] rawLines = str.split("\n");
+ for (int i = 0; i < rawLines.length; i++) {
+ lines.add(new Line(rawLines[i], this.defaultColor, highlighter));
+ }
+ }
+
/**
* Increment the line number by one. If at the last line, do nothing.
+ *
+ * @return true if the editing line changed
*/
- public void down() {
+ public boolean down() {
if (lineNumber < lines.size() - 1) {
+ int x = lines.get(lineNumber).getCursor();
lineNumber++;
+ if (x > lines.get(lineNumber).getDisplayLength()) {
+ lines.get(lineNumber).end();
+ } else {
+ lines.get(lineNumber).setCursor(x);
+ }
+ return true;
}
+ return false;
}
/**
* increment only to the last line.
*
* @param n the number of lines to increment by
+ * @return true if the editing line changed
*/
- public void down(final int n) {
- lineNumber += n;
- if (lineNumber > lines.size() - 1) {
- lineNumber = lines.size() - 1;
+ public boolean down(final int n) {
+ if (lineNumber < lines.size() - 1) {
+ int x = lines.get(lineNumber).getCursor();
+ lineNumber += n;
+ if (lineNumber > lines.size() - 1) {
+ lineNumber = lines.size() - 1;
+ }
+ if (x > lines.get(lineNumber).getDisplayLength()) {
+ lines.get(lineNumber).end();
+ } else {
+ lines.get(lineNumber).setCursor(x);
+ }
+ return true;
}
+ return false;
}
/**
* Decrement the line number by one. If at the first line, do nothing.
+ *
+ * @return true if the editing line changed
*/
- public void up() {
+ public boolean up() {
if (lineNumber > 0) {
+ int x = lines.get(lineNumber).getCursor();
lineNumber--;
+ if (x > lines.get(lineNumber).getDisplayLength()) {
+ lines.get(lineNumber).end();
+ } else {
+ lines.get(lineNumber).setCursor(x);
+ }
+ return true;
}
+ return false;
}
/**
* decrement only to the first line.
*
* @param n the number of lines to decrement by
+ * @return true if the editing line changed
*/
- public void up(final int n) {
- lineNumber -= n;
- if (lineNumber < 0) {
- lineNumber = 0;
+ public boolean up(final int n) {
+ if (lineNumber > 0) {
+ int x = lines.get(lineNumber).getCursor();
+ lineNumber -= n;
+ if (lineNumber < 0) {
+ lineNumber = 0;
+ }
+ if (x > lines.get(lineNumber).getDisplayLength()) {
+ lines.get(lineNumber).end();
+ } else {
+ lines.get(lineNumber).setCursor(x);
+ }
+ return true;
}
+ return false;
}
/**
* Decrement the cursor by one. If at the first column, do nothing.
+ *
+ * @return true if the cursor position changed
*/
- public void left() {
- lines.get(lineNumber).left();
+ public boolean left() {
+ return lines.get(lineNumber).left();
}
/**
* Increment the cursor by one. If at the last column, do nothing.
+ *
+ * @return true if the cursor position changed
*/
- public void right() {
- lines.get(lineNumber).right();
+ public boolean right() {
+ return lines.get(lineNumber).right();
}
/**
* Go to the first column of this line.
+ *
+ * @return true if the cursor position changed
*/
- public void home() {
- lines.get(lineNumber).home();
+ public boolean home() {
+ return lines.get(lineNumber).home();
}
/**
* Go to the last column of this line.
+ *
+ * @return true if the cursor position changed
*/
- public void end() {
- lines.get(lineNumber).end();
+ public boolean end() {
+ return lines.get(lineNumber).end();
}
/**
* @param ch the character to replace or insert
*/
public void addChar(final char ch) {
- lines.get(lineNumber).addChar(ch);
+ if (overwrite) {
+ lines.get(lineNumber).replaceChar(ch);
+ } else {
+ lines.get(lineNumber).addChar(ch);
+ }
}
/**
return n;
}
- /**
- * Construct a new Document from an existing text string.
- *
- * @param str the text string
- */
- public Document(final String str) {
- String [] rawLines = str.split("\n");
- for (int i = 0; i < rawLines.length; i++) {
- lines.add(new Line(rawLines[i]));
- }
- }
-
}