*/
package jexer.teditor;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
*/
private boolean overwrite = false;
+ /**
+ * If true, the document has been edited.
+ */
+ private boolean dirty = false;
+
/**
* The default color for the TEditor class.
*/
return overwrite;
}
+ /**
+ * Get the dirty value.
+ *
+ * @return true if the buffer is dirty
+ */
+ public boolean isDirty() {
+ return dirty;
+ }
+
+ /**
+ * Save contents to file.
+ *
+ * @param filename file to save to
+ * @throws IOException if a java.io operation throws
+ */
+ public void saveToFilename(final String filename) throws IOException {
+ OutputStreamWriter output = null;
+ try {
+ output = new OutputStreamWriter(new FileOutputStream(filename),
+ "UTF-8");
+
+ for (Line line: lines) {
+ output.write(line.getRawString());
+ output.write("\n");
+ }
+
+ dirty = false;
+ }
+ finally {
+ if (output != null) {
+ output.close();
+ }
+ }
+ }
+
/**
* Set the overwrite flag.
*
return lines.get(lineNumber).getCursor();
}
+ /**
+ * Set the current cursor position of the editing line. 0-based.
+ *
+ * @param cursor the new cursor position
+ */
+ public void setCursor(final int cursor) {
+ lines.get(lineNumber).setCursor(cursor);
+ }
+
/**
* Construct a new Document from an existing text string.
*
* Delete the character under the cursor.
*/
public void del() {
+ dirty = true;
lines.get(lineNumber).del();
}
* Delete the character immediately preceeding the cursor.
*/
public void backspace() {
+ dirty = true;
lines.get(lineNumber).backspace();
}
* @param ch the character to replace or insert
*/
public void addChar(final char ch) {
+ dirty = true;
if (overwrite) {
lines.get(lineNumber).replaceChar(ch);
} else {