Blame view

readline/src/pad/prac1/Line.java 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;
	}

}