Blame view

readline/src/pad/prac1/Line.java 2.67 KB
1
2
package pad.prac1;
3
4
5
import java.util.Observable;

public class Line extends Observable
6
7
8
9
{
	private String line;
	private int cursorPosition;
	private int writeMode;
10
	private Console console;
11
12
	public Line(Console cons)
13
	{
14
		console = cons;
15
		line = "";
16
		writeMode = EditableBufferedReader.INSERT;
17
		cursorPosition = 1;
18
		addObserver(console);
19
20
21
22
23
24
25
26
27
28
29
30
	}

	public int length()
	{
		return line.length();
	}

	public int getCursorPosition()
	{
		return cursorPosition;
	}
31
	public void toggleMode()
32
	{
33
		writeMode = 1 - writeMode;
34
35
	}
36
	private void sendCommand(int type, int value)
37
	{
38
39
		setChanged();
		notifyObservers(new Command(type,value));
40
41
	}
42
	private void sendCommand(int type)
43
	{
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
		setChanged();
		notifyObservers(new Command(type));
	}

	private 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()));
		}
66
67
	}
68
	private void removeCharAt(int pos)
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	{
		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()));
			}
		}
	}
87
	public void addChar(char c)
88
	{
89
		if(c == EditableBufferedReader.LINE_FEED)
90
		{
91
			sendCommand(Command.INSERT_CHAR,EditableBufferedReader.RETURN_KEY);
92
		}
93
		switch(writeMode)
94
		{
95
96
97
98
			case EditableBufferedReader.INSERT:
				sendCommand(Command.INSERT_CHAR,(int)c);
				insertCharAt(c, cursorPosition-1);
				cursorPosition++;
99
				break;
100
101
102
103
104
105
106
107
108
109
110

			case EditableBufferedReader.OVERWRITE:
				if(cursorPosition != line.length()+1)
				{
					removeCharAt(cursorPosition-1);
					sendCommand(Command.DELETE_CHAR);
				}
				sendCommand(Command.INSERT_CHAR,(int)c);
				insertCharAt(c, cursorPosition-1);
				cursorPosition++;
			break;
111
		}
112
113
114
115
116
	}

	public void delChar(int mode)
	{
		switch(mode)
117
		{
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			case EditableBufferedReader.DEL_BACKWARD:
				if(cursorPosition != 1)
				{
					sendCommand(Command.MOVE_CURSOR,cursorPosition-1);
					sendCommand(Command.DELETE_CHAR);
					removeCharAt(cursorPosition-2);
					cursorPosition--;
				}
			break;
			case EditableBufferedReader.DEL_FORWARD:
				if(cursorPosition != (line.length()+1))
				{
					removeCharAt(cursorPosition-1);
					sendCommand(Command.DELETE_CHAR,1);
				}
			break;
		}
	}

	public void setCursorTo(int pos)
	{
		if((pos <= line.length()+1) && (pos >= 1) && (pos != cursorPosition))
		{
			sendCommand(Command.MOVE_CURSOR,pos);
			cursorPosition = pos;
143
144
145
146
147
148
149
150
151
		}
	}

	public String toString()
	{
		return line;
	}

}