import java.io.*;
import java.util.*;

/**
* OpenFile defines a opened file within JPE, it handles its buffers
* status and how it gets loaded and saved.
*/
public class OpenFile extends Object {

	String filename;
	String text="";
	int savedpos=0;
	int	errorpos=-1;
	boolean dirty=false;
	boolean compiled=false;
	boolean locked=false;

	/**
	* Create a Openfile if possible it will load it from storage
	*/
	public OpenFile(String filename) {
		this.filename=filename;
		loadFile();
	}

	

	/**
	* get the name of the file
	*/
	public String getName() {
		return(filename);
	}

	/**
	* set a new filename for this file
	*/
	public void setName(String filename) {
		this.filename=filename;
	}


	/**
	* load the file from disk into the buffer
	*/
	public void loadFile() {
		String part;
		StringBuffer all=new StringBuffer("");
		try
		{
			File file=new File(filename);
			FileReader fr = new FileReader(file);
			BufferedReader br=new BufferedReader(fr);

			while ( (part=br.readLine()) != null)
			{
				all.append(part);
				all.append('\n');
			}

			br.close();
			fr.close();
			text=all.toString();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		dirty=false;
	}

	/**
	* return the text of this file
	*/
	public String getText() {
		return(text);
	}

	/**
	* set the text of this file
	*/
	public void setText(String text) {
		this.text=text;
	}

	/**
	* append text to the current file
	*/
	public void appendText(String text) {
		this.text+=text;
	}

	
	/**
	* save file to storage
	*/	
	public void saveFile() {
		try {
			File file=new File(filename);
			FileWriter fw = new FileWriter(file);
			BufferedWriter bw=new BufferedWriter(fw);
			bw.write(text,0,text.length());
			bw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		dirty=false;
	}


	/**
	* set the cursor position within the file
	*/
	public void setCaretPosition(int pos) {
		savedpos=pos;
	}

	/**
	* get the cursor position within the file
	*/
	public int getCaretPosition() {
		return(savedpos);
	}

	public int getErrorPos() {
		return(errorpos);
	}

	public void setErrorPos(int errorpos) {
		this.errorpos=errorpos;
	}	

	public boolean isCompiled() {
		return(compiled);
	}

	public boolean isLocked() {
		return(locked);
	}

	public boolean isDirty() {
		return(dirty);
	}

	public void setDirty() {
		dirty=true;
		clearCompiled();
	}

	public void clearDirty() {
		dirty=false;
	}

	public void clearCompiled() {
		compiled=false;
	}
	public void setCompiled() {
		compiled=true;
	}

	public void clearLocked() {
		locked=false;
	}

	public void setLocked() {
		locked=true;
	}


}
