Line.java 1.26 KB
package pad.prac1;

public class Line 
{
	/*
	 * MODE DEFINITIONS
	 */
	
	public static final int INSERT 			= 0;
	public static final int OVERWRITE 		= 1;
	
	private String line;
	private int cursorPosition;
	private int writeMode;
	
	public Line()
	{
		line = "";
		writeMode = INSERT;
		cursorPosition = 1;
	}
	
	public int length()
	{
		return line.length();
	}
	
	public int getCursorPosition()
	{
		return cursorPosition;
	}
	
	public void setCursorPosition(int pos)
	{
		cursorPosition = pos;
	}
	
	public int getMode()
	{
		return writeMode;
	}
	
	public void toggleMode()
	{
		writeMode = 1 - writeMode;
	}
	
	public void removeCharAt(int pos)
	{
		if(pos >= 0)
		{
			if(pos == 0)
			{
				line = line.substring(1);
			}
			else if(pos == line.length()-1)
			{
				line = line.substring(0, line.length()-1);
			}
			else
			{
				line = line.substring(0, pos).concat(line.substring(pos+1,line.length()));
			}
		}
	}
	
	public void insertCharAt(char c, int pos)
	{
		if(pos == 0)
		{
			String s = "";
			s += c;
			line = s + line;
		}
		else if(pos == line.length())
		{
			line += c;
		}
		else
		{
			String s = "";
			s += c;
			line = line.substring(0, pos).concat(s).concat(line.substring(pos,line.length()));
		}
	}
	
	public String toString()
	{
		return line;
	}
	
}