X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fteditor%2FDocument.java;h=42082e55ad21a670b31a91e8ba6e9b7e0950344e;hb=71a389c9810382e014682dde52e94d3f34e385fa;hp=5b7050fe22efd3963ca762256e163ba30c39a87b;hpb=e8a11f986bfe2556e450d7b8ad6ef0059b369bbc;p=fanfix.git diff --git a/src/jexer/teditor/Document.java b/src/jexer/teditor/Document.java index 5b7050f..42082e5 100644 --- a/src/jexer/teditor/Document.java +++ b/src/jexer/teditor/Document.java @@ -28,6 +28,9 @@ */ package jexer.teditor; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; @@ -54,6 +57,11 @@ public class Document { */ private boolean overwrite = false; + /** + * If true, the document has been edited. + */ + private boolean dirty = false; + /** * The default color for the TEditor class. */ @@ -73,6 +81,41 @@ public class Document { 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. * @@ -136,6 +179,15 @@ public class Document { 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. * @@ -280,6 +332,7 @@ public class Document { * Delete the character under the cursor. */ public void del() { + dirty = true; lines.get(lineNumber).del(); } @@ -287,6 +340,7 @@ public class Document { * Delete the character immediately preceeding the cursor. */ public void backspace() { + dirty = true; lines.get(lineNumber).backspace(); } @@ -297,6 +351,7 @@ public class Document { * @param ch the character to replace or insert */ public void addChar(final char ch) { + dirty = true; if (overwrite) { lines.get(lineNumber).replaceChar(ch); } else {